This commit is contained in:
Ingy döt Net 2013-06-05 21:47:54 +00:00
parent 1f1ad49427
commit 6f050a029e
2496 changed files with 37609 additions and 3031 deletions

View file

@ -6,10 +6,10 @@ The general idea behind an executable library is to create a library that when u
* The library, when executed directly should satisfy the remaining requirements of the [[Hailstone sequence]] task:
:: 2. Use the routine to show that the hailstone sequence for the number 27 has 112 elements starting with 27, 82, 41, 124 and ending with 8, 4, 2, 1
:: 3. Show the number less than 100,000 which has the longest hailstone sequence together with that sequences length.
:: 3. Show the number less than 100,000 which has the longest hailstone sequence together with that sequence's length.
* Create a second executable to calculate the following:
** Use the libraries hailstone function, in the standard manner, (or document how this use deviates from standard use of a library), together with extra code in this executable, to find the hailstone length returned most often for 1 ≤ n < 100,000.
** Use the library's hailstone function, in the standard manner, (or document how this use deviates from standard use of a library), together with extra code in this executable, to find the hailstone length returned most often for 1 ≤ n < 100,000.
* Explain any extra setup/run steps needed to complete the task.

View file

@ -0,0 +1,35 @@
/* NetRexx */
options replace format comments java crossref symbols nobinary
import RHailstoneSequence
runSample(arg)
return
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method runSample(arg) private static
parse arg beginNum endNum .
if beginNum = '' | beginNum = '.' then beginNum = 1
if endNum = '' | endNum = '.' then endNum = 100000
if beginNum > endNum then signal IllegalArgumentException('Gronk!')
-- collect sequences
hailstones = 0
loop hn = beginNum while hn < endNum
hslist = RHailstoneSequence.hailstone(hn)
hscount = hslist.words()
hailstones[hscount] = hailstones[hscount] + 1
end hn
-- locate most common
mostOftenNum = 0
mostOftenCount = 0
loop hn = beginNum while hn < endNum
if hailstones[hn] > mostOftenCount then do
mostOftenCount = hailstones[hn]
mostOftenNum = hn
end
end hn
say 'The length of hailstone sequence that is most common in the range' beginNum '<= N <' endNum 'is' mostOftenNum'. It occurs' mostOftenCount 'times.'
return

View file

@ -0,0 +1,21 @@
#lang racket
(provide hailstone)
(define hailstone
(let ([t (make-hasheq)])
(hash-set! t 1 '(1))
(λ(n) (hash-ref! t n
(λ() (cons n (hailstone (if (even? n) (/ n 2) (+ (* 3 n) 1)))))))))
(module+ main
(define h27 (hailstone 27))
(printf "h(27) = ~s, ~s items\n"
`(,@(take h27 4) ... ,@(take-right h27 4))
(length h27))
(define N 100000)
(define longest
(for/fold ([m #f]) ([i (in-range 1 (add1 N))])
(define h (hailstone i))
(if (and m (> (cdr m) (length h))) m (cons i (length h)))))
(printf "for x<=~s, ~s has the longest sequence with ~s items\n"
N (car longest) (cdr longest)))

View file

@ -0,0 +1,12 @@
#lang racket
(require "hs.rkt")
(define N 100000)
(define t (make-hasheq))
(define best
(for/fold ([best #f]) ([i (in-range 1 (add1 N))])
(define len (length (hailstone i)))
(define freq (add1 (hash-ref t len 0)))
(hash-set! t len freq)
(if (and best (> (car best) freq)) best (cons freq len))))
(printf "Most frequent sequence length for x<=~s: ~s, appearing ~s times\n" N
(cdr best) (car best))