project_euler.problem_046.sol1

Problem 46: https://projecteuler.net/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 x 12 15 = 7 + 2 x 22 21 = 3 + 2 x 32 25 = 7 + 2 x 32 27 = 19 + 2 x 22 33 = 31 + 2 x 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?

Attributes

odd_composites

Functions

compute_nums(→ list[int])

Returns a list of first n odd composite numbers which do

is_prime(→ bool)

Checks to see if a number is a prime in O(sqrt(n)).

solution(→ int)

Return the solution to the problem

Module Contents

project_euler.problem_046.sol1.compute_nums(n: int) list[int]

Returns a list of first n odd composite numbers which do not follow the conjecture. >>> compute_nums(1) [5777] >>> compute_nums(2) [5777, 5993] >>> compute_nums(0) Traceback (most recent call last):

ValueError: n must be >= 0 >>> compute_nums(“a”) Traceback (most recent call last):

ValueError: n must be an integer >>> compute_nums(1.1) Traceback (most recent call last):

ValueError: n must be an integer

project_euler.problem_046.sol1.is_prime(number: int) bool

Checks to see if a number is a prime in O(sqrt(n)).

A number is prime if it has exactly two factors: 1 and itself.

>>> is_prime(0)
False
>>> is_prime(1)
False
>>> is_prime(2)
True
>>> is_prime(3)
True
>>> is_prime(27)
False
>>> is_prime(87)
False
>>> is_prime(563)
True
>>> is_prime(2999)
True
>>> is_prime(67483)
False
project_euler.problem_046.sol1.solution() int

Return the solution to the problem

project_euler.problem_046.sol1.odd_composites