Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
update projecteuler solutions for syntax change
- Loading branch information
1 parent
7939c7d
commit ca9e305
Showing
6 changed files
with
35 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 ) | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
) |