From ca9fb1fcf1e68a1536bc0280f8a6f55281a84197 Mon Sep 17 00:00:00 2001 From: Aaron Schaefer Date: Wed, 1 Apr 2009 21:53:18 -0400 Subject: [PATCH] Update usages of divisor? where appropriate --- basis/calendar/calendar.factor | 8 ++++---- basis/math/functions/functions-docs.factor | 6 +++--- basis/math/primes/factors/factors.factor | 4 ++-- extra/project-euler/001/001.factor | 4 ++-- extra/project-euler/004/004.factor | 6 +++--- extra/project-euler/014/014.factor | 6 +++--- extra/project-euler/043/043.factor | 6 +++--- extra/project-euler/052/052.factor | 7 +++---- extra/project-euler/common/common.factor | 5 ++--- 9 files changed, 25 insertions(+), 27 deletions(-) diff --git a/basis/calendar/calendar.factor b/basis/calendar/calendar.factor index 104941ddb2..54741567bb 100644 --- a/basis/calendar/calendar.factor +++ b/basis/calendar/calendar.factor @@ -1,8 +1,8 @@ ! Copyright (C) 2007 Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: arrays kernel math math.functions namespaces sequences -strings system vocabs.loader threads accessors combinators -locals classes.tuple math.order summary combinators.short-circuit ; +USING: accessors arrays classes.tuple combinators combinators.short-circuit + kernel locals math math.functions math.order namespaces sequences strings + summary system threads vocabs.loader ; IN: calendar HOOK: gmt-offset os ( -- hours minutes seconds ) @@ -136,7 +136,7 @@ CONSTANT: day-abbreviations3 GENERIC: leap-year? ( obj -- ? ) M: integer leap-year? ( year -- ? ) - dup 100 mod zero? 400 4 ? mod zero? ; + dup 100 divisor? 400 4 ? divisor? ; M: timestamp leap-year? ( timestamp -- ? ) year>> leap-year? ; diff --git a/basis/math/functions/functions-docs.factor b/basis/math/functions/functions-docs.factor index 02fcd5f4d9..f7d0d5a941 100644 --- a/basis/math/functions/functions-docs.factor +++ b/basis/math/functions/functions-docs.factor @@ -271,9 +271,9 @@ HELP: gcd { $notes "If " { $snippet "d" } " is 1, then " { $snippet "a" } " is the inverse of " { $snippet "y" } " modulo " { $snippet "x" } "." } ; HELP: divisor? -{ $values { "x" integer } { "y" 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" } "." } -{ $notes "Returns t for both negative and positive divisors, as well as trivial and non-trivial divisors." } ; +{ $values { "m" integer } { "n" integer } { "?" "a boolean" } } +{ $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 for trivial and non-trivial divisors." } ; HELP: mod-inv { $values { "x" integer } { "n" integer } { "y" integer } } diff --git a/basis/math/primes/factors/factors.factor b/basis/math/primes/factors/factors.factor index 199b72b7e1..9acc2b58c6 100644 --- a/basis/math/primes/factors/factors.factor +++ b/basis/math/primes/factors/factors.factor @@ -1,6 +1,6 @@ ! Copyright (C) 2007-2009 Samuel Tardieu. ! 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 : euler001c ( -- answer ) - 1000 [ { 3 5 } [ mod 0 = ] with any? ] filter sum ; + 1000 [ { 3 5 } [ divisor? ] with any? ] filter sum ; ! [ euler001c ] 100 ave-time ! 0 ms ave run time - 0.06 SD (100 trials) diff --git a/extra/project-euler/004/004.factor b/extra/project-euler/004/004.factor index ff62b4e181..fe09914d9f 100644 --- a/extra/project-euler/004/004.factor +++ b/extra/project-euler/004/004.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2007 Aaron Schaefer, Daniel Ehrenberg. ! See http://factorcode.org/license.txt for BSD license. -USING: hashtables kernel math math.ranges project-euler.common sequences - sorting sets ; +USING: hashtables kernel math math.functions math.ranges project-euler.common + sequences sorting sets ; IN: project-euler.004 ! http://projecteuler.net/index.php?section=problems&id=4 @@ -21,7 +21,7 @@ IN: project-euler.004 diff --git a/extra/project-euler/043/043.factor b/extra/project-euler/043/043.factor index 7edcd14364..75241499e1 100644 --- a/extra/project-euler/043/043.factor +++ b/extra/project-euler/043/043.factor @@ -1,7 +1,7 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: combinators.short-circuit kernel math math.combinatorics math.parser - math.ranges project-euler.common sequences sets sorting ; +USING: combinators.short-circuit kernel math math.functions 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 @@ -36,7 +36,7 @@ IN: project-euler.043 integer swap mod zero? ; + [ 1- dup 3 + ] dip subseq 10 digits>integer swap divisor? ; : interesting? ( seq -- ? ) { diff --git a/extra/project-euler/052/052.factor b/extra/project-euler/052/052.factor index 1b3b9ba1f1..c25b1adcc0 100644 --- a/extra/project-euler/052/052.factor +++ b/extra/project-euler/052/052.factor @@ -1,8 +1,7 @@ ! Copyright (c) 2008 Aaron Schaefer. ! See http://factorcode.org/license.txt for BSD license. -USING: combinators.short-circuit kernel math - project-euler.common sequences sorting - grouping ; +USING: combinators.short-circuit kernel math math.functions + project-euler.common sequences sorting grouping ; IN: project-euler.052 ! http://projecteuler.net/index.php?section=problems&id=52 @@ -31,7 +30,7 @@ IN: project-euler.052 [ number>digits natural-sort ] map all-equal? ; : candidate? ( n -- ? ) - { [ odd? ] [ 3 mod 0 = ] } 1&& ; + { [ odd? ] [ 3 divisor? ] } 1&& ; : next-all-same ( x n -- n ) dup candidate? [ diff --git a/extra/project-euler/common/common.factor b/extra/project-euler/common/common.factor index 0ad3225e3e..ba8c81fbf4 100644 --- a/extra/project-euler/common/common.factor +++ b/extra/project-euler/common/common.factor @@ -44,7 +44,7 @@ IN: project-euler.common : (sum-divisors) ( n -- sum ) 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 ] { } make sum ; @@ -120,7 +120,7 @@ PRIVATE> factor-2s dup [ 1+ ] [ perfect-square? -1 0 ? ] [ dup sqrt >fixnum [1,b] ] tri* [ - dupd mod 0 = [ [ 2 + ] dip ] when + dupd divisor? [ [ 2 + ] dip ] when ] each drop * ; ! These transforms are for generating primitive Pythagorean triples @@ -137,4 +137,3 @@ SYNTAX: SOLUTION: [ drop in get vocab (>>main) ] [ [ . ] swap prefix (( -- )) define-declared ] 2bi ; -