1 ! Copyright (c) 2008 Aaron Schaefer.
2 ! See https://factorcode.org/license.txt for BSD license.
3 USING: kernel math math.functions math.primes ranges sequences
7 ! https://projecteuler.net/problem=46
12 ! It was proposed by Christian Goldbach that every odd composite
13 ! number can be written as the sum of a prime and twice a
23 ! It turns out that the conjecture was false.
25 ! What is the smallest odd composite that cannot be written as
26 ! the sum of a prime and twice a square?
34 : perfect-squares ( n -- seq )
35 2 /i sqrt >integer [1..b] [ sq ] map ;
37 : fits-conjecture? ( n -- ? )
38 dup perfect-squares [ 2 * - ] with map [ prime? ] any? ;
40 : next-odd-composite ( n -- m )
41 dup odd? [ 2 + ] [ 1 + ] if dup prime? [ next-odd-composite ] when ;
43 : disprove-conjecture ( n -- m )
44 dup fits-conjecture? [ next-odd-composite disprove-conjecture ] when ;
48 : euler046 ( -- answer )
49 9 disprove-conjecture ;
51 ! [ euler046 ] 100 ave-time
52 ! 37 ms ave run time - 3.39 SD (100 trials)