I wanted to ask for help with the 1000th prime number problem that I am close to solving in my 5th hour of working on it. I do not know it is not dropping into the final section of the code to declare "number" as prime?
float number = 101.0; float place_holder = 0; boolean is_prime = true; float divisor = 3.0; //int test_number = int(ceil(sqrt(number))); void setup() { if (number == 0) { println(number + " is undefined"); } else if (number > 0 && number <= 3) { println(number + " is prime"); } else if (number % 2 == 0 && number > 2) { println(number + " is not prime") ; } else if (number > 2 && number % 2 !=0) { while ( is_prime == true) { if (number % divisor == 0) { is_prime = false; divisor = 3; println(number+ " is not prime"); number = number +1; } else if ( number % divisor !=0) { //THIS CODE RIGHT HERE IS WHERE MY PROBLEM IS divisor = divisor + 1; //IT WONT ALLOW ME TO GO DEEPER IN THE ALOGORITHM println(number + "number" + divisor + "divisor"); } else if ( number -1 == divisor) { println(number + " is prime"); is_prime = true; } } } }
which code is this?C,C++.....
the processing programming language from processing.org
mmm where first u have a redundancy, here in the second statement with : } else if (number > 2 && number % 2 !=0) { well u already checked that before, u can use an else instead of evaluating 2 times the same condition. Second u are using floatating numbers :S, primes arent natural numbers? Using operations over flotating points lead to errors in a computers, remember u CANT represent all real numbers in a computer. I think thats the problem, code seems ok for me, but i just runned it on my head xD. There are already some algorithms to print all primes: http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes http://en.wikipedia.org/wiki/Sieve_of_Atkin U u can find more info here too: http://en.wikipedia.org/wiki/Prime_number
Join our real-time social learning platform and learn together with your friends!