Update usages of divisor? where appropriate

db4
Aaron Schaefer 2009-04-01 21:53:18 -04:00
parent c698a83a12
commit ca9fb1fcf1
9 changed files with 25 additions and 27 deletions

View File

@ -1,8 +1,8 @@
! Copyright (C) 2007 Doug Coleman. ! Copyright (C) 2007 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays kernel math math.functions namespaces sequences USING: accessors arrays classes.tuple combinators combinators.short-circuit
strings system vocabs.loader threads accessors combinators kernel locals math math.functions math.order namespaces sequences strings
locals classes.tuple math.order summary combinators.short-circuit ; summary system threads vocabs.loader ;
IN: calendar IN: calendar
HOOK: gmt-offset os ( -- hours minutes seconds ) HOOK: gmt-offset os ( -- hours minutes seconds )
@ -136,7 +136,7 @@ CONSTANT: day-abbreviations3
GENERIC: leap-year? ( obj -- ? ) GENERIC: leap-year? ( obj -- ? )
M: integer leap-year? ( year -- ? ) M: integer leap-year? ( year -- ? )
dup 100 mod zero? 400 4 ? mod zero? ; dup 100 divisor? 400 4 ? divisor? ;
M: timestamp leap-year? ( timestamp -- ? ) M: timestamp leap-year? ( timestamp -- ? )
year>> leap-year? ; year>> leap-year? ;

View File

