Project Euler Homework
|
00001 #include <iostream> 00002 using namespace std; 00003 unsigned gcd(unsigned p, unsigned q) 00004 { 00005 //assume p<q 00006 unsigned r = q%p; 00007 while (r>0) { 00008 q=p; 00009 p=r; 00010 r = q%p; 00011 } 00012 return p; 00013 } 00014 00015 int main() 00016 { 00017 unsigned prod_p=1,prod_q=1; 00018 00019 for (unsigned p=10;p<=99;p++) 00020 for (unsigned q=p+1;q<=99;q++) 00021 if ( 00022 (p%10==q/10 && p/10*q==q%10*p) || 00023 (p/10==q%10 && p%10*q==q/10*p) 00024 //(p%10*q==q%10*p && p%10!=0) 00025 ) { 00026 cout << p <<'/'<<q<< endl; 00027 prod_p*=p; 00028 prod_q*=q; 00029 } 00030 00031 unsigned hcf = gcd(prod_p,prod_q); 00032 cout << prod_q/hcf << endl; 00033 00034 return 0; 00035 }