PyPE solution to PE problem 46
PyPE solution to PE problem 46
Blueprint information
- Status:
- Complete
- Approver:
- None
- Priority:
- Undefined
- Drafter:
- None
- Direction:
- Needs approval
- Assignee:
- None
- Definition:
- Approved
- Series goal:
- None
- Implementation:
- Implemented
- Milestone target:
- None
- Started by
- Scott Armitage
- Completed by
- Scott Armitage
Related branches
Related bugs
Sprints
Whiteboard
ProjectEule
===
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*1^(2)
15 = 7 + 2*2^(2)
21 = 3 + 2*3^(2)
25 = 7 + 2*3^(2)
27 = 19 + 2*2^(2)
33 = 31 + 2*1^(2)
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?
Solution
--------
Assumptions: a reasonable starting guess is known; in this case, the limit
guess is 10,000. If the desired value is higher than this limit, then no
solution will be returned, and the limit must be increased.
First we generate a list of all odd numbers between 3 and the limit (we
exclude 1 because it is not a composite number anyway, and so not of
interest). Next we generate a list of all primes up to and including the
limit. The valid composite numbers of interest are those numbers that
appear in odd_numbers but not in primes.
If the conjecture holds, we have that c = p + 2*n*n. So, for each composite
nubmer of interest, and for each n between 1 and sqrt(limit), we have:
p = c - 2*n*n.
If none of the resulting values of p (one for each value of n) are in the
list of prime numbers (i.e. if none of these values of p are prime), then
we have found a composite number that cannot be written in this form.
NOTE: Having solved the problem by progressively increasing the limit number
until a solution was found, we can use this knowledge to set a reasonable
limit value to reduce actual computation time (i.e. not computing prime
numbers greater than we need to).
Answer
------
5777