0
$\begingroup$

I was trying to write my own code for primes in Python. I know that code already exists, but I am doing this to challenge my knowledge and make my own solution. I was wondering if any of you guys would tell me what's wrong with it.

factor_list = [ ]

def integer_test(x):

    if type(x) == int:
        return True
    else:
        return False

def factor_checker(x):

    count = 0
    y = x
    while count <= y:
        print(count)
        count += 1
        if integer_test(x / count) == True:
            factor_list.append(count)

factor_checker(45)

print(factor_list)

When I try to input a number for factor checker and then look at the factor list, I just get an empty list. What is wrong?

Thanks.

$\endgroup$

closed as off-topic by Yuval Filmus, David Richerby, FrankW, Wandering Logic, Luke Mathieson May 17 '14 at 14:00

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions about software development or programming tools are off-topic here, but can be asked on Stack Overflow." – Yuval Filmus, David Richerby, FrankW, Wandering Logic, Luke Mathieson
If this question can be reworded to fit the rules in the help center, please edit the question.

  • $\begingroup$ Welcome to Computer Science! Your question is off-topic here: we deal with computer science questions, not programming questions (see our FAQ). Your question might be on-topic on Stack Overflow or perhaps Code Review (but please choose one: don't post to both). $\endgroup$ – David Richerby May 17 '14 at 7:00
  • $\begingroup$ My mistake! :) Sorry about that. $\endgroup$ – louie mcconnell May 18 '14 at 23:16
1
$\begingroup$

You don't get an empty list. See this. However you won't get the desired results because in python int divided by int is always int. You may want to try this:

import math

factor_list = [ ]

def integer_test(x):

    if math.floor(x) == math.ceil(x):
        return True
    else:
        return False

def factor_checker(x):

    count = 0
    y = x
    while count <= y:
        print(count)
        count += 1
        if integer_test(1.0 * x / count) == True:
            factor_list.append(count)

factor_checker(45)

print(factor_list)

In this code x/count is a double. So the integer_test will only return true if it's ceiling and floor return the same number i.e the integer itself.

$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.