Hi all, i'm new to any programming . What i am trying to do is calculate prime numbers . Can anyone check this code and see whats wrong num = int(raw_input('number here')) for x in range (2, num): if num % x !=0: print 'is prime'
Are you trying to tell if 'num' is prime or print all prime numbers until 'num'?
was trying to print 'is prime ' if user enters a prime number
So the basic idea for checking a prime number is this: For each number n from 2 to num - 1: if number is divisible by n, it's not prime. So I would recommend setting a boolean variable
But first, let's get the modular division right: If x is divisible by n, x % n = 0
So let's make some code: num = int(raw_input('number here')) boolean isPrime = true for x in range (2, num - 1): //not num, because num is always divisible by itself if num % x == 0: isPrime = false if (isPrime) print 'is prime' else print 'is not prime'
That's a very basic example. It won't perform extremely well. If you want a challenge and are new to programming, I would make a function called isPrime(x) that, instead of setting isPrime to false, just returns false. Then have whatever function called isPrime print the corresponding text depending on isPrime's return value.
The advantage to the above is that, as soon as it determines it's not prime, it stops. Whereas in my first solution, it'll continue to loop through the numbers (when we really don't need to). They are both valid.
Thnx for quick response matt. I will try your code and experiment. been learning python exactly one week and finding learning curve pretty steep so appreciate your help
No problem. Any more questions just post here (or the other Computer Science group). Also, please give me a medal if I helped :). Good luck!
let's take a look at what your program actually does and where it goes astray. 1: the first line asks for an input and assigns it the variable num; you also convert it to type integer. so far so good. 2: then you create loop that begins with 2 and goes up to num. your intent here makes sense. you are planning to divide num by each number beginning with 2 up to num. so far so good. 3: next you use a condition to test whether num is divisible by the number in the for loop. the condition makes sense. if num = 7, for example, and the starting number in the for loop is 2, then 7%2 (7 modulo 2) does not equal 0, meaning that 7 is not divisible by 2. But this is where you go wrong. From that alone you can't conclude that the number is prime or not prime. Suppose the user inputs the number 6, which is not prime. Your program performs as follows: it assigns 6 to num. then x is assigned 2 as part of the for loop. because 6 is divisible by 2, the if condition is not satisfied. we return to the top of the for loop. x increments by 1 to 3. 6 is divisible by 3. x increments to 4. now 6 is *not* divisible by 4 (i.e. without remainder). therefore, your program prints "is prime". well. that's incorrect. then your program continues to increment x to 5. 6 is not divisible by 5 so it prints "is prime" again. What your program computes is not whether a number is prime or not but whether it is has a divisor.
Thanks malpaso That totally makes sense . I guess writing it out in plain English helps . Back to the drawing board :)
Join our real-time social learning platform and learn together with your friends!