Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ## Project Euler
- ## Problem 3
- ##
- ## The prime factors of 13195 are 5, 7, 13 and 29.
- ##
- ## What is the largest prime factor of the number 600851475143 ?
- #the number we want to find
- number = 600851475143
- listprimes = []
- result = [] #empty lists we will use later
- #function to check if number is prime
- def isprime(n):
- if n < 2:
- return False
- if n == 2:
- return True
- if not n & 1:
- return False
- for x in range(3, int(n**0.5)+1, 2):
- if n % x == 0:
- return False
- return True
- print('Generating list...')
- #generates a list of the first 10k prime numbers (arbitrary)
- for i in range(1, 10001):
- test = isprime(i)
- if test == True:
- listprimes.append(i)
- print('Added '+str(i)+' to the list.')
- print('Verifying divisors...')
- i = 0 #starts up counter
- try:
- while True:
- if number % listprimes[i] == 0: ## this test checks if the number is divisible by the prime
- result.append(listprimes[i]) ## if it is, the number is added to a list
- print('Divisible by '+str(listprimes[i])) ## and it tells you so
- else:
- print('Not divisible by '+str(listprimes[i])) #if it isn't, it also tells you.
- i += 1 #moves the counter one position ahead
- except IndexError:
- print('End of primes list')
- print(result)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement