Another update from ingydotnet^djgoku

This commit is contained in:
Ingy döt Net 2015-11-18 06:14:39 +00:00
parent 91df62d461
commit 948b86eafa
7604 changed files with 108452 additions and 22726 deletions

View file

@ -0,0 +1,13 @@
(defn hamming
"Computes the unbounded sequence of Hamming 235 numbers."
[]
(letfn [(merge [xs ys]
(let [xv (first xs), yv (first ys)]
(if (< xv yv) (cons xv (lazy-seq (merge (next xs) ys)))
(cons yv (lazy-seq (merge xs (next ys))))))),
(smult [m s] ;; equiv to map (* m) s -- faster
(cons (*' m (first s)) (lazy-seq (smult m (next s)))))]
(do (def s5 (cons 5 (lazy-seq (smult 5 s5))))
(def s35 (cons 3 (lazy-seq (merge s5 (smult 3 s35)))))
(def s235 (cons 2 (lazy-seq (merge s35 (smult 2 s235)))))
(cons 1 (lazy-seq s235)))))

View file

@ -0,0 +1,43 @@
$ limit = p1
$
$ n = 0
$ h_'n = 1
$ x2 = 2
$ x3 = 3
$ x5 = 5
$ i = 0
$ j = 0
$ k = 0
$
$ n = 1
$ loop:
$ x = x2
$ if x3 .lt. x then $ x = x3
$ if x5 .lt. x then $ x = x5
$ h_'n = x
$ if x2 .eq. h_'n
$ then
$ i = i + 1
$ x2 = 2 * h_'i
$ endif
$ if x3 .eq. h_'n
$ then
$ j = j + 1
$ x3 = 3 * h_'j
$ endif
$ if x5 .eq. h_'n
$ then
$ k = k + 1
$ x5 = 5 * h_'k
$ endif
$ n = n + 1
$ if n .le. limit then $ goto loop
$
$ i = 0
$ loop2:
$ write sys$output h_'i
$ i = i + 1
$ if i .lt. 20 then $ goto loop2
$
$ n = limit - 1
$ write sys$output h_'n

View file

@ -0,0 +1,32 @@
defmodule Hamming do
def generater do
queues = [{2, queue}, {3, queue}, {5, queue}]
Stream.unfold({1, queues}, fn {n, q} -> next(n, q) end)
end
defp next(n, queues) do
queues = Enum.map(queues, fn {m, queue} -> {m, push(queue, m*n)} end)
min = Enum.map(queues, fn {_, queue} -> top(queue) end) |> Enum.min
queues = Enum.map(queues, fn {m, queue} ->
{m, (if min==top(queue), do: erase_top(queue), else: queue)}
end)
{n, {min, queues}}
end
defp queue, do: {[], []}
defp push({input, output}, term), do: {[term | input], output}
defp top({input, []}), do: List.last(input)
defp top({_, [h|_]}), do: h
defp erase_top({input, []}), do: erase_top({[], Enum.reverse(input)})
defp erase_top({input, [_|t]}), do: {input, t}
end
IO.puts "first twenty Hamming numbers:"
IO.inspect Hamming.generater |> Enum.take(20)
IO.puts "1691st Hamming number:"
IO.puts Hamming.generater |> Enum.take(1691) |> List.last
IO.puts "one millionth Hamming number:"
IO.puts Hamming.generater |> Enum.take(1_000_000) |> List.last

View file

@ -1,5 +1,5 @@
import java.math.BigInteger;
import java.util.PriorityQueue;
import java.math.BigInteger
import java.util.PriorityQueue
val Three = BigInteger.valueOf(3)
val Five = BigInteger.valueOf(5)

View file

@ -0,0 +1,33 @@
import java.math.BigInteger
import java.util.PriorityQueue
val One = BigInteger.ONE
val Three = BigInteger.valueOf(3)
val Five = BigInteger.valueOf(5)
fun PriorityQueue<BigInteger>.update(x: BigInteger) {
add(x shiftLeft(1))
add(x multiply(Three))
add(x multiply(Five))
}
fun hamming(n: Int): BigInteger {
val frontier = PriorityQueue<BigInteger>()
frontier.update(One)
var lowest = One
repeat(n - 1) {
lowest = frontier.poll() ?: lowest
while (frontier.peek() == lowest)
frontier.poll()
frontier.update(lowest)
}
return lowest
}
fun hamming(i : Iterable<Int>) : Iterable<BigInteger> = i.map { hamming(it) }
fun main(args: Array<String>) {
val r = 1..20
println("Hamming($r) = " + hamming(r))
arrayOf(1691, 1000000).forEach { println("Hamming(${it}) = " + hamming(it)) }
}

View file

@ -0,0 +1,32 @@
import java.math.BigInteger
import java.util.PriorityQueue
val One = BigInteger.ONE
val Three = BigInteger.valueOf(3)
val Five = BigInteger.valueOf(5)
fun PriorityQueue<BigInteger>.update(x: BigInteger) {
add(x shiftLeft 1)
add(x multiply Three)
add(x multiply Five)
}
fun hamming(a: Any?): Any = when (a) {
is Number -> {
val pq = PriorityQueue<BigInteger>()
pq update One
var lowest = One
repeat(a.toInt() - 1) {
lowest = pq.poll() ?: lowest
while (pq.peek() == lowest) pq.poll()
pq update lowest
}
lowest
}
is Iterable<*> -> a.map { hamming(it) }
else -> throw IllegalArgumentException("cannot parse argument")
}
fun main(args: Array<String>) {
arrayOf(1..20, 1691, 1000000).forEach { println("Hamming($it) = " + hamming(it)) }
}

View file

@ -1,13 +1,12 @@
my $limit = 32;
sub powers_of ($radix) { 1, [\*] $radix xx * }
sub powers_of ($radix) { 1, |[\*] $radix xx * }
my @hammings =
( powers_of(2)[^ $limit ] X*
( powers_of(3)[^($limit * 2/3)] X*
powers_of(3)[^($limit * 2/3)] X*
powers_of(5)[^($limit * 1/2)]
)
).sort;
).sort;
say ~@hammings[^20];
say @hammings[^20];
say @hammings[1690]; # zero indexed

View file

@ -1,22 +1,21 @@
/*REXX program computes Hamming numbers: 1──►20, #1691, one millionth.*/
numeric digits 100 /*ensure we have enough precision*/
call hamming 1, 20 /*show the first ──► twentieth #s*/
call hamming 1691 /*show the 1,691st Hamming number*/
call hamming 1000000 /*show the one millionth number.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HAMMING subroutine──────────────────*/
hamming: procedure; parse arg x,y; if y=='' then y=x; w=length(y)
#2=1; #3=1; #5=1; @.=0; @.1=1
/*REXX program computes Hamming numbers: 1 ──► 20, # 1691, the one millionth.*/
numeric digits 100 /*ensure enough decimal digits. */
call hamming 1, 20 /*show the 1st ──► twentieth Hamming #s*/
call hamming 1691 /*show the 1,691st Hamming number. */
call hamming 1000000 /*show the 1 millionth Hamming number.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
hamming: procedure; parse arg x,y; if y=='' then y=x; w=length(y)
#2=1; #3=1; #5=1; @.=0; @.1=1
do n=2 for y-1
@.n = min(2*@.#2, 3*@.#3, 5*@.#5) /*pick the minimum of three pigs.*/
if 2*@.#2 == @.n then #2 = #2+1 /*# already defined? Use next #.*/
if 3*@.#3 == @.n then #3 = #3+1 /*" " " " " " */
if 5*@.#5 == @.n then #5 = #5+1 /*" " " " " " */
end /*n*/
do j=x to y /*W is used to align the index. */
say 'Hamming('right(j,w)") =" @.j /*list 'em, Dano.*/
@.n = min(2*@.#2, 3*@.#3, 5*@.#5) /*pick the minimum of 3 Hamming numbers*/
if 2*@.#2 == @.n then #2 = #2+1 /*number already defined? Use next #. */
if 3*@.#3 == @.n then #3 = #3+1 /* " " " " " " */
if 5*@.#5 == @.n then #5 = #5+1 /* " " " " " " */
end /*n*/ /* [↑] maybe assign next 3 Hamming #s.*/
do j=x to y /*W is used to align the (output) index*/
say 'Hamming('right(j,w)") =" @.j /*display 'em, Dano.*/
end /*j*/
say right( 'length of last Hamming number =' length(@.y), 70); say
say right( 'length of last Hamming number =' length(@.y), 70); say
return

View file

@ -1,28 +1,27 @@
/*REXX program computes Hamming numbers: 1──►20, #1691, one millionth.*/
numeric digits 100 /*ensure we have enough precision*/
call hamming 1, 20 /*show the first ──► twentieth #s*/
call hamming 1691 /*show the 1,691st Hamming number*/
call hamming 1000000 /*show the one millionth number.*/
exit /*stick a fork in it, we're done.*/
/*──────────────────────────────────HAMMING subroutine──────────────────*/
hamming: procedure; parse arg x,y; if y=='' then y=x; w=length(y)
#2=1; #3=1; #5=1; @.=0; @.1=1
/*REXX program computes Hamming numbers: 1 ──► 20, # 1691, the one millionth.*/
numeric digits 100 /*ensure enough decimal digits. */
call hamming 1, 20 /*show the 1st ──► twentieth Hamming #s*/
call hamming 1691 /*show the 1,691st Hamming number. */
call hamming 1000000 /*show the 1 millionth Hamming number.*/
exit /*stick a fork in it, we're all done. */
/*────────────────────────────────────────────────────────────────────────────*/
hamming: procedure; parse arg x,y; if y=='' then y=x; w=length(y)
#2=1; #3=1; #5=1; @.=0; @.1=1
do n=2 for y-1
_2 = @.#2 + @.#2 /*this is faster than 2 * @.#2 */
_2 = @.#2 + @.#2 /*this is faster than: 2 * @.#2 */
_3 = 3 * @.#3
_5 = 5 * @.#5
m =_2 /*assume a minimum (of the three)*/
if _3 < m then m =_3 /*is this less than the minimum? */
if _5 < m then m =_5 /* " " " " " " */
@.n = m /*now, assign the next Hamming #.*/
if _2 == m then #2 = #2 + 1 /*# already defined? Use next #.*/
if _3 == m then #3 = #3 + 1 /*" " " " " " */
if _5 == m then #5 = #5 + 1 /*" " " " " " */
end /*n*/
do j=x to y /*W is used to align the index. */
say 'Hamming('right(j,w)") =" @.j /*list 'em, Dano.*/
m =_2 /*assume a minimum of the 3 Hamming #s.*/
if _3 < m then m =_3 /*is this number less than the minimum?*/
if _5 < m then m =_5 /* " " " " " " " */
@.n = m /*now, assign the next Hamming number. */
if _2 == m then #2 = #2 + 1 /*number already defined? Use next #. */
if _3 == m then #3 = #3 + 1 /* " " " " " " */
if _5 == m then #5 = #5 + 1 /* " " " " " " */
end /*n*/ /* [↑] maybe assign next 3 Hamming #s.*/
do j=x to y /*W is used to align the (output) index*/
say 'Hamming('right(j,w)") =" @.j /*display 'em, Dano.*/
end /*j*/
say right( 'length of last Hamming number =' length(@.y), 70); say
say right( 'length of last Hamming number =' length(@.y), 70); say
return

View file

@ -0,0 +1,13 @@
def hamming(): Stream[BigInt] = {
def merge(a: Stream[BigInt], b: Stream[BigInt]): Stream[BigInt] = {
val av = a.head; val bv = b.head
if (av < bv) av #:: merge(a.tail, b)
else bv #:: merge(a, b.tail)
}
def smult(m:BigInt, s: Stream[BigInt]): Stream[BigInt] =
(m * s.head) #:: smult(m, s.tail) // equiv to map (m *) s - faster
lazy val s5: Stream[BigInt] = 5 #:: smult(5, s5)
lazy val s35: Stream[BigInt] = 3 #:: merge(s5, smult(3, s35))
lazy val s235: Stream[BigInt] = 2 #:: merge(s35, smult(2, s235))
1 #:: s235
}

View file

@ -0,0 +1,22 @@
(define (hamming)
(define (merge a b)
(let ((x (car a)) (y (car b)))
(if (< x y) (cons x (delay (merge (force (cdr a)) b)))
(cons y (delay (merge a (force (cdr b))))))))
(define (smult m s) (cons (* m (car s))
(delay (smult m (force (cdr s)))))) ;; equiv to map (* m) s
(define s5 (cons 5 (delay (smult 5 s5))))
(define s35 (cons 3 (delay (merge s5 (smult 3 s35)))))
(define s235 (cons 2 (delay (merge s35 (smult 2 s235)))))
(cons 1 (delay s235)))
;;; test...
(define (stream-take->list n strm)
(if (= n 0) (list) (cons (car strm)
(stream-take->list (- n 1) (force (cdr strm))))))
(define (stream-ref strm nth)
(do ((nxt strm (force (cdr nxt))) (cnt 0 (+ cnt 1)))
((>= cnt nth) (car nxt))))
(display (stream-take->list 20 (hamming))) (newline)
(display (stream-ref (hamming) (- 1691 1))) (newline)
(display (stream-ref (hamming) (- 1000000 1))) (newline)

View file

@ -0,0 +1,21 @@
limit := 10 raisedToInteger: 84.
tape := Set new.
hammingProcess := [:newHamming|
(newHamming <= limit)
ifTrue:
[| index |
index := tape scanFor: newHamming.
(tape array at: index)
ifNil:
[tape atNewIndex: index put: newHamming asSetElement.
hammingProcess value: newHamming * 2.
hammingProcess value: newHamming * 3.
hammingProcess value: newHamming * 5]]].
hammingProcess value: 1.
sc := tape asSortedCollection.
sc first: 20. "a SortedCollection(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36)"
sc at: 1691. "2125764000"
sc at: 1000000. "519312780448388736089589843750000000000000000000000000000000000000000000000000000000"

View file

@ -0,0 +1,22 @@
For h = 1 To 20
WScript.StdOut.Write "H(" & h & ") = " & Hamming(h)
WScript.StdOut.WriteLine
Next
WScript.StdOut.Write "H(" & 1691 & ") = " & Hamming(1691)
WScript.StdOut.WriteLine
Function Hamming(l)
Dim h() : Redim h(l) : h(0) = 1
i = 0 : j = 0 : k = 0
x2 = 2 : x3 = 3 : x5 = 5
For n = 1 To l-1
m = x2
If m > x3 Then m = x3 End If
If m > x5 Then m = x5 End If
h(n) = m
If m = x2 Then i = i + 1 : x2 = 2 * h(i) End If
If m = x3 Then j = j + 1 : x3 = 3 * h(j) End If
If m = x5 Then k = k + 1 : x5 = 5 * h(k) End If
Next
Hamming = h(l-1)
End Function