@ -271,9 +271,9 @@ HELP: gcd
{ $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ; { $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ;
HELP: divisor? HELP: divisor?
{ $values { "x" integer } { "y" integer } { "?" "a boolean" } } { $values { "m" integer } { "n" integer } { "?" "a boolean" } }
{ $description "Tests if " { $snippet "y" } " is a divisor of " { $snippet "x" } ". This is the same thing as saying " { $snippet "x" } " is divisible by " { $snippet "y" } "." } { $description "Tests if " { $snippet "n" } " is a divisor of " { $snippet "m" } ". This is the same thing as asking if " { $snippet "m" } " is divisible by " { $snippet "n" } "." }
{ $notes "Returns t for both negative and positive divisors, as well as trivial and non-trivial divisors." } ; { $notes "Returns t for both negative and positive divisors, as well as for trivial and non-trivial divisors." } ;
HELP: mod-inv HELP: mod-inv
{ $values { "x" integer } { "n" integer } { "y" integer } } { $values { "x" integer } { "n" integer } { "y" integer } }

View File

@ -1,6 +1,6 @@
! Copyright (C) 2007-2009 Samuel Tardieu. ! Copyright (C) 2007-2009 Samuel Tardieu.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: arrays combinators kernel make math math.primes sequences ; USING: arrays combinators kernel make math math.fucntions math.primes sequences ;
IN: math.primes.factors IN: math.primes.factors
<PRIVATE <PRIVATE
@ -11,7 +11,7 @@ IN: math.primes.factors
swap ; swap ;
: write-factor ( n d -- n' d' ) : write-factor ( n d -- n' d' )
2dup mod zero? [ 2dup divisor? [
[ [ count-factor ] keep swap 2array , ] keep [ [ count-factor ] keep swap 2array , ] keep
! If the remainder is a prime number, increase d so that ! If the remainder is a prime number, increase d so that
! the caller stops looking for factors. ! the caller stops looking for factors.

View File

@ -1,6 +1,6 @@
! Copyright (c) 2007, 2008 Aaron Schaefer, Slava Pestov. ! Copyright (c) 2007, 2008 Aaron Schaefer, Slava Pestov.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math math.ranges sequences project-euler.common ; USING: kernel math math.functions math.ranges sequences project-euler.common ;
IN: project-euler.001 IN: project-euler.001
! http://projecteuler.net/index.php?section=problems&id=1 ! http://projecteuler.net/index.php?section=problems&id=1
@ -53,7 +53,7 @@ PRIVATE>
: euler001c ( -- answer ) : euler001c ( -- answer )
1000 [ { 3 5 } [ mod 0 = ] with any? ] filter sum ; 1000 [ { 3 5 } [ divisor? ] with any? ] filter sum ;
! [ euler001c ] 100 ave-time ! [ euler001c ] 100 ave-time
! 0 ms ave run time - 0.06 SD (100 trials) ! 0 ms ave run time - 0.06 SD (100 trials)

View File

@ -1,7 +1,7 @@
! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg. ! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: hashtables kernel math math.ranges project-euler.common sequences USING: hashtables kernel math math.functions math.ranges project-euler.common
sorting sets ; sequences sorting sets ;
IN: project-euler.004 IN: project-euler.004
! http://projecteuler.net/index.php?section=problems&id=4 ! http://projecteuler.net/index.php?section=problems&id=4
@ -21,7 +21,7 @@ IN: project-euler.004
<PRIVATE <PRIVATE
: source-004 ( -- seq ) : source-004 ( -- seq )
100 999 [a,b] [ 10 mod 0 = not ] filter ; 100 999 [a,b] [ 10 divisor? not ] filter ;
: max-palindrome ( seq -- palindrome ) : max-palindrome ( seq -- palindrome )
natural-sort [ palindrome? ] find-last nip ; natural-sort [ palindrome? ] find-last nip ;

View File

@ -1,6 +1,6 @@
! Copyright (c) 2007 Aaron Schaefer. ! Copyright (c) 2007 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel make math math.ranges USING: combinators.short-circuit kernel make math math.functions math.ranges
sequences project-euler.common ; sequences project-euler.common ;
IN: project-euler.014 IN: project-euler.014
@ -59,7 +59,7 @@ PRIVATE>
<PRIVATE <PRIVATE
: worth-calculating? ( n -- ? ) : worth-calculating? ( n -- ? )
1- 3 { [ mod 0 = ] [ / even? ] } 2&& ; 1- 3 { [ divisor? ] [ / even? ] } 2&& ;
PRIVATE> PRIVATE>

View File

@ -1,7 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer. ! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel math math.combinatorics math.parser USING: combinators.short-circuit kernel math math.functions math.combinatorics
math.ranges project-euler.common sequences sets sorting ; math.parser math.ranges project-euler.common sequences sets sorting ;
IN: project-euler.043 IN: project-euler.043
! http://projecteuler.net/index.php?section=problems&id=43 ! http://projecteuler.net/index.php?section=problems&id=43
@ -36,7 +36,7 @@ IN: project-euler.043
<PRIVATE <PRIVATE
: subseq-divisible? ( n index seq -- ? ) : subseq-divisible? ( n index seq -- ? )
[ 1- dup 3 + ] dip subseq 10 digits>integer swap mod zero? ; [ 1- dup 3 + ] dip subseq 10 digits>integer swap divisor? ;
: interesting? ( seq -- ? ) : interesting? ( seq -- ? )
{ {

View File

@ -1,8 +1,7 @@
! Copyright (c) 2008 Aaron Schaefer. ! Copyright (c) 2008 Aaron Schaefer.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: combinators.short-circuit kernel math USING: combinators.short-circuit kernel math math.functions
project-euler.common sequences sorting project-euler.common sequences sorting grouping ;
grouping ;
IN: project-euler.052 IN: project-euler.052
! http://projecteuler.net/index.php?section=problems&id=52 ! http://projecteuler.net/index.php?section=problems&id=52
@ -31,7 +30,7 @@ IN: project-euler.052
[ number>digits natural-sort ] map all-equal? ; [ number>digits natural-sort ] map all-equal? ;
: candidate? ( n -- ? ) : candidate? ( n -- ? )
{ [ odd? ] [ 3 mod 0 = ] } 1&& ; { [ odd? ] [ 3 divisor? ] } 1&& ;
: next-all-same ( x n -- n ) : next-all-same ( x n -- n )
dup candidate? [ dup candidate? [

View File

@ -44,7 +44,7 @@ IN: project-euler.common
: (sum-divisors) ( n -- sum ) : (sum-divisors) ( n -- sum )
dup sqrt >integer [1,b] [ dup sqrt >integer [1,b] [
[ 2dup mod 0 = [ 2dup / + , ] [ drop ] if ] each [ 2dup divisor? [ 2dup / + , ] [ drop ] if ] each
dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if dup perfect-square? [ sqrt >fixnum neg , ] [ drop ] if
] { } make sum ; ] { } make sum ;
@ -120,7 +120,7 @@ PRIVATE>
factor-2s dup [ 1+ ] factor-2s dup [ 1+ ]
[ perfect-square? -1 0 ? ] [ perfect-square? -1 0 ? ]
[ dup sqrt >fixnum [1,b] ] tri* [ [ dup sqrt >fixnum [1,b] ] tri* [
dupd mod 0 = [ [ 2 + ] dip ] when dupd divisor? [ [ 2 + ] dip ] when
] each drop * ; ] each drop * ;
! These transforms are for generating primitive Pythagorean triples ! These transforms are for generating primitive Pythagorean triples
@ -137,4 +137,3 @@ SYNTAX: SOLUTION:
[ drop in get vocab (>>main) ] [ drop in get vocab (>>main) ]
[ [ . ] swap prefix (( -- )) define-declared ] [ [ . ] swap prefix (( -- )) define-declared ]
2bi ; 2bi ;