Factorization: What went wrong with d?

Question:

Consider:

Enter image description here

Input: 20
       17
       999997

Output: 2^2 * 5
        17
        757 * 1321

My code:

a = int(input())

# Find the factors first
for i in range(2, a+1):

    s = 0
    b = a
    d = 0

    # See if it is a prime number
    if a%i == 0:
        for x in range(1, i+1):
            if a%x == 0:
                d = d + x

        if (d-1)/i == 1:
            d = 0
            print(i)
        else:
            s = 0
            b = a
            d = 0

            continue

            d = 0

        # I will see how many prime numbers
        while(b>0):

            if (b/i)%1 == 0:
                s = s + 1
                b = b/i
            else:
                b = 0
                if b == 1:
                   b = 0

        print(s)

I will find the factors first, and then see if it is a prime number. If so, I will see how many prime numbers it is

if i input 12, it outputs 2 2

Enter link description here

Asked By: 蔡銘恩

||

Answers:

a = int(input("Enter a number:"))

for i in range(2, a + 1):
    if a % i != 0:
        continue

    # SETTING THE DEFAULT VALUES AT THE BEGINNING OF EVERY ITERATION OF THE LOOP
    s = 0
    b = a
    d = 0

    for x in range(1, i + 1):
        if b % x == 0:
            d = d + x

    if (d - 1) / i == 1:
        d = 0
        print(i)
    else:
        # s = 0 # NO LONGER NEEDED, AS WE RESET THEM AT THE BEGINNING OF THE LOOP
        # b = a
        # d = 0
        continue

    while b > 0:
        if (b / i) % 1 == 0:
            s = s + 1
            b = b / i
        else:
            b = 0
            if b == 1:
                b = 0

    print(s)
    a /= i**s # THIS LINE IS IMPORTANT

You were close. You forgot to set the default values at the beginning of every iteration of the loop, so they sometimes didn’t have the right values ; and you should set a to a different value by dividing it by the factor you found (i**s, so i to the power of s).

As has been mentioned, your code also follows an odd coding style. I suggest you stop putting newlines between each statement, and start separating operators with spaces (example: range(3+5) is bad, range(3 + 5) is more readable)

Answered By: OctaveL

I believe you need the output of the following.

import math

a = int(input())
while (a % 2 == 0):
    print(2)
    a = int(a/2)
while (a % 3 == 0):
    print(3)
    a = int(a/3)
for i in range(5, math.ceil(math.sqrt(a)), 6):
    while (a % i == 0):
        print(i)
        a = int(a / i)
    while (a % (i + 2) == 0):
        print(i + 2)
        a = int(a / (i + 2))
if (a > 3):
    print(a)

This will give you the prime factors for a given number. As I can understand, it is what you are looking for.

Answered By: Chamal Perera

You are using too many loops here and that’s why you are getting too much confused. Here is the code which serve the same purpose (if I understand your problem correctly)

a = int(input("Enter a number: "))
i = 2
factors = []
while i <= a:
    if (a%i) == 0:
        factors.append(i)
        a = a/i
    else:
        i = i + 1
print(factors)

here I am returning a list, if you want you can change the type accordingly.

Here are the inputs/outputs:

Enter a number: 17
[17]
Enter a number: 100
[2, 2, 5, 5]
Enter a number: 12
[2, 2, 3]
Answered By: Ranjeet Singh
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.