Need a explaination of why my computer is struggling to do math

i have written a small program to factor numbers for me, i absolutely do not want to use other libraries, i want to figure it all out and learn everything i can, and not using math related libraries or any other libraries is a good way to do that.

the problem is, well first i should say this program does work…but only with numbers that are 8 digits or less.. and thats what the problem is, why does this program only work with numbers that are 8 digits or less? do i need to use threading? do i need to break it down more, into two different for loops, i would put each for loop in their own function then thread them both, would that work? (i would split the variable “halfnumber” in half and those quarters of “usernumber” would be in their own functions) or what else could i do?

and please if you have the knowledge, is there something i need to know about how cpu’s do math and their limits? could you please let me know. i am not a expert (yet) but i want to learn everything i can, please let me know.

here is the code below,
“”"
print(“enter in a number you want to factor”)
usernumber = int(input("number: “))
halfnumber = int(usernumber / 2)
temp = int
factors = list()
for z in range(1, halfnumber):
temp = (usernumber / z)
temp2 = (temp % 1)
if temp2 == 0:
factors.append(temp)
print(“well the math is done”)
print(factors)
print(“yeah buddy”)
“””

In order to preserve formatting, please select any code or traceback that you post and then click the </> button.

The / operator performs floating-point division, and floating point has a limited precision. It’s equivalent to double in the C language.

The // operator, on the other hand, performs “floor” division, and, more importantly, performs integer division on integers.

Integers (int) in Python can have an unlimited number of digits.

1 Like

What do you mean by doesn’t work for >= 9 digit numbers? Does it actually crash, or does it just take a very long time, and maybe go OOM?

It looks like a simple brute force attack, repeatedly trying to divide N by 1, …, |N/2|.

A little domain knowledge would tell you that factoring products of large primes in particular, is known to be a computationally demanding problem.

A little math would tell you, that once you’ve found one factor m s.t. N = m * q + r (and them all being integers with r < m requires r = 0), then you don’t need to keep on dividing the whole of N, you can just look for the remaining factors in its corresponding quotient q.

2 Likes

This is the reason, you should keep your data in the int type all along the calculation. (tips : You will need to modify that % 1, but you need a modulo, no division necessary.)

1 Like

I did a simple test where I entered a relatively small number of 100. The result I obtained was:

[100.0, 50.0, 25.0, 20.0, 10.0, 5.0, 4.0]

As you can see, it is missing 2.0.

When I tested it with a value of 50, I got:

[50.0, 25.0, 10.0, 5.0]

… also missing the 2.0.

One small note, the results are floating point values. I believe that you want them as integers (type int).

You might want to give @JamesParrott’s suggestion a try as from my understanding it may reduce the calculations substantially.

1 Like

hey thanks everyone i just read this right now i will give it all a try, and also try to get the number 2 included in the list of factors, thanks everyone, i really appreciate it