For the love of Christ..
Date: 06/02/05
(Code WTF) Keywords: programming
[Error: Irreparable invalid markup ('') in entry. Owner must fix manually. Raw contents below.]
I am new to programming in C++, about a week old. I've got past experience in scripting macros, basic shell scripting, etc. I'm trying to get a very simple method for prime number generation working, but it's dying at my second if statement.. and my brain hurts.
#include
typedef long unsigned int ulong;
long unsigned int mplier1 = 1, mplier2 = 1, pfactor = 1, pcheck, searchcount = 0, usercount = 0;
void primeloop();
ulong mathcheck(ulong x, ulong y);
int main()
{
cout << "How many Primes do you wish to find?: ";
// Define usercount.
cin >> usercount;
cout << "\n";
// Begin the Prime sweep.
primeloop();
return 0;
}
void primeloop()
{
// Has the user's criteria been met?
if ( usercount == searchcount )
{
cout << "\n\nYour Prime criteria of " << usercount << " has been met.\n";
cout << "Exiting...";
return;
}
// Generate product to check against primary factor.
// cout << mathcheck(mplier1,mplier2) << endl;
pcheck = mathcheck(mplier1,mplier2);
// If the neither multiplier is equal to the factor, increase multiplier 2.
if ( ( mplier1 != pcheck ) && ( mplier2 != pcheck ) )
{
// cout << "Check 1\n";
mplier2++;
primeloop();
}
// If multiplier 1 doesn't equal the factor but multiplier 2 does, increment multiplier 1 and reset multiplier 2.
else
if ( ( mplier1 != pcheck ) && ( mplier2 == pcheck ) )
{
// cout << "Check 2\n";
mplier2 = 0;
cout << mplier1++ << endl;
primeloop();
}
// If the factor has been matched by 2 lesser intergers, move to next factor.
if ( ( pfactor == pcheck ) && ( mplier1 != 1 ) && ( mplier2 != pfactor ) )
{
// cout << "Check 3\n";
mplier1 = 1;
mplier2 = 0;
pfactor++;
primeloop();
}
// If the factor cannot be matched by 2 lesser intergers, it is a prime.
if ( ( pfactor == pcheck ) && ( mplier1 == pcheck ) && ( mplier2 == pcheck ) )
{
// cout << "Found prime:\n";
cout << pfactor << endl;
mplier1 = 1;
mplier2 = 0;
pfactor++;
searchcount++;
primeloop();
}
}
ulong mathcheck(ulong x, ulong y)
{
return x * y;
}
Source: http://www.livejournal.com/community/code_wtf/8555.html