-1

I am a beginner at learning python 3 and I am just writing basic programs. I wrote this simple program which would take a number in and divide it by numbers starting from 1 to the square root of the number and find the remainders and add it to a list and print it.

import math

def prime_checker(num):
    n=1
    list_of_remainder=[]
    while n == math.floor(num**0.5):
        var=int(num % n)
        list_of_remainder.append(var)
        n += 1
    return list_of_remainder

var=prime_checker(10)
print(var)

Please tell me what I did wrong. I would like to point out here that I did try to research a bit and find error but I couldn't and only then have I posted this question. The problem that I faced was that it printed out an empty list.

Koushik Sahu
  • 99
  • 1
  • 1
  • 9
  • What's the problem? An error? Bad output? Being vague doesn't help us help you. – Carcigenicate Jun 17 '18 at 15:04
  • 2
    You have: `while n == math.floor(...)` shouldn't that be: `while n <= math.floor(...)`? – Iridium Jun 17 '18 at 15:04
  • Possible duplicate of [Improve code to find prime numbers](https://stackoverflow.com/questions/50752438/improve-code-to-find-prime-numbers) – Olivier Melançon Jun 17 '18 at 15:19
  • [PythonTutor](http://pythontutor.com/visualize.html#mode=edit) or any other debugger can help you identifying these kind of problems. – Mr. T Jun 17 '18 at 16:18

2 Answers2

1

to start with, your while loop is not executed even once. The condition for your while loop is while n == math.floor(num**0.5): The argument num you are passing to the function prime_checker is equal to 10. In this case your condition test is: while 1 == math.floor(10**0.5) which is while 1 == 3 which is obviously not true and as a result the loop is not executed even once.

1
import math

def prime_checker(num):
    list_of_remainder = []
    number=num;
    n=1
    x=math.floor(number**0.5)

    while n <= x:

        v=int(number % n)

        list_of_remainder.append(v)
        n += 1

    return list_of_remainder

var=prime_checker(10)
print(var)
Rithesh Bhat
  • 127
  • 1
  • 12