Renamed polygonal functions to include -number the name.

This commit is contained in:
Jens Axel Søgaard 2012-11-22 17:08:13 +01:00
parent fdfaf6bee0
commit 50c03c3622
3 changed files with 56 additions and 59 deletions

View File

@ -4,53 +4,53 @@
(only-in "number-theory.rkt" perfect-square)
"types.rkt")
(provide triangle triangle?
square?
pentagonal pentagonal?
hexagonal hexagonal?
heptagonal heptagonal?
octagonal octagonal?)
(provide triangle-number triangle-number?
square-number?
pentagonal-number pentagonal-number?
hexagonal-number hexagonal-number?
heptagonal-number heptagonal-number?
octagonal-number octagonal-number?)
(: triangle : Natural -> Natural)
(define (triangle n)
(: triangle-number : Natural -> Natural)
(define (triangle-number n)
(quotient (* n (+ n 1)) 2))
(: triangle? : Natural -> Boolean)
(define (triangle? n)
(: triangle-number? : Natural -> Boolean)
(define (triangle-number? n)
(not (null? (quadratic-natural-solutions 1/2 1/2 (- n)))))
(: square? : Natural -> Boolean)
(define (square? n)
(: square-number? : Natural -> Boolean)
(define (square-number? n)
(and (perfect-square n) #t))
(: pentagonal : Natural -> Natural)
(define (pentagonal n)
(: pentagonal-number : Natural -> Natural)
(define (pentagonal-number n)
(assert (quotient (* n (- (* 3 n) 1)) 2) natural?))
(: pentagonal? : Natural -> Boolean)
(define (pentagonal? n)
(: pentagonal-number? : Natural -> Boolean)
(define (pentagonal-number? n)
(not (null? (quadratic-natural-solutions 3/2 -1/2 (- n)))))
(: hexagonal : Natural -> Natural)
(define (hexagonal n)
(: hexagonal-number : Natural -> Natural)
(define (hexagonal-number n)
(assert (* n (- (* 2 n) 1)) natural?))
(: hexagonal? : Natural -> Boolean)
(define (hexagonal? n)
(: hexagonal-number? : Natural -> Boolean)
(define (hexagonal-number? n)
(not (null? (quadratic-natural-solutions 2 -1 (- n)))))
(: heptagonal : Natural -> Natural)
(define (heptagonal n)
(: heptagonal-number : Natural -> Natural)
(define (heptagonal-number n)
(assert (quotient (* n (- (* 5 n) 3)) 2) natural?))
(: heptagonal? : Natural -> Boolean)
(define (heptagonal? n)
(: heptagonal-number? : Natural -> Boolean)
(define (heptagonal-number? n)
(not (null? (quadratic-natural-solutions 5/2 -3/2 (- n)))))
(: octagonal : Natural -> Natural)
(define (octagonal n)
(: octagonal-number : Natural -> Natural)
(define (octagonal-number n)
(assert (* n (- (* 3 n) 2)) natural?))
(: octagonal? : Natural -> Boolean)
(define (octagonal? n)
(: octagonal-number? : Natural -> Boolean)
(define (octagonal-number? n)
(not (null? (quadratic-natural-solutions 3 -2 (- n)))))

View File

@ -698,30 +698,27 @@ Returns the @racket[n]th tangent number; @racket[n] must be nonnegative.
@subsection{Polygonal Numbers}
@defproc[(triangle? [n Natural]) Boolean]{}
@defproc[(square? [n Natural]) Boolean]{}
@defproc[(pentagonal? [n Natural]) Boolean]{}
@defproc[(hexagonal? [n Natural]) Boolean]{}
@defproc[(heptagonal? [n Natural]) Boolean]{}
@defproc[(octagonal? [n Natural]) Boolean]{
The functions
@racket[triangle?], @racket[square?], @racket[pentagonal?],
@racket[hexagonal?],@racket[heptagonal?] and @racket[octagonal?]
checks whether the input is a polygonal number of the types
@deftogether[
(@defproc[(triangle-number? [n Natural]) Boolean]
@defproc[(square-number? [n Natural]) Boolean]
@defproc[(pentagonal-number? [n Natural]) Boolean]
@defproc[(hexagonal-number? [n Natural]) Boolean]
@defproc[(heptagonal-number? [n Natural]) Boolean]
@defproc[(octagonal-number? [n Natural]) Boolean])]{
These functions check whether the input is a polygonal number of the types
triangle, square, pentagonal, hexagonal, heptagonal and octogonal
respectively.
}
@defproc[(triangle [n Natural]) Natural]{}
@defproc[(sqr [n Natural]) Natural]{}
@defproc[(pentagonal [n Natural]) Natural]{}
@defproc[(hexagonal [n Natural]) Natural]{}
@defproc[(heptagonal [n Natural]) Natural]{}
@defproc[(octagonal [n Natural]) Natural]{
The functions @racket[triangle], @racket[sqr], @racket[pentagonal],
@racket[hexagonal],@racket[heptagonal] and @racket[octagonal]
return the @racket[n]th polygonal number of the corresponding
type of polygonal number.
@deftogether[
(@defproc[(triangle-number [n Natural]) Natural]
@defproc[(sqr [n Natural]) Natural]
@defproc[(pentagonal-number [n Natural]) Natural]
@defproc[(hexagonal-number [n Natural]) Natural]
@defproc[(heptagonal-number [n Natural]) Natural]
@defproc[(octagonal-number [n Natural]) Natural])]{
These functions return the @racket[n]th polygonal number
of the corresponding type of polygonal number.
}

View File

@ -37,17 +37,17 @@
(check-true (andmap find-and-check-root '(1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 78125)))
;"polygonal.rkt"
(check-equal? (map triangle '(0 1 2 3 4 5)) '(0 1 3 6 10 15))
(check-equal? (map pentagonal '(0 1 2 3 4 5)) '(0 1 5 12 22 35))
(check-equal? (map hexagonal '(0 1 2 3 4 5)) '(0 1 6 15 28 45))
(check-equal? (map heptagonal '(0 1 2 3 4 5)) '(0 1 7 18 34 55))
(check-equal? (map octagonal '(0 1 2 3 4 5)) '(0 1 8 21 40 65))
(check-true (andmap triangle? '(0 1 3 6 10 15)))
(check-true (andmap square? '(0 1 4 9 16 25)))
(check-true (andmap pentagonal? '(0 1 5 12 22 35)))
(check-true (andmap hexagonal? '(0 1 6 15 28 45)))
(check-true (andmap heptagonal? '(0 1 7 18 34 55)))
(check-true (andmap octagonal? '(0 1 8 21 40 65)))
(check-equal? (map triangle-number '(0 1 2 3 4 5)) '(0 1 3 6 10 15))
(check-equal? (map pentagonal-number '(0 1 2 3 4 5)) '(0 1 5 12 22 35))
(check-equal? (map hexagonal-number '(0 1 2 3 4 5)) '(0 1 6 15 28 45))
(check-equal? (map heptagonal-number '(0 1 2 3 4 5)) '(0 1 7 18 34 55))
(check-equal? (map octagonal-number '(0 1 2 3 4 5)) '(0 1 8 21 40 65))
(check-true (andmap triangle-number? '(0 1 3 6 10 15)))
(check-true (andmap square-number? '(0 1 4 9 16 25)))
(check-true (andmap pentagonal-number? '(0 1 5 12 22 35)))
(check-true (andmap hexagonal-number? '(0 1 6 15 28 45)))
(check-true (andmap heptagonal-number? '(0 1 7 18 34 55)))
(check-true (andmap octagonal-number? '(0 1 8 21 40 65)))
; "farey.rkt"
(check-equal? (farey 5) '(0 1/5 1/4 1/3 2/5 1/2 3/5 2/3 3/4 4/5 1))