Skip to content

Instantly share code, notes, and snippets.

@JohnEarnest
Created June 19, 2013 00:31
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnEarnest/5810773 to your computer and use it in GitHub Desktop.
Save JohnEarnest/5810773 to your computer and use it in GitHub Desktop.
Solving some Project Euler problems to become more familiar with factor. This is pretty old, but I thought it'd be worth stashing here for safekeeping.
USING:
kernel
sequences
sequences.deep
math
math.functions
math.order
math.ranges
math.primes
math.primes.factors
project-euler.common
fry
;
IN: euler
: prob3 ( n -- f ) factors last ;
: prob7 ( n -- nth ) nth-prime ;
: prob10 ( max -- sum ) primes-upto sum ;
: prob1 ( n -- n )
1 (a,b]
[
[ 3 mod 0 = ]
[ 5 mod 0 = ] bi or
] filter
sum
;
: fibs-upto ( max -- seq )
'[ dup first2 + _ <= ]
{ 2 1 } swap
[ dup first2 + prefix ] while
;
: prob2 ( max -- n )
fibs-upto [ even? ] filter sum
;
: prob4 ( digits -- max )
10 swap ^ dup 10 / (a,b]
dup [ * ] cartesian-map flatten
[ palindrome? ] filter
0 [ max ] reduce
;
: prob5 ( max -- n )
1 [a,b] 1 [ lcm ] reduce
;
: prob6 ( max -- n )
1 [a,b] dup
sum 2 ^ swap
[ 2 ^ ] map sum -
;
: prob12 ( max -- n )
1 1 rot '[ dup divisors length _ < ]
[ swap 1 + swap over + ] while nip
;
: prob16 ( pow -- sum )
2 swap ^ number>digits sum
;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment