Tuesday, 10 May 2011

Python program for generating prime factors for the given number

hi,

I made a program to generate prime factors for the given number.

the code has a lot of bugs. this is just an idea.

the code is,...
# usr/bin/env python
def checkPrime(val):
    for num in range(2,val):
        if val % num == 0:
            return True
            break
    else:
        return False

inputNum = int(input('enter the number : '))
storeFac = []
while checkPrime(inputNum):
    for numb in range(2,inputNum):
        if inputNum % numb == 0:
            inputNum = inputNum / numb
            storeFac.append(numb)
            break

storeFac.append(inputNum)
print "the prime factors are : ",str(storeFac).strip('[]')




snap-explanation: checking the given number if prime - if prime print the number - else use while loop to check number is still prime - then for loop to to find the factors - then if to to check numb is the factor - if true make given number divided by numb - then append the factor in a list - then print the factors.

i/p:

  enter the number : 60

o/p:

the prime factors are :  2, 2, 3, 5

No comments:

Post a Comment