do {
for( j = i-1; j > 1; j--){
if (isWhole((i/j), 0) == true){
k = false;
}else{
k = true;
}
}
}while (k == true);
The while loop will end when k will be false. In the for loop you are setting k = not isWhole(i/j, 0);, but the only value that will mater is k = not isWhole( i/2, 0 );
Maybe you want to break;
Still you may want to modularise a little more
1 2 3
for (int K = 2; K <= max; K++)
if( is_prime(K) )
writeFile(K);
Also k==true. Don't do that, k is a boolean, just ask for its value, if( k )
That certainly cleaned it up, but the problem still exist.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
void createPrimes (int max){
int i, j;
bool k;
bool K;
for ( i = 2; i <= max; i++){ //Creating numbers from 2 through max
K = true; //Assuming i is a prime before disproven by j-loop
for ( j = i -1; j > 1; j-- ){ //For each number created, creates all numbers from max - 1 through 2
k = (isWhole(i/j, 0)); //Gets truth about i/j is a whole number. true = whole
if(k){ //If k is a whole
K = !k; //Masterswitch K is false, and the whole thing will
break; //break to the next i-loop step
}
}
if(K)
writeFile (i); //If K still is true after j-loop then write i to file
}
}
I now only get the number '2' And I have the nasty feeling I'm missing an important point somehow.
The isWhole function calls upon a number and accuracy (here = 0), both floats, then forces the number into an int, before subtracting them. If the remainder = 0, it returns True.
Hang on, so it processes i/j before it forces it into a float, thereby making sure that every number comes out as a whole, none as prime. But then the 2 shouldn't be written at all... hmm...
Sorry, my mind is going. I tested isWhole earlier with division inside the func. call and it wored perfectly... Any idea on what's holding the script back?
Yes! it's working! My evil minions can now utilize the power of primes, soon the world shall thank the saint ne555!
But really thanks for pointing towards %. Before I implemented it I reversed the order, now counting downwards, but I do not know if this has any significance. (after i=2, i=3 comes along through the loop one time and so forth) I'll probably figure exactly what went wrong and why, but now I'm just too happy. Thanks again ne.