How to resolve the algorithm Hamming numbers step by step in the Ada programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Hamming numbers step by step in the Ada 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 Ada programming language
Source code in the ada programming language
with Ada.Numerics.Generic_Elementary_Functions;
with Ada.Text_IO; use Ada.Text_IO;
with GNATCOLL.GMP.Integers;
with GNATCOLL.GMP.Lib;
procedure Hamming is
type Log_Type is new Long_Long_Float;
package Funcs is new Ada.Numerics.Generic_Elementary_Functions (Log_Type);
type Factors_Array is array (Positive range <>) of Positive;
generic
Factors : Factors_Array := (2, 3, 5);
-- The factors for smooth numbers. Hamming numbers are 5-smooth.
package Smooth_Numbers is
type Number is private;
function Compute (Nth : Positive) return Number;
function Image (N : Number) return String;
private
type Exponent_Type is new Natural;
type Exponents_Array is array (Factors'Range) of Exponent_Type;
-- Numbers are stored as the exponents of the prime factors.
type Number is record
Exponents : Exponents_Array;
Log : Log_Type;
-- The log of the value, used to ease sorting.
end record;
function "=" (N1, N2 : Number) return Boolean
is (for all F in Factors'Range => N1.Exponents (F) = N2.Exponents (F));
end Smooth_Numbers;
package body Smooth_Numbers is
One : constant Number := (Exponents => (others => 0), Log => 0.0);
Factors_Log : array (Factors'Range) of Log_Type;
function Image (N : Number) return String is
use GNATCOLL.GMP.Integers, GNATCOLL.GMP.Lib;
R, Tmp : Big_Integer;
begin
Set (R, "1");
for F in Factors'Range loop
Set (Tmp, Factors (F)'Image);
Raise_To_N (Tmp, GNATCOLL.GMP.Unsigned_Long (N.Exponents (F)));
Multiply (R, Tmp);
end loop;
return Image (R);
end Image;
function Compute (Nth : Positive) return Number is
Candidates : array (Factors'Range) of Number;
Values : array (1 .. Nth) of Number;
-- Will result in Storage_Error for very large values of Nth
Indices : array (Factors'Range) of Natural :=
(others => Values'First);
Current : Number;
Tmp : Number;
begin
for F in Factors'Range loop
Factors_Log (F) := Funcs.Log (Log_Type (Factors (F)));
Candidates (F) := One;
Candidates (F).Exponents (F) := 1;
Candidates (F).Log := Factors_Log (F);
end loop;
Values (1) := One;
for Count in 2 .. Nth loop
-- Find next value (the lowest of the candidates)
Current := Candidates (Factors'First);
for F in Factors'First + 1 .. Factors'Last loop
if Candidates (F).Log < Current.Log then
Current := Candidates (F);
end if;
end loop;
Values (Count) := Current;
-- Update the candidates. There might be several candidates with
-- the same value
for F in Factors'Range loop
if Candidates (F) = Current then
Indices (F) := Indices (F) + 1;
Tmp := Values (Indices (F));
Tmp.Exponents (F) := Tmp.Exponents (F) + 1;
Tmp.Log := Tmp.Log + Factors_Log (F);
Candidates (F) := Tmp;
end if;
end loop;
end loop;
return Values (Nth);
end Compute;
end Smooth_Numbers;
package Hamming is new Smooth_Numbers ((2, 3, 5));
begin
for N in 1 .. 20 loop
Put (" " & Hamming.Image (Hamming.Compute (N)));
end loop;
New_Line;
Put_Line (Hamming.Image (Hamming.Compute (1691)));
Put_Line (Hamming.Image (Hamming.Compute (1_000_000)));
end Hamming;
You may also check:How to resolve the algorithm Loops/Downward for step by step in the Python programming language
You may also check:How to resolve the algorithm Substring step by step in the AArch64 Assembly programming language
You may also check:How to resolve the algorithm Nested function step by step in the Clojure programming language
You may also check:How to resolve the algorithm Random number generator (included) step by step in the Java programming language
You may also check:How to resolve the algorithm Sum multiples of 3 and 5 step by step in the J programming language