just practicing for fun on some problem i saw posted on yahoo answers,mathematics section
problem: xyz times pqr = some number
x,y,z,p,q,r should be 2, 3, 5, or 7. and some number's digits should also be made
up entirely of 2, 3, 5, or 7.
solution:
335 x 753 = 252255 or 753 x 335 = 252255
353 x 775 = 273575 or 775 x 353 = 273575
355 x 725 = 257375 or 725 x 355 = 257375
377 x 725 = 273325 or 725 x 377 = 273325
my program starts with x = y = 222. multiplies x times every y from 222 through 777.
then increments x by 1 and loops until x is 777. so the first thing it does is 222 x 222. the last thing it does is 777 x 777.
if x, y, and the product are all composed entirely of 2's, 3's, 5's, or 7's, then it will output to the file.
my code is crude/ugly/ and generally representative of my programming skills.
please help me improve by explaining to me:
How would you write this?
Thanks!
#include <iostream>
#include <cmath>
#include <fstream.h> // file I/O
#include <iomanip.h> // format manipulation
using namespace std;
void main()
{
int test;
int i;
int j=1;
int x=222;
int y=222;
ofstream myfile;
myfile.open ("alexprimeproducts.txt");
while ( j<556){
for (i=1; i<556; i++){
if (x*y %10 == 2 || x*y %10 == 3 ||x*y %10 == 5 ||x*y %10 == 7 )
{
if (x*y/10 %10 == 2 || x*y/10 %10 == 3 ||x*y/10 %10 == 5 ||x*y/10 %10 == 7 )
{
if (x*y/100 %10 == 2 || x*y/100 %10 == 3 ||x*y/100 %10 == 5 ||x*y/100 %10 == 7 )
{
if (x*y/1000 %10 == 2 ||x*y/1000 %10 == 3 ||x*y/1000 %10 == 5 ||x*y/1000 %10 == 7 )
{
if (x*y/10000 %10 == 2||x*y/10000 %10 == 3 ||x*y/10000 %10 == 5 ||x*y/10000 %10 == 7 )
{
if (x*y/100000 %10 == 2||x*y/100000 %10 == 3 ||x*y/100000 %10 == 5 ||x*y/100000 %10 == 7 )
{
if (x %10 == 2 || x%10 == 3 || x%10 == 5 || x%10 == 7)
{
if (y %10 == 2 || y%10 == 3 || y%10 == 5 || y%10 == 7)
{
if (x/10 %10 == 2 || x/10%10 == 3 || x/10%10 == 5 || x/10%10 == 7)
{
if (y/10 %10 == 2 || y/10%10 == 3 || y/10%10 == 5 || y/10%10 == 7)
{
if (x/100 %10 == 2 || x/100%10 == 3 || x/100%10 == 5 || x/100%10 == 7)
{
if (y/100 %10 == 2 || y/100%10 == 3 || y/100%10 == 5 || y/100%10 == 7)
{
//cout << x << " " << y << " " << x*y << endl;
myfile << x << " x " << y << " = " << x*y << endl;
}
}
}
}
}
}
}
}
}
}
}
}
x++;
} //end of first for
x=222;
y++;
j++;
} //end of while
myfile.close();
//cin >> test;
}