! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
-USING: kernel math math.ranges project-euler.common sequences sets sorting assocs fry ;
+USING: fry kernel math math.ranges project-euler.common
+sequences sets ;
IN: project-euler.023
! http://projecteuler.net/index.php?section=problems&id=23
[1,b] [ abundant? ] filter ;
: possible-sums ( seq -- seq )
- H{ } clone
- [ dupd '[ _ [ + _ conjoin ] with each ] each ]
- keep keys ;
+ HS{ } clone
+ [ dupd '[ _ [ + _ adjoin ] with each ] each ]
+ keep members ;
PRIVATE>
! for each prime number, count the families it belongs to. When one reaches count of 8, stop, and get the smallest number by replacing * with ones.
-USING: assocs kernel math math.combinatorics math.functions
-math.parser math.primes namespaces project-euler.common
-sequences sets strings grouping math.ranges arrays fry math.order ;
+USING: assocs fry kernel math math.combinatorics math.functions
+math.order math.parser math.primes math.ranges namespaces
+project-euler.common sequences sets ;
IN: project-euler.051
<PRIVATE
SYMBOL: family-count
SYMBOL: large-families
: reset-globals ( -- )
H{ } clone family-count namespaces:set
- H{ } clone large-families namespaces:set ;
+ HS{ } clone large-families namespaces:set ;
: digits-positions ( str -- positions )
H{ } clone [ '[ swap _ push-at ] each-index ] keep ;
[ all-positions-combinations [ replace-positions-with-* ] with map ] with map concat ;
: save-family ( family -- )
- dup family-count get at 8 = [ large-families get conjoin ] [ drop ] if ;
+ dup family-count get at 8 = [ large-families get adjoin ] [ drop ] if ;
: increment-family ( family -- )
family-count get inc-at ;
: handle-family ( family -- )
reset-globals
n-digits-primes
[ number>string families [ handle-family ] each ] each
- large-families get ;
+ large-families get members ;
: fill-*-with-ones ( str -- str )
[ dup CHAR: * = [ drop CHAR: 1 ] when ] map ;
! recursively test all primes by length until we find an answer
: (euler051) ( i -- answer )
- dup test-n-digits-primes
- dup assoc-size 0 >
- [ nip values [ fill-*-with-ones string>number ] [ min ] map-reduce ]
- [ drop 1 + (euler051) ] if ;
+ dup test-n-digits-primes [
+ 1 + (euler051)
+ ] [
+ nip [ fill-*-with-ones string>number ] [ min ] map-reduce
+ ] if-empty ;
+
PRIVATE>
: euler051 ( -- answer )
! Copyright (c) 2009 Guillaume Nargeot.
! See http://factorcode.org/license.txt for BSD license.
-USING: assocs hashtables kernel math math.ranges
-project-euler.common sequences sets ;
+USING: hash-sets kernel math.ranges project-euler.common
+sequences sets ;
IN: project-euler.074
! http://projecteuler.net/index.php?section=problems&id=074
number>digits [ digit-factorial ] map-sum ;
: chain-length ( n -- n )
- 61 <hashtable>
- [ 2dup key? not ]
- [ [ conjoin ] [ [ digits-factorial-sum ] dip ] 2bi ]
- while nip assoc-size ;
+ 61 <hash-set> [ 2dup ?adjoin ] [
+ [ digits-factorial-sum ] dip
+ ] while nip cardinality ;
PRIVATE>
! Copyright (c) 2005 Mackenzie Straight.
! See http://factorcode.org/license.txt for BSD license.
-USING: assocs grouping kernel math random sequences sets
+USING: assocs fry grouping kernel math random sequences sets
tools.test trees.splay ;
IN: trees.splay.tests
100 iota [ drop 100 random of drop ] with each ;
: make-numeric-splay-tree ( n -- splay-tree )
- iota <splay> [ [ conjoin ] curry each ] keep ;
+ iota <splay> [ '[ dup _ set-at ] each ] keep ;
{ t } [
100 make-numeric-splay-tree dup randomize-numeric-splay-tree