Clean up Projece Euler solutions

db4
Aaron Schaefer 2008-11-03 16:20:36 -05:00
parent 852c16a79b
commit 92e2ee3509
7 changed files with 16 additions and 17 deletions

View File

@ -1,7 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
USING: ascii io.files kernel math math.functions namespaces make
project-euler.common sequences splitting io.encodings.ascii ;
USING: ascii io.encodings.ascii io.files kernel make math math.functions
namespaces project-euler.common sequences splitting ;
IN: project-euler.042
! http://projecteuler.net/index.php?section=problems&id=42

View File

@ -1,8 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit hashtables kernel math
math.combinatorics math.parser math.ranges project-euler.common sequences
sorting sets ;
USING: combinators.short-circuit kernel math math.combinatorics math.parser
math.ranges project-euler.common sequences sets sorting ;
IN: project-euler.043
! http://projecteuler.net/index.php?section=problems&id=43
@ -74,13 +73,13 @@ PRIVATE>
1000 over <range> [ number>digits 3 0 pad-left ] map [ all-unique? ] filter ;
: overlap? ( seq -- ? )
dup first 2 tail* swap second 2 head = ;
[ first 2 tail* ] [ second 2 head ] bi = ;
: clean ( seq -- seq )
[ unclip 1 head prefix concat ] map [ all-unique? ] filter ;
: add-missing-digit ( seq -- seq )
dup natural-sort 10 swap diff first prefix ;
dup natural-sort 10 swap diff prepend ;
: interesting-pandigitals ( -- seq )
17 candidates { 13 11 7 5 3 2 } [
@ -93,6 +92,6 @@ PRIVATE>
interesting-pandigitals [ 10 digits>integer ] sigma ;
! [ euler043a ] 100 ave-time
! 19 ms run / 1 ms GC ave time - 100 trials
! 12 ms ave run time - 2.32 SD (100 trials)
MAIN: euler043a

View File

@ -31,7 +31,7 @@ IN: project-euler.044
dup 3 * 1- * 2 / ;
: sum-and-diff? ( m n -- ? )
2dup + -rot - [ pentagonal? ] bi@ and ;
[ + ] [ - ] 2bi [ pentagonal? ] bi@ and ;
PRIVATE>
@ -40,7 +40,7 @@ PRIVATE>
[ first2 sum-and-diff? ] filter [ first2 - abs ] map infimum ;
! [ euler044 ] 10 ave-time
! 8924 ms run / 2872 ms GC ave time - 10 trials
! 5727 ms ave run time - 1102.38 SD (10 trials)
! TODO: this solution is ugly and not very efficient...find a better algorithm

View File

@ -1,6 +1,6 @@
! Copyright (c) 2008 Eric Mertens.
! See http://factorcode.org/license.txt for BSD license.
USING: arrays assocs combinators kernel locals math math.order math.ranges
USING: arrays assocs kernel locals.backend math math.order math.ranges
sequences ;
IN: project-euler.076
@ -55,6 +55,6 @@ PRIVATE>
100 (euler076) ;
! [ euler076 ] 100 ave-time
! 704 ms run time - 100 trials
! 560 ms ave run time - 17.74 SD (100 trials)
MAIN: euler076

View File

@ -1,7 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license.
USING: assocs hashtables io.files kernel math math.parser
namespaces make io.encodings.ascii sequences sets ;
USING: assocs io.encodings.ascii io.files kernel make math math.parser
sequences sets ;
IN: project-euler.079
! http://projecteuler.net/index.php?section=problems&id=79
@ -58,7 +58,7 @@ PRIVATE>
source-079 >edges topological-sort 10 digits>integer ;
! [ euler079 ] 100 ave-time
! 2 ms run / 0 ms GC ave time - 100 trials
! 1 ms ave run time - 1.41 SD (100 trials)
! TODO: prune and diff are relatively slow; topological sort could be
! cleaned up and generalized much better, but it works for this problem

View File

@ -32,7 +32,7 @@ IN: project-euler.092
0 swap [ dup zero? not ] [ 10 /mod sq -rot [ + ] dip ] [ ] while drop ;
: chain-ending ( n -- m )
dup 1 = over 89 = or [ next-link chain-ending ] unless ;
dup [ 1 = ] [ 89 = ] bi or [ next-link chain-ending ] unless ;
: lower-endings ( -- seq )
567 [1,b] [ chain-ending ] map ;

View File

@ -2,7 +2,7 @@
! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel make math math.functions math.matrices math.miller-rabin
math.order math.parser math.primes.factors math.ranges sequences
sequences.lib sorting unicode.case ;
sequences.lib sorting strings unicode.case ;
IN: project-euler.common
! A collection of words used by more than one Project Euler solution