How to resolve the algorithm Hamming numbers step by step in the Smalltalk programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Hamming numbers step by step in the Smalltalk programming language

Table of Contents

Problem Statement

Hamming numbers are numbers of the form   Hamming numbers   are also known as   ugly numbers   and also   5-smooth numbers   (numbers whose prime divisors are less or equal to 5).

Generate the sequence of Hamming numbers, in increasing order.   In particular:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Hamming numbers step by step in the Smalltalk programming language

Source code in the smalltalk programming language

Object subclass: Hammer [
  Hammer class >> hammingNumbers: howMany [
    |h i j k x2 x3 x5| 
      h := OrderedCollection new.
      i := 0. j := 0. k := 0.
      h add: 1.
      x2 := 2. x3 := 2. x5 := 5.
      [ ( h size) < howMany ] whileTrue: [
        |m|
        m := { x2. x3. x5 } sort first.
        (( h indexOf: m ) = 0) ifTrue: [ h add: m ].
        ( x2 = (h last) ) ifTrue: [ i := i + 1. x2 := 2 * (h at: i) ].
        ( x3 = (h last) ) ifTrue: [ j := j + 1. x3 := 3 * (h at: j) ].
        ( x5 = (h last) ) ifTrue: [ k := k + 1. x5 := 5 * (h at: k) ]. 
      ].
      ^ h sort
  ]
].

(Hammer hammingNumbers: 20) displayNl.
(Hammer hammingNumbers: 1690) last displayNl.

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"

tape := Heap with: 1 -> 1.
hammingStream :=
	[| next |
	next := tape removeFirst.
	next value <= 2 ifTrue: [tape add: next key * 2 -> 2].
	next value <= 3 ifTrue: [tape add: next key * 3 -> 3].
	next value <= 5 ifTrue: [tape add: next key * 5 -> 5].
	next key]
		reading.

hammingStream read: 20. "get first 20 values => #(1 2 3 4 5 6 8 9 10 12 15 16 18 20 24 25 27 30 32 36)"
hammingStream ++ 1670. "skip the next 1670 values"
hammingStream get. "and the 1691th value is => 2125764000".
hammingStream ++ (999999 - 1691). "now skip more to position at 999,999".
hammingStream get. "and the 1,000,000th value is =>  519312780448388736089589843750000000000000000000000000000000000000000000000000000000".
tape size. "See how many we have buffered =>  24904"

  

You may also check:How to resolve the algorithm Self numbers step by step in the XPL0 programming language
You may also check:How to resolve the algorithm Monads/List monad step by step in the J programming language
You may also check:How to resolve the algorithm Sorting algorithms/Quicksort step by step in the Lua programming language
You may also check:How to resolve the algorithm Handle a signal step by step in the PL/I programming language
You may also check:How to resolve the algorithm Break OO privacy step by step in the Go programming language