from math import sqrt
def isPerfectSquare(n):
sr = int(sqrt(n))
return sr * sr == n
for N in range(1, 10000000000):
foo = 1 + 12*(N*N + N)
bar = 1 + 4*(N*N + N)
if isPerfectSquare(foo) and isPerfectSquare(bar):
x, y = int(sqrt(foo)), int(sqrt(bar))
if (1+x)%6 == 0 and (1+y)%4 == 0:
print(f'Candidate found: N={N}, T_N={N*(N+1)/2}') import Data.Ord
tri n = n*(n+1)`quot`2
pent n = n*(3*n-1)`quot`2
hex n = n*(2*n-1)
fs :: (Integer -> Integer)->[Integer]
fs f = map f [1..]
cm aas@(a:as) bbs@(b:bs)
= case compare a b of
EQ -> a : cm as bs
GT -> cm aas bs
LT -> cm as bbs
tt = (fs tri) `cm` (fs pent) `cm` (fs hex)
main = print . take 3 $ tt
Added: the one after that is 2172315626468283465 which took about 6 minutes with the same Haskell program. I'm sure there are faster implementations and better algorithms. I didn't try to search any further. $ time cargo run --release
Finished `release` profile [optimized] target(s) in 0.02s
Running `target/release/p45`
0
1
40755
1533776805
57722156241751
2172315626468283465
cargo run --release 2.95s user 0.04s system 98% cpu 3.029 total
Runs on the Rust Playground too: https://play.rust-lang.org/?version=stable&mode=release&edit...Rust newbie q - why use `x.wrapping_sub()` instead of regular old `x - 1`? Seems like we're never going to underflow `usize` for any of the 3 formulae?