#!/usr/bin/env setl
--
-- Project Euler problem 46 in SETL
--
-- """
-- 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?
-- """
--
-- This SETL program was created by Hakan Kjellerstrand (hakank@bonetmail.com)
-- Also see my SETL page: http://www.hakank.org/setl/
--
problem46();
proc is_prime(n);
if n = 2 or n = 3 then
return(true);
end if;
if n mod 2 = 0 then
return(false);
end if;
for i in [3,5..round(sqrt(n))+1] loop
if n mod i = 0 then
return(false);
end if;
end loop;
return(true);
end proc;
proc problem46;
for i in [3,5..10000] loop
if is_prime(i) then continue; end if;
s := fix(sqrt(i/2));
found := false;
for j in [1..s] loop
ts := j*j*2;
if is_prime(i-ts) then
found := true;
continue;
end if;
end for;
if not(found) then
quit;
end if;
end loop;
print(i);
end proc;