-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdeloitte_01.py
59 lines (48 loc) · 2.09 KB
/
deloitte_01.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# Ross's script v2 for James's coding challenge.
# Run this from the command line by calling "python deloitte_primes.py".
# (At least on Ubuntu, not sure of the equivalent system in Windows.)
# (Try http://pythoncentral.io/execute-python-script-file-shell/ maybe.)
# Enter positive integers when prompted and press Enter.
import time
# Define a function that sums an integer's digits by adding the last digit,
# dividing by 10 (Python automatically rounds down), and repeating.
# This is written in Python 2; need to use // instead of / in Python 3.
def sum_digits(_n):
_total = 0
while (_n > 0):
_total += (_n%10)
_n /= 10
return _total
# User-chosen values. These are inputted at the command line when prompted.
how_many_primes = int(raw_input("\nHow many primes do you want to consider?\n")) # or just set this to 2016.
target_digits_sum = int(raw_input("\nWhat would you like the primes' digits to sum to?\n")) # or just set this to 13.
# Start the timer.
start = time.time()
# Construct a list of the first N primes. There's probably a faster way to do this,
# but this method ensures it stops after the first N primes have been found.
primes = [2]
test = 3
while (len(primes) < how_many_primes):
is_prime = True
ceiling = test**0.5
for p in primes:
if (test%p==0):
is_prime = False
break
elif (p>ceiling): # only need to try factor's up to test's square root, because factors pair up
break
if is_prime:
primes.append(test)
test += 2
prime_time = time.time() - start
# Go through the primes in the list, and check if their digits sum to the target.
count = 0
for p in primes:
if (sum_digits(p) == target_digits_sum):
count += 1
time_taken = time.time() - start
# This first bit really takes most of the time.
print "\nTime taken to construct set of first {} primes: {} s.".format(how_many_primes,prime_time)
# Print results and time taken.
print "\n{} of the first {} primes' digits sum to {}.\n".format(count,how_many_primes,target_digits_sum)
print "Total time taken: {} s.\n".format(time_taken)