// PYTHAGOREAN TRIPLETS FINDER
// Find all distinct sets of Pythagorean numbers less than 100
// Relation: a² + b² = c² where a < b < c < 100
#include <stdio.h>
#include <math.h> // For sqrt() function
int main() {
printf("PYTHAGOREAN TRIPLETS FINDER\n");
printf("Finding all triplets (a,b,c) where a² + b² = c²\n");
printf("Constraints: a < b < c < 100\n");
printf("==========================================\n\n");
printf("%-5s %-5s %-5s %-10s %-10s %-10s\n",
"a", "b", "c", "a²", "b²", "c²");
printf("%-5s %-5s %-5s %-10s %-10s %-10s\n",
"---", "---", "---", "--------", "--------", "--------");
int triplet_count = 0; // Counter for total triplets found
// ALGORITHMIC DECISION: Why WHILE loops instead of FOR loops?
// We're doing a SEARCH-BASED operation where we check conditions
// and may terminate early based on mathematical constraints
int a = 1; // Start from smallest possible value
// OUTER LOOP: Iterate through possible values of 'a'
// UPPER BOUND REASONING: If a gets too large, even with minimum b and c,
// c will exceed 100. Through analysis, a cannot exceed ~70
while(a < 67) { // Conservative upper bound to ensure c < 100
int b = a + 1; // b must be greater than a
// INNER LOOP: For each 'a', find all valid 'b' values
// DYNAMIC UPPER BOUND: b < √(10000 - a²) to ensure c < 100
int max_b = (int)sqrt(10000 - a*a); // Mathematical constraint
while(b < max_b) {
// MATHEMATICAL COMPUTATION: Calculate c² = a² + b²
int a_squared = a * a;
int b_squared = b * b;
int c_squared = a_squared + b_squared;
// KEY INSIGHT: Check if c is a perfect integer
// We calculate c using sqrt() but need to verify it's exactly an
integer
double c_double = sqrt(c_squared);
int c = (int)c_double;
// VALIDATION CONDITIONS:
// 1. c must be exactly an integer (no fractional part)
// 2. c must be less than 100
// 3. a < b < c must hold (automatically satisfied by our loop
structure)
if(c_double == c && c < 100) {
// FOUND A VALID PYTHAGOREAN TRIPLET!
printf("%-5d %-5d %-5d %-10d %-10d %-10d\n",
a, b, c, a_squared, b_squared, c_squared);
triplet_count++;
// VERIFICATION: Let's double-check our math
if(a_squared + b_squared != c_squared) {
printf("ERROR: Mathematical verification failed!\n");
}
}
b++; // Move to next possible value of b
}
a++; // Move to next possible value of a
}
printf("\n=== RESULTS SUMMARY ===\n");
printf("Total Pythagorean triplets found: %d\n", triplet_count);
// MATHEMATICAL INSIGHTS FOR VIVA DISCUSSION:
printf("\n=== MATHEMATICAL INSIGHTS ===\n");
printf("1. Most famous triplet: (3,4,5) where 3² + 4² = 5²\n");
printf("2. Multiples of basic triplets are also valid:\n");
printf(" (3,4,5) → (6,8,10), (9,12,15), (12,16,20), etc.\n");
printf("3. Primitive triplets (no common factors): ");
printf("(3,4,5), (5,12,13), (8,15,17), etc.\n");
// ALGORITHM ANALYSIS:
printf("\n=== ALGORITHM ANALYSIS ===\n");
printf("Approach: Nested while loops with mathematical constraints\n");
printf("Outer loop: a from 1 to ~67\n");
printf("Inner loop: b from (a+1) to √(10000-a²)\n");
printf("Optimization: Dynamic upper bound prevents unnecessary iterations\n");
return 0;
}
/*
=== VIVA PREPARATION NOTES ===
1. WHY WHILE LOOPS INSTEAD OF FOR LOOPS?
- This is a SEARCH problem, not a counting problem
- We have dynamic bounds that change based on current values
- while loops better express the "search until condition" logic
- The bounds aren't simple arithmetic progressions
2. KEY ALGORITHMIC OPTIMIZATIONS:
- Dynamic upper bound for b: prevents checking impossible combinations
- Early constraint checking: saves computation time
- Integer validation: ensures c is exactly an integer, not approximate
3. MATHEMATICAL INSIGHTS:
- Constraint analysis: a² + b² = c² with c < 100
- Derived bounds: b < √(10000 - a²)
- Perfect square detection: crucial for valid triplets
4. POTENTIAL VIVA QUESTIONS:
Q: "Why not use three nested loops for a, b, c?"
A: "More efficient to calculate c from a,b than search all c values"
Q: "How do you ensure a < b < c?"
A: "Loop structure: b starts from a+1, c calculated from a,b"
Q: "What's the time complexity?"
A: "Roughly O(n²) where n ≈ 67, but optimized by dynamic bounds"
5. EDGE CASES HANDLED:
- Non-integer square roots (filtered out)
- c ≥ 100 (prevented by dynamic bounds)
- Duplicate checking (prevented by a < b constraint)
6. COMPILATION NOTES:
- Requires -lm flag for math library (sqrt function)
- gcc -o pythagorean pythagorean.c -lm
*/