Skip to content

Commit

Permalink
update projecteuler solutions for syntax change
Browse files Browse the repository at this point in the history
  • Loading branch information
cassowarii committed Jan 14, 2020
1 parent 7939c7d commit ca9e305
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 34 deletions.
6 changes: 3 additions & 3 deletions projecteuler/pe1.alma
@@ -1,9 +1,9 @@
# Given a list of integers, check whether a given integer is a multiple of any of them.
func multiple-of-any: 'multiple curry-all satisfies-any ;
def multiple-of-any ( 'multiple curry-all satisfies-any )

# Takes two parameters: an integer and a list of integers.
# Returns the sum of all multiples of those integers below the
# first integer.
func factors sum-multiples: range [factors multiple-of-any] filter sum ;
def factors sum-multiples ( range [factors multiple-of-any] filter sum )

func main: 1000 {3,5} sum-multiples say ;
def main ( 1000 {3,5} sum-multiples say )
12 changes: 6 additions & 6 deletions projecteuler/pe2.alma
@@ -1,16 +1,16 @@
# Take the two last items off a list and leave them on the stack on top of it: {a b c} -> {a} b c
func 2unappend: unappend 'unappend dip ;
def 2unappend ( unappend 'unappend dip )

# Append two items on top of the stack to a list underneath them: {a} b c -> {a b c}
func 2append: 'append dip append ;
def 2append ( 'append dip append )

# Same but for 3
func 3append: '2append dip append ;
def 3append ( '2append dip append )

# Given a list of fibonacci numbers, add the next one to the list.
func next-fib: 2unappend 2dup + 3append ;
def next-fib ( 2unappend 2dup + 3append )

# Construct a list of all fibonacci numbers below a certain value N.
func N fibs-less-than: {1,1} | list-iter-until: [last N ≥] [next-fib] ;
def N fibs-less-than ( {1,1} | list-iter-until: [last N ≥] [next-fib] )

func main: 4000000 fibs-less-than [2 multiple] filter sum say ;
def main ( 4000000 fibs-less-than [2 multiple] filter sum say )
18 changes: 9 additions & 9 deletions projecteuler/pe3.alma
@@ -1,27 +1,27 @@
# Find the integer square root but we don't have a sqrt function yet
func n crappy-sqrt: 1 | while*: [dup * n <] [incr] ;
def n crappy-sqrt ( 1 | while*: [dup * n <] [incr] )

# a b divides -> does a divide b?
func divides: swap multiple ;
def divides ( swap multiple )

# Find the maximum (positive) integer in a list.
func max-of-list: 0 [max] fold ; # should really be -Inf [max] fold
def max-of-list ( 0 [max] fold ) # should really be -Inf [max] fold

# Given a list of integers, check whether a given integer is a multiple of any of them.
func multiple-of-any: 'multiple curry-all satisfies-any ;
def multiple-of-any ( 'multiple curry-all satisfies-any )

# Find the possible prime factors of a number n (2..sqrt n).
func n prime-candidates: range-ends: 2 n crappy-sqrt ;
def n prime-candidates ( range-ends: 2 n crappy-sqrt )

# Check whether a number is prime (i.e. it's not 1 and it's not a multiple of
# any of the 'prime candidates' below it.)
func prime?: if*: [1 =] [drop 0] [dup prime-candidates multiple-of-any not] ;
def prime? ( if*: [1 =] [drop 0] [dup prime-candidates multiple-of-any not] )

# Find the prime factors of a number n
func n prime-factors: n prime-candidates [n divides] filter 'prime? filter ;
def n prime-factors ( n prime-candidates [n divides] filter 'prime? filter )

# Find the largest prime factor of a number.
func largest-prime-factor: prime-factors max-of-list ;
def largest-prime-factor ( prime-factors max-of-list )

func main: 600851475143 largest-prime-factor say ;
def main ( 600851475143 largest-prime-factor say )

8 changes: 4 additions & 4 deletions projecteuler/pe5.alma
Expand Up @@ -4,14 +4,14 @@
# or in postfix:
# a 0 gcd -> a
# a b gcd -> b a b mod gcd
func gcd: if*: [0 =] [drop] [swap over mod gcd] ;
def gcd ( if*: [0 =] [drop] [swap over mod gcd] )

# The least common multiple is the product of
# two numbers divided by their GCD.
func lcm: '* 'gcd '/ 2fork ;
def lcm ( '* 'gcd '/ 2fork )

# Find the LCM of all integers ≤ a given value.
func lcm-up-to: iota 1 [lcm] fold ;
def lcm-up-to ( iota 1 [lcm] fold )

func main: 20 lcm-up-to say ;
def main ( 20 lcm-up-to say )

8 changes: 4 additions & 4 deletions projecteuler/pe6.alma
@@ -1,11 +1,11 @@
# Find the sum of the squares of a list.
func sum-squares: [dup *] map sum ;
def sum-squares ( [dup *] map sum )

# Find the square of the sum of a list.
func square-sum: sum dup * ;
def square-sum ( sum dup * )

# Find the difference between the two.
func square-diff: 'square-sum 'sum-squares '- fork ;
def square-diff ( 'square-sum 'sum-squares '- fork )

func main: 100 iota square-diff say ;
def main ( 100 iota square-diff say )

17 changes: 9 additions & 8 deletions projecteuler/pe7.alma
@@ -1,24 +1,25 @@
# (Prime stuff copied from PE3)

# Find the integer square root but we don't have a sqrt function yet
func n crappy-sqrt: 1 | while*: [dup * n <] [incr] ;
def n crappy-sqrt ( 1 | while*: [dup * n <] [incr] )

# Given a list of integers, check whether a given integer is a multiple of any of them.
func multiple-of-any: 'multiple curry-all satisfies-any ;
def multiple-of-any ( 'multiple curry-all satisfies-any )

# Find the possible prime factors of a number n (2..sqrt n+1).
# (Hack: we want to return {2} for 4, but {} for 2 (so it doesn't
# divide 2 by itself). So we just check for 2 explicitly.
func n factor-candidates: if: [n 2 >] [2 n crappy-sqrt 1 + range-ends] [{}] ;
def n factor-candidates ( if: [n 2 >] [2 n crappy-sqrt 1 + range-ends] [{}] )

# Check whether a number is prime (i.e. it's not 1 and it's not a multiple of
# any of the 'prime candidates' below it.)
func prime?: if*: [1 =] [drop 0] [dup factor-candidates multiple-of-any not] ;
def prime? ( if*: [1 =] [drop 0] [dup factor-candidates multiple-of-any not] )

# Find the nth prime by iterating over all odd numbers and counting the prime ones.
# There's probably a faster way to do this...
func n nth-prime: 1 1 | while: [over n <] [2 + | when*: [prime?] ['incr dip]] | nip ;
def n nth-prime ( 1 1 | while: [over n <] [2 + | when*: [prime?] ['incr dip]] | nip )

func main: "This’ll take some doing. Hold tight." say
10001 nth-prime say
;
def main (
"This’ll take some doing. Hold tight." say
10001 nth-prime say
)

0 comments on commit ca9e305

Please sign in to comment.