Automatic commit performed through alias...
This commit is contained in:
69
problems/003_problem/003_problem.py
Executable file
69
problems/003_problem/003_problem.py
Executable file
@@ -0,0 +1,69 @@
|
||||
#!/usr/bin/env python
|
||||
|
||||
# Problem 3:
|
||||
#
|
||||
# The prime factors of 13195
|
||||
# are 5, 7, 13 and 29.
|
||||
#
|
||||
# What is the largest prime
|
||||
# factor of the number 600851475143 ?
|
||||
#
|
||||
|
||||
import decorators
|
||||
import time
|
||||
|
||||
# Create function that finds the next
|
||||
# prime number when supplied with an
|
||||
# intitial integer.
|
||||
|
||||
def primes_gen(start_n,max_n):
|
||||
"""
|
||||
Returns a generator object, containing the
|
||||
primes inside a specified range.
|
||||
|
||||
primes_gen(start_n,max_n)
|
||||
|
||||
param 'start_n': Previous prime.
|
||||
param 'max_n': Maximum
|
||||
|
||||
"""
|
||||
|
||||
start_n += 1
|
||||
for candidate in range(start_n,max_n):
|
||||
notPrime = False
|
||||
|
||||
if candidate in [0,1,2,3]:
|
||||
yield candidate
|
||||
|
||||
for dividend in range(2,candidate):
|
||||
|
||||
if candidate%dividend == 0:
|
||||
notPrime = True
|
||||
|
||||
if not notPrime:
|
||||
yield candidate
|
||||
|
||||
@decorators.function_timer
|
||||
def main():
|
||||
|
||||
orig = 600851475143
|
||||
result1 = orig
|
||||
prime_start = 2
|
||||
returned_prime = prime_start
|
||||
prime_factor_list = []
|
||||
|
||||
while returned_prime < orig ** 0.5 and returned_prime<result1:
|
||||
|
||||
|
||||
returned_prime = primes_gen(prime_start,orig).__next__()
|
||||
|
||||
result2 = result1/returned_prime
|
||||
if result1%returned_prime == 0:
|
||||
#print(" {} / {} = {}".format(result1,returned_prime,result2))
|
||||
prime_factor_list.append(returned_prime)
|
||||
result1 = result2
|
||||
prime_start = returned_prime
|
||||
|
||||
print("This highest prime factor of {} is {} ... ".format(orig,max(prime_factor_list)))
|
||||
|
||||
main()
|
||||
Reference in New Issue
Block a user