Files
project_euler/58/main.ua

35 lines
971 B
Plaintext
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
# project euler 58
p ← =1⧻°/×
Sieve ← ▽⊸≡p+2⇡
Primes ← Sieve 37417
# is_prime(n) ? n
# p ← ⨬(=|1)⊸=¯1⊸(⍣⊢¯1▽⤚(=0≡◿) Primes ¤)
NewP ← (
# get the next three prime candidates (lower right is odd square)
⊃((+5+∩×₄⊃×₂ⁿ₂)
| (+3+⊃×₆(×4ⁿ2))
| (+7+⊃×₁₀(×4ⁿ2))
)
++∩₃p # count how many are prime (0-3)
)
Sol ← (
# initial values are:
# - 5: the amount of numbers so far; 1, 3, 5, 7, 9 (counter for diagonal)
# - NewP.: = 3, since 3, 5, 7 are prime (counter for primes on diagonal)
# - 0: current NewP generation. NewP 0 is 3 (see above)
5 NewP. 0
⍢(+4 ⊙+ ⊙⊙(⊸NewP+1) # add 4 for new corners, generate NewP(n+1)
| >0.1 ÷ # check prime ratio
)
# discard number of primes and diagonal values,
# scale generation up to side length
# newp gen 0 -> 0*2 + 3 = side length 3
# newp gen 1 -> 1*2 + 3 = side length 5, ...
⋅⋅(+3×2)
)