-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProblem46.py
More file actions
71 lines (50 loc) · 1.85 KB
/
Problem46.py
File metadata and controls
71 lines (50 loc) · 1.85 KB
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
60
61
62
63
64
65
66
67
68
69
70
71
# Code by @AmirMotefaker
# projecteuler.net
# https://projecteuler.net/problem=46
# Goldbach's other conjecture
# Problem 46
# It was proposed by Christian Goldbach that every odd composite number can be written as the sum of a prime and twice a square.
# 9 = 7 + 2×12
# 15 = 7 + 2×22
# 21 = 3 + 2×32
# 25 = 7 + 2×32
# 27 = 19 + 2×22
# 33 = 31 + 2×12
# It turns out that the conjecture was false.
# What is the smallest odd composite that cannot be written as the sum of a prime and twice a square?
# We will start with number 3. As we don't know the upper limit,
# we will use a while loop. Question states that the number on
# the left hand side is a composite odd number. Now let us consider
# the equation given in the question as follows:
# number = prime + 2 × some_number2
# ⇒ some_number = ((number-prime)/2)1/2
# In the above equation for some_number to be a positive integer
# there are two conditions, value of number should be greater than
# prime number or we can say - We can iterate with the prime numbers below number.
# Value of some_number((number-prime)/2) should be a perfect number.
import math, time
start_time = time.time() #Time at the start of program execution
def is_prime(n):
if n % 2 == 0:
return False
else:
for i in range(3, int(n**0.5+1),2):
if n % i == 0:
return False
return True
number = 3
primes = [2] # list of primes
while True:
if is_prime(number):
primes.append(number)
else:
for i in primes:
if math.sqrt(((number-i)/2)) == int(math.sqrt(((number-i)/2))):
break
else:
print (number)
break
number += 2
end_time = time.time() #Time at the end of execution
print ("Time of program execution:", (end_time - start_time)) # Time of program execution
### Answer: 5777