Skip to content

Instantly share code, notes, and snippets.

@mkhan45
Last active June 13, 2021 18:46
Show Gist options
  • Select an option

  • Save mkhan45/00d4c4c12b82a514bb14a0080198e6f1 to your computer and use it in GitHub Desktop.

Select an option

Save mkhan45/00d4c4c12b82a514bb14a0080198e6f1 to your computer and use it in GitHub Desktop.
APL Euler
⍝ Problem 1
{+/(∨⌿0=(3 5)∘.|⍵)/⍵}⍳999
⍝ ⍳999 is [1..999]
⍝ {...} is a function which is applied to [1..999]
⍝ a/b where a and b are vectors of the same length replicates
⍝ each index of b by the equivalent index of a. Here it acts as a filter since each value in a is either 0 or 1
⍝ (3 5)∘.|⍵ is the dot product of (3 5) with w using | (residue) as the operator. | is like backwards remainder in this case
⍝ 0= compares the remainder matrix with 0
⍝ ∨⌿ does boolean OR on the rows
⍝ +/ sums the array
⍝ Problem 2
evenfib←{⍺←0 2 ⋄ ⍵=0: ⍺[1] ⋄ (⍺[2] (⍺[1]+⍺[2]×4))∇(⍵-1)}
{⍺←0 ⋄ x←evenfib ⍵ ⋄ x>4000000:⍺ ⋄ (⍺+x )∇(⍵+1)} 0
⍝ Problem 3
isprime←{(⍵∊2 3 5 7) ∨ ~{l←⌈⍵*0.5 ⋄ ls←2,1+2×⍳⌈l÷2 ⋄ ∨/0=ls|⍵}⍵}
fac←{l←⌈⍵*0.5 ⋄ ls←1↓⍳l ⋄ ¯1↑((0=ls|⍵)∧(isprime¨ls))/ls}
fac 600851475143
⍝ Problem 5
∧/⍳20
⍝ Problem 6
{((+/⍵)*2) - (+/⍵*2)}⍳100
⍝ Problem 7
{⍺←1 ⋄ p←isprime ⍺ ⋄ (⍵=10000)∧p:⍺ ⋄ (⍺+2)∇(⍵+p)}0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment