Use mouse to select the text and paste into your editor
// FILE: triples1.cpp (Use mouse to select the text and paste into your editor)
// PURPOSE: Use nested loops to print out Pythagorean triples
//=================================================================
#include <iomanip.h>
int main() {
//===============================================================
// PROBLEM: Print out Pythagoran triples more effectively than
// program currently does
//==============================================================
int a, b, c; // The lengths of the triangle's sides
int max(20), wdth(6), count(0);
cout << "\n\n\n\n\n\t\t\t" << "Printing Pythagorean Triples" << endl << endl;
// ==============================================================
// INSTRUCTIONS:
// 1. The program currently prints triples for all a,b,c in [3,20]--
// That's 36 triples.
// 2. Modify the CONDITION AND nested loops to prevent duplicate triples from
// being printed. EXAMPLE: if 5 12 13 is printed, then 12 13 5 shouldn't be.
// That's only 6 triples!!!
// 3. Define any additional variables you need. (You shouldn't need any)
// =================================================================
// THE NESTED LOOPS:
cout << " n a b c\n\n";
for ( a = 3; a <= max; ++a ) {
for ( b = 3; b <= max; ++b ) {
for ( c = 3; c <= max; c++ ) {
if ( a*a + b*b == c*c || a*a + c*c == b*b || b*b + c*c == a*a ) {
count += 1;
cout << setw(wdth) << count << setw(wdth) << a << setw(wdth) << b
<< setw(wdth) << c << endl;
}
} //*** for c
} //*** for b
} //*** for a
cout << "\nPress <ENTER> to continue...";
cin.get();
return 0;
}
// FILE: perfect.cpp (Use mouse to select the text and paste into your editor)
// PURPOSE: Use functions to find perfect and amicable numbers
//-----------------------------------------------------------------
// 1. First complete sigma() as specified below. Run the program
// to enable the testSigma() function to validate your solution.
// 2. Once sigma() is validated, complete isPerfect() as specified
// below. Use sigma() to build isPerfect(). Run program to enable
// testIsPerfect() to validate your
// solution.
// 3. Once isPerfect() is validated, complete listAmicable()
// as specified below.
//-----------------------------------------------------------------
#include <iomanip.h>
#include <math.h>
#include <string>
int sigma(int n) {
// precondition: n > 0
//postcondition: returns sum of n's proper divisors
//EXAMPLE: sigma(8) = 1+2+4 = 7 and sigma(12) = 1+2+3+4+6 = 16
//------------------------------------------------------------
return -77;
}
bool isPerfect(int n) {
// precondition: n > 1
// postcondition: Uses sigma() to return true when n is the sum of its
// proper divisors, otherwise return false
// EXAMPLE: 6 is perfect since 1 + 2 + 3 = 6,
// 28 because 1 + 2 + 4 + 7 + 14 = 28
//------------------------------------------------------------
return true;
}
void listAmicable(void) {
//postcondition: lists amicable pairs from 2 to n
// INSTRUCTIONS: Use sigma function to print all
// amicable pairs with at least one value < n
// NOTE: Do not print duplicate pairs: (a,b) precludes (b,a)
//------------------------------------------------
const int LAST(100000);
string title("Listing Amicable Pairs from 2 to ");
cout << "\n\n\n\n\n" << setw(37 + title.length()/2) << title.c_str()
<< LAST << endl << endl;
cout << "You should print out 13 unique pairs, "
<< "the last being (79750, 88730)\n\n";
//--------------------------------------------------------
// YOUR CODE:
}
void testSigma(void);
void testPerfect(void);
void wait();
int main() {
testSigma();
testPerfect();
listAmicable();
wait();
return 0;
}
////////////////////////////////////////////
// SUPPORT FUNCTIONS
////////////////////////////////////////////
void wait() {
cout << "\nPress <ENTER> to continue: ";
cin.ignore(10, '\n');
}
void testSigma(void) {
int table[20] = { 0 , 1, 1, 3, 1, 6, 1, 7, 4,
8, 1, 16, 1, 10, 9, 15, 1, 21, 1, 22 };
string title("Testing your sigma() function");
cout << "\n\n\n\n\n" << setw(39 + title.length()/2) << title.c_str()
<< endl << endl;
int errorCount(0);
for (int k = 2; k <= 20; k++) {
cout << " sigma(" << setw(2) << k << ") = " << setw(2) << table[k-1]
<< " and your sigma() returns " << setw(3) << sigma(k) << " "
<< ((sigma(k)==table[k-1])? "OK\n" : "***ERROR\n");
if ( sigma(k) != table[k-1] )
++errorCount;
}
if ( errorCount != 0 ) {
cout << "\n\nYour sigma() doesn't work yet: there were "
<< errorCount << " errors.\n";
cout << "Keep working on sigma() before starting isPerfect()\n";
}
else {
cout << "\n\nYour sigma() is fine. You're ready to start on isPerfect()\n";
cout << "Go to main() and delete the call to testSigma()\n";
}
wait();
}
void testPerfect(void) {
// precondition: LAST > 1
//postcondition: uses isPerfect() to list perfect numbers from 2 to LAST
const int LAST(9000);
string title("Testing your isPerfect() function");
cout << "\n\n\n\n\n" << setw(39 + title.length()/2) << title.c_str()
<< endl << endl;
int table[] = {6, 28, 496, 8128}, perfectCount(0);
const int numPerfects = sizeof(table)/sizeof(int);
cout << "Printing perfect numbers in [2," << LAST << "]\n\n";
for ( int k = 2; k <= LAST; k++) {
if ( isPerfect(k) ) {
++perfectCount;
if ( perfectCount > numPerfects || k != table[perfectCount-1] ) {
cout << "\n\nYour isPerfect() doesn't work yet: " << k
<< " isn't a perfect number\n";
cout << "Keep working on isPerfect()\n";
return;
}
else {
cout << setw(3) << perfectCount << "." << setw(6) << k << endl;
}
}//** if isPerfect
}//** for
if ( perfectCount != numPerfects ) {
cout << "\n\nYour isPerfect() doesn't work yet: you should have found "
<< numPerfects << " perfect numbers, but " << perfectCount
<< " were found.\n";
}
else {
cout << "\n\nYour isPerfect() is fine. "
<< "You're ready to start on listAmicable()\n";
cout << "Go to main() and delete the call to testPerfect()\n";
}
wait();
}
// FILE: triples2.cpp (Use mouse to select the text and paste into your editor)
// PURPOSE: Use nested loops to print out Pythagorean triples with
// generating numbers (m, n)
//=================================================================
#include <iomanip.h>
int main() {
//===============================================================
// PROBLEM: Print out Pythagoran triples without using a
// Pythagorean if condition
//==============================================================
int m, n; // The generating numbers
int a, b, c; // The lengths of the triangle's sides
int max(8), wdth(8), count(0);
cout << "\n\n\n\n\n\t\t\t" << "Printing Pythagorean Triples" << endl << endl;
// ==============================================================
// INSTRUCTIONS:
// 1. Modify the for statements to reduce output lines to 28
// 2. Modify the inner loop by using generating numbers (m,n) to
// assign values to a, b, c
// 3. If max = 8, you should generate 28 unique triples, the last
// being (15, 112, 113)
// 4a. CHALLENGE: WITH AN IF STATEMENT that refers only to m and n, generate
// and print only primitive triples, triples where GCD(a, b, c) = 1
// HINT: This requires a lot of code because it's an odd/even problem
// and a GCD problem!! There are 15, the last being (15, 112, 113)
// 4b. CHALLENGE: WITHOUT AN IF STATEMENT, use algorithm in a manner
// that the hypotenuse of every triple is a perfect square.
// HINT: For each triple, use algorithm twice!!
// =================================================================
// CODE TO GENERATE THE 28 TRIPLES
for ( m = 1; m <= max; ++m )
for ( n = 1; n <= max; ++n ) {
if (count == 20) {
cout << "\nPress <ENTER> to continue...";
cin.get();
}
if ( count % 10 == 0 ) {
cout << " k m n a b c\n"
<< "------------------------------------------------\n";
}
//-----------------------------------------------------------
// YOUR CODE:
count += 1;
a = -77; b = -77; c = -77;
//------------------------------------------------------------
cout << setw(wdth) << count << setw(wdth) << m << setw(wdth) << n
<< setw(wdth) << a << setw(wdth) << b << setw(wdth) << c << endl;
}
cout << "\nPress <ENTER> to continue...";
cin.get();
cout << endl << endl;
return 0;
}
// end of triples2.cpp
expected output from triples3.cpp challenge
Printing Pythagorean Triples
k m n a b c
------------------------------------------------
1 4 3 7 24 25
2 8 6 28 96 100
3 12 5 119 120 169
4 15 8 161 240 289
5 16 12 112 384 400
6 24 7 527 336 625
7 24 10 476 480 676
8 21 20 41 840 841
9 30 16 644 960 1156
10 40 9 1519 720 1681
Press <ENTER> to continue...
k m n a b c
------------------------------------------------
11 35 12 1081 840 1369
12 32 24 448 1536 1600
13 36 27 567 1944 2025
14 48 20 1904 1920 2704
15 60 11 3479 1320 3721
16 48 14 2108 1344 2500
17 45 28 1241 2520 2809
18 42 40 164 3360 3364
19 56 33 2047 3696 4225
20 70 24 4324 3360 5476
Press <ENTER> to continue...
k m n a b c
------------------------------------------------
21 84 13 6887 2184 7225
22 63 16 3713 2016 4225
23 60 32 2576 3840 4624
24 55 48 721 5280 5329
25 64 48 1792 6144 6400
26 80 39 4879 6240 7921
27 96 28 8432 5376 10000
28 112 15 12319 3360 12769