Taxicab numbers | Rosetta Code | #12
URL to the problem page: http://rosettacode.org/wiki/Taxicab_numbers
A taxicab number (the definition that is being used here) is a positive integer that can be expressed as the sum of two positive cubes in more than one way.
The first taxicab number is 1729, which is:
13 + 123 and
93 + 103.
Taxicab numbers are also known as:
taxi numbers
Hardy-Ramanujan numbers
Compute and display the lowest 25 taxicab numbers.
A taxicab number (the definition that is being used here) is a positive integer that can be expressed as the sum of two positive cubes in more than one way.
The first taxicab number is 1729, which is:
13 + 123 and
93 + 103.
Taxicab numbers are also known as:
taxi numbers
Hardy-Ramanujan numbers
Compute and display the lowest 25 taxicab numbers.
#include <iostream>
using namespace std;
int power(int a, int b) {
int result = 1;
for (int i = 0; i < b; i++) {
result *= a;
}
return result;
}
int main() {
int cnt = 0, cnt2, tmp;
cout << "Lowest 25 taxicab numbers are:" << endl;
for (int i = 1729; cnt < 25; i++) {
cnt2 = 0;
tmp = 0;
for (int j = 1; j < i; j++) {
if (power(j, 3) >= i) {
break;
}
if (j == tmp) {
continue;
}
for (int k = 1; k < i; k++) {
if (power(k, 3) >= i || (power(j, 3) + power(k, 3)) > i) {
break;
}
if ((power(j, 3) + power(k, 3)) == i) {
tmp = k;
cnt2++;
break;
}
}
if (cnt2 == 2) {
cnt++;
cout << i << endl;
break;
}
}
}
return 0;
}
Comments
Post a Comment