Loops - Problem

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
void createPrimes (int max){
	int i, j;
	bool k = 0;

	for ( i = 2; i <= max; i++){

		do {

			for( j = i-1; j > 1; j--){
		
				if (isWhole((i/j), 0) == true){
				k = false;
				}else{
				k = true; 
				}

			}

			}while (k == true);
		if (k == true){
			writeFile (i);
			cout << "Prime.\n";
		}
	}
}


Hi. I can't seem to get this working. Staring at it too long I imagine. All other functions work perfectly, exept this.

I'm trying to write a list of all prime numbers from 2 through max. Sofar I've just gotten every number, none, or just 2.

isWhole() checks if a number is whole
writeFile() writes to file, just incase some didn't get that.

Anyone seing any errors?
isWhole() checks if a number is whole
wonder how are you doing that.
cout << (5/3);

1
2
3
4
5
6
7
8
9
10
11
12
13
		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.
Last edited on
Put the code of isWhole, ¿what floats?
I don't explain it properly. The integer division returns an integer.
1
2
int a, b;
a/b; //returns an int 
1
2
3
bool isWhole(float x, float e){
	return ( (x - int (x)) <= e);
}


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...
Last edited on
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?
Hi, the 2 is being printed because the inner loop is not being executed
1
2
3
for ( j = i -1; j > 1; j-- ){ 
//when i=2 that translates to
for( j=1; j>1; j--) //the condition is false, so is never executed 


To test if the number is divisible, use the modulo o remainder
i%j ==0

(is like doing n - d * floor(n/d))
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.
Topic archived. No new replies allowed.