Humble numbers: Difference between revisions

Content added Content deleted
(Added AutoHotkey)
m (syntax highlighting fixup automation)
Line 36: Line 36:
{{trans|C++}}
{{trans|C++}}


<lang 11l>F is_humble(i)
<syntaxhighlight lang="11l">F is_humble(i)
I i <= 1
I i <= 1
R 1B
R 1B
Line 65: Line 65:
I num !C humble
I num !C humble
L.break
L.break
print(‘#5 have #. digits’.format(humble[num], num))</lang>
print(‘#5 have #. digits’.format(humble[num], num))</syntaxhighlight>


{{out}}
{{out}}
Line 84: Line 84:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO;


procedure Show_Humble is
procedure Show_Humble is
Line 149: Line 149:
Count_Humble_Digits;
Count_Humble_Digits;
Show_Digit_Counts;
Show_Digit_Counts;
end Show_Humble;</lang>
end Show_Humble;</syntaxhighlight>


{{out}}
{{out}}
Line 168: Line 168:


=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
<lang algol68>BEGIN # find some Humble numbers - numbers with no prime factors above 7 #
<syntaxhighlight lang="algol68">BEGIN # find some Humble numbers - numbers with no prime factors above 7 #
INT max humble = 2048;
INT max humble = 2048;
INT max shown humble = 49;
INT max shown humble = 49;
Line 212: Line 212:
)
)
OD
OD
END</lang>
END</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 226: Line 226:
=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
As noted by other samples, this is similar to the Hamming Numbers task. This is a modified version of the Algol W solution for Hamming Numbers. The numbers are generated in sequence.
As noted by other samples, this is similar to the Hamming Numbers task. This is a modified version of the Algol W solution for Hamming Numbers. The numbers are generated in sequence.
<lang algolw>begin % find some Humble numbers - numbers with no prime factors above 7 %
<syntaxhighlight lang="algolw">begin % find some Humble numbers - numbers with no prime factors above 7 %
% returns the minimum of a and b %
% returns the minimum of a and b %
integer procedure min ( integer value a, b ) ; if a < b then a else b;
integer procedure min ( integer value a, b ) ; if a < b then a else b;
Line 290: Line 290:
write( "there are", h6, " Humble numbers with 6 digits" )
write( "there are", h6, " Humble numbers with 6 digits" )
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 303: Line 303:


=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
<lang AutoHotkey>n := 1, c := 0
<syntaxhighlight lang="autohotkey">n := 1, c := 0
while (c < 50)
while (c < 50)
{
{
Line 362: Line 362:
ans.push(n)
ans.push(n)
return ans
return ans
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 372: Line 372:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f HUMBLE_NUMBERS.AWK
# syntax: GAWK -f HUMBLE_NUMBERS.AWK
#
#
Line 404: Line 404:
return(0)
return(0)
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 422: Line 422:
=={{header|C}}==
=={{header|C}}==
{{trans|C++}}
{{trans|C++}}
<lang C>#include <limits.h>
<syntaxhighlight lang="c">#include <limits.h>
#include <stdbool.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>
Line 469: Line 469:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 486: Line 486:
=={{header|C sharp|C#}}==
=={{header|C sharp|C#}}==
{{trans|D}}
{{trans|D}}
<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using System.Collections.Generic;
using System.Collections.Generic;


Line 535: Line 535:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 553: Line 553:
{{trans|Go}}
{{trans|Go}}
{{libheader|System.Numerics}}
{{libheader|System.Numerics}}
<lang csharp>#define BI
<syntaxhighlight lang="csharp">#define BI


using System;
using System;
Line 594: Line 594:
Console.WriteLine("The first {0} humble numbers are: {1}", firstAmt, string.Join(" ",h.Take(firstAmt)));
Console.WriteLine("The first {0} humble numbers are: {1}", firstAmt, string.Join(" ",h.Take(firstAmt)));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Results from a core i7-7700 @ 3.6Ghz.<br/>BigIntegers: (tabulates up to 100 digits in about 3/4 of a minute, but a lot of memory is consumed - 4.2 GB)
Results from a core i7-7700 @ 3.6Ghz.<br/>BigIntegers: (tabulates up to 100 digits in about 3/4 of a minute, but a lot of memory is consumed - 4.2 GB)
Line 732: Line 732:
Why use fixed-point logarithms of UIint64 instead of double? Because the rounding of the doubles when added together throws the sums off a bit so they don't match properly when incrementing the i, j, k, & l variables. If one were to change the 'fac" variable to a larger number, such as 1e15, there is too much "noise" on the least significant bits and the ''ijkl'' variables advance unevenly enough to throw off some of the counts. Some of the solutions presented here implement an "error banding" check to defeat this issue, but it seems a bit over complicated.
Why use fixed-point logarithms of UIint64 instead of double? Because the rounding of the doubles when added together throws the sums off a bit so they don't match properly when incrementing the i, j, k, & l variables. If one were to change the 'fac" variable to a larger number, such as 1e15, there is too much "noise" on the least significant bits and the ''ijkl'' variables advance unevenly enough to throw off some of the counts. Some of the solutions presented here implement an "error banding" check to defeat this issue, but it seems a bit over complicated.


<lang csharp>using System;
<syntaxhighlight lang="csharp">using System;
using UI = System.UInt64;
using UI = System.UInt64;


Line 773: Line 773:
humLog(255); // see tabulation for digits 1 to 255
humLog(255); // see tabulation for digits 1 to 255
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
verified results against the Pascal entry:
verified results against the Pascal entry:
Line 1,038: Line 1,038:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang cpp>#include <iomanip>
<syntaxhighlight lang="cpp">#include <iomanip>
#include <iostream>
#include <iostream>
#include <map>
#include <map>
Line 1,097: Line 1,097:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 1,114: Line 1,114:
=== Direct Generation - Variant ===
=== Direct Generation - Variant ===
A direct generation variant. Rather quick, as the humble numbers are not generated in order. And the digits are not counted individually, the log representation of each humble number is just binned into the decade tally with a simple division by log(10). Note: g++ compiler options: <code>-O3 -std=c++17</code>
A direct generation variant. Rather quick, as the humble numbers are not generated in order. And the digits are not counted individually, the log representation of each humble number is just binned into the decade tally with a simple division by log(10). Note: g++ compiler options: <code>-O3 -std=c++17</code>
<lang c>#include <chrono>
<syntaxhighlight lang="c">#include <chrono>
#include <cmath>
#include <cmath>
#include <locale>
#include <locale>
Line 1,151: Line 1,151:
delete [] bins;
delete [] bins;
printf("Counting took %8f seconds\n", duration<double>(steady_clock::now() - st).count());
printf("Counting took %8f seconds\n", duration<double>(steady_clock::now() - st).count());
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
Seems to give correct values as compared to the pascal (modification of hamming numbers fast alternative) version. And goes noticeably faster, up to 877 digits in about 3 1/4 minutes, where as pascal takes 1 1/3 hours to get to 877 digits.
Seems to give correct values as compared to the pascal (modification of hamming numbers fast alternative) version. And goes noticeably faster, up to 877 digits in about 3 1/4 minutes, where as pascal takes 1 1/3 hours to get to 877 digits.
Line 2,040: Line 2,040:
Checks if each number upto limit is humble number.
Checks if each number upto limit is humble number.
{{trans|C++}}
{{trans|C++}}
<lang ruby>def humble?(i)
<syntaxhighlight lang="ruby">def humble?(i)
return true if (i < 2)
return true if (i < 2)
return humble?(i // 2) if (i % 2 == 0)
return humble?(i // 2) if (i % 2 == 0)
Line 2,063: Line 2,063:


print "\n\nOf the first #{count} humble numbers:\n"
print "\n\nOf the first #{count} humble numbers:\n"
(1..digits).each { |num| printf("%5d have %2d digits\n", humble[num], num) }</lang>
(1..digits).each { |num| printf("%5d have %2d digits\n", humble[num], num) }</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 2,082: Line 2,082:
Generate humble numbers directly.
Generate humble numbers directly.
{{trans|Zkl}}
{{trans|Zkl}}
<lang ruby>require "big"
<syntaxhighlight lang="ruby">require "big"


def humble(digits)
def humble(digits)
Line 2,106: Line 2,106:
print "First 50 Humble Numbers: \n"; (0...50).each { |i| print "#{h[i]} " }
print "First 50 Humble Numbers: \n"; (0...50).each { |i| print "#{h[i]} " }
print "\n\nOf the first #{count} humble numbers:\n"
print "\n\nOf the first #{count} humble numbers:\n"
(1..digits).each { |num| printf("%6d have %2d digits\n", counts[num], num) }</lang>
(1..digits).each { |num| printf("%6d have %2d digits\n", counts[num], num) }</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Humble Numbers:
<pre>First 50 Humble Numbers:
Line 2,165: Line 2,165:
=={{header|D}}==
=={{header|D}}==
{{trans|C++}}
{{trans|C++}}
<lang d>import std.conv;
<syntaxhighlight lang="d">import std.conv;
import std.stdio;
import std.stdio;


Line 2,206: Line 2,206:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 2,225: Line 2,225:
=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
===The Functions===
===The Functions===
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Generate humble numbers. Nigel Galloway: June 18th., 2020
// Generate humble numbers. Nigel Galloway: June 18th., 2020
let fN g=let mutable n=1UL in (fun()->n<-n*g;n)
let fN g=let mutable n=1UL in (fun()->n<-n*g;n)
Line 2,238: Line 2,238:
|r->vg<-fG vg (fI vn (g()));vn<-n();v<-Some r;vg()
|r->vg<-fG vg (fI vn (g()));vn<-n();v<-Some r;vg()
let humble = seq{yield 1UL;yield! fE(fL (fN 7UL) (fun()->fL (fN 5UL) (fun()->fL (fN 3UL) (fun()->fN 2UL))))}
let humble = seq{yield 1UL;yield! fE(fL (fN 7UL) (fun()->fL (fN 5UL) (fun()->fL (fN 3UL) (fun()->fN 2UL))))}
</syntaxhighlight>
</lang>
===The Tasks===
===The Tasks===
<lang fsharp>
<syntaxhighlight lang="fsharp">
humble |> Seq.take 50 |> Seq.iter (printf "%d ");printfn ""
humble |> Seq.take 50 |> Seq.iter (printf "%d ");printfn ""
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
</pre>
</pre>
<lang fsharp>
<syntaxhighlight lang="fsharp">
for n in [1..18] do let g=pown 10UL n in printfn "There are %d humble numbers with %d digits" (humble|>Seq.skipWhile(fun n->n<g/10UL)|>Seq.takeWhile(fun n->n<g)|>Seq.length) n
for n in [1..18] do let g=pown 10UL n in printfn "There are %d humble numbers with %d digits" (humble|>Seq.skipWhile(fun n->n<g/10UL)|>Seq.takeWhile(fun n->n<g)|>Seq.length) n
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,272: Line 2,272:
</pre>
</pre>
=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: accessors assocs combinators deques dlists formatting fry
<syntaxhighlight lang="factor">USING: accessors assocs combinators deques dlists formatting fry
generalizations io kernel make math math.functions math.order
generalizations io kernel make math math.functions math.order
prettyprint sequences tools.memory.private ;
prettyprint sequences tools.memory.private ;
Line 2,330: Line 2,330:
] tri ] time ;
] tri ] time ;


MAIN: humble-numbers</lang>
MAIN: humble-numbers</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">
Line 2,440: Line 2,440:
=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Visual Basic .NET}}
{{trans|Visual Basic .NET}}
<lang freebasic>Function IsHumble(i As Integer) As Boolean
<syntaxhighlight lang="freebasic">Function IsHumble(i As Integer) As Boolean
If i <= 1 Then Return True
If i <= 1 Then Return True
If i Mod 2 = 0 Then Return IsHumble(i \ 2)
If i Mod 2 = 0 Then Return IsHumble(i \ 2)
Line 2,483: Line 2,483:
Exit While
Exit While
End If
End If
Wend</lang>
Wend</syntaxhighlight>
{{out}}
{{out}}
<pre>Los 50 primeros números de Humble son:
<pre>Los 50 primeros números de Humble son:
Line 2,507: Line 2,507:
=={{header|Go}}==
=={{header|Go}}==
Not particularly fast and uses a lot of memory but easier to understand than the 'log' based methods for generating 7-smooth numbers.
Not particularly fast and uses a lot of memory but easier to understand than the 'log' based methods for generating 7-smooth numbers.
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 2,601: Line 2,601:
fmt.Printf("%9s have %2d digit%s\n", commatize(counts[i]), i, s)
fmt.Printf("%9s have %2d digit%s\n", commatize(counts[i]), i, s)
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,682: Line 2,682:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.Set (deleteFindMin, fromList, union)
<syntaxhighlight lang="haskell">import Data.Set (deleteFindMin, fromList, union)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)
import Data.List (group)
import Data.List (group)
Line 2,708: Line 2,708:
------------------------- DISPLAY -------------------------
------------------------- DISPLAY -------------------------
justifyRight :: Int -> a -> [a] -> [a]
justifyRight :: Int -> a -> [a] -> [a]
justifyRight n c = (drop . length) <*> (replicate n c ++)</lang>
justifyRight n c = (drop . length) <*> (replicate n c ++)</syntaxhighlight>
{{Out}}
{{Out}}
<pre>First 50 Humble numbers:
<pre>First 50 Humble numbers:
Line 2,746: Line 2,746:
=={{header|J}}==
=={{header|J}}==
Multiply all the humble numbers by all the factors appending the next largest value.
Multiply all the humble numbers by all the factors appending the next largest value.
<lang>
<syntaxhighlight lang="text">
humble=: 4 : 0
humble=: 4 : 0
NB. x humble y generates x humble numbers based on factors y
NB. x humble y generates x humble numbers based on factors y
Line 2,755: Line 2,755:
end.
end.
)
)
</syntaxhighlight>
</lang>
<pre>
<pre>
p: i.4
p: i.4
Line 2,774: Line 2,774:


Use a class to simulate the python generator. This is a more efficient implementation of the first method.
Use a class to simulate the python generator. This is a more efficient implementation of the first method.
<lang>
<syntaxhighlight lang="text">
FACTORS_h_=: p: i. 4
FACTORS_h_=: p: i. 4
HUMBLE_h_=: 1
HUMBLE_h_=: 1
Line 2,784: Line 2,784:
)
)
reset_h_=: 3 :'0 $ HUMBLE=: 1'
reset_h_=: 3 :'0 $ HUMBLE=: 1'
</syntaxhighlight>
</lang>
<pre>
<pre>
3 :0 [ 50 [ reset_h_''
3 :0 [ 50 [ reset_h_''
Line 2,826: Line 2,826:


=={{header|Java}}==
=={{header|Java}}==
<lang java>
<syntaxhighlight lang="java">
import java.math.BigInteger;
import java.math.BigInteger;
import java.util.ArrayList;
import java.util.ArrayList;
Line 2,890: Line 2,890:


}
}
</syntaxhighlight>
</lang>
{{Out}}
{{Out}}
<pre>
<pre>
Line 2,936: Line 2,936:


=={{header|JavaScript}}==
=={{header|JavaScript}}==
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 3,170: Line 3,170:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>First 50 humble numbers:
<pre>First 50 humble numbers:
Line 3,197: Line 3,197:
===Brute force===
===Brute force===
First, brute force (because we can) ...
First, brute force (because we can) ...
<lang>
<syntaxhighlight lang="text">
# Input: a positive integer
# Input: a positive integer
# Output: true iff the input is humble
# Output: true iff the input is humble
Line 3,228: Line 3,228:
(.humble | range(1;length) as $i | " \($i): \(.[$i])") ;
(.humble | range(1;length) as $i | " \($i): \(.[$i])") ;


task(6; 50)</lang>
task(6; 50)</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50:
<pre>First 50:
Line 3,243: Line 3,243:


Having already shown one way to display the first few humble numbers, this subsection will focus on the more difficult problem.
Having already shown one way to display the first few humble numbers, this subsection will focus on the more difficult problem.
<syntaxhighlight lang="jq">
<lang jq>
# A generator
# A generator
def humbles($digits):
def humbles($digits):
Line 3,267: Line 3,267:
(distribution(humbles($digits)) | range(0;length) as $i | " \($i+1): \(.[$i])") ;
(distribution(humbles($digits)) | range(0;length) as $i | " \($i+1): \(.[$i])") ;


task(16)</lang>
task(16)</syntaxhighlight>
{{out}}
{{out}}
<pre>Distribution of the number of decimal digits up to 16 digits:
<pre>Distribution of the number of decimal digits up to 16 digits:
Line 3,291: Line 3,291:
=={{header|Julia}}==
=={{header|Julia}}==
To spare heap memory, keeps only the last 2 million values found for use in the generation of further values.
To spare heap memory, keeps only the last 2 million values found for use in the generation of further values.
<lang julia>
<syntaxhighlight lang="julia">
function counthumbledigits(maxdigits, returnsequencelength=50)
function counthumbledigits(maxdigits, returnsequencelength=50)
n, count, adjustindex, maxdiff = BigInt(1), 0, BigInt(0), 0
n, count, adjustindex, maxdiff = BigInt(1), 0, BigInt(0), 0
Line 3,334: Line 3,334:
println(lpad(digitcounts[ndigits], 10), " have ", lpad(ndigits, 3), " digits.")
println(lpad(digitcounts[ndigits], 10), " have ", lpad(ndigits, 3), " digits.")
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
828.693164 seconds (3.61 G allocations: 64.351 GiB, 51.37% gc time)
828.693164 seconds (3.61 G allocations: 64.351 GiB, 51.37% gc time)
Line 3,447: Line 3,447:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>fun isHumble(i: Int): Boolean {
<syntaxhighlight lang="scala">fun isHumble(i: Int): Boolean {
if (i <= 1) return true
if (i <= 1) return true
if (i % 2 == 0) return isHumble(i / 2)
if (i % 2 == 0) return isHumble(i / 2)
Line 3,486: Line 3,486:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 3,503: Line 3,503:
=={{header|Lua}}==
=={{header|Lua}}==
{{trans|C}}
{{trans|C}}
<lang lua>function isHumble(n)
<syntaxhighlight lang="lua">function isHumble(n)
local n2 = math.floor(n)
local n2 = math.floor(n)


Line 3,555: Line 3,555:
end
end


main()</lang>
main()</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 3,573: Line 3,573:
Create a simple function which efficiently generates humble numbers up to an inputted max number, then call it twice to generate the output. Finds the number of humble numbers with digits up to 100 in 5 minutes.
Create a simple function which efficiently generates humble numbers up to an inputted max number, then call it twice to generate the output. Finds the number of humble numbers with digits up to 100 in 5 minutes.


<lang Mathematica>HumbleGenerator[max_] :=
<syntaxhighlight lang="mathematica">HumbleGenerator[max_] :=
Sort[Flatten@ParallelTable[
Sort[Flatten@ParallelTable[
2^i 3^j 5^k 7^m, {i, 0, Log[2, max]}, {j, 0, Log[3, max/2^i]}, {k,
2^i 3^j 5^k 7^m, {i, 0, Log[2, max]}, {j, 0, Log[3, max/2^i]}, {k,
Line 3,582: Line 3,582:
"\nDigits\[Rule]Count",
"\nDigits\[Rule]Count",
Rule @@@ Tally[IntegerLength /@ Drop[HumbleGenerator[10^100], -1]] //
Rule @@@ Tally[IntegerLength /@ Drop[HumbleGenerator[10^100], -1]] //
Column} // Column</lang>
Column} // Column</syntaxhighlight>


{{out}}
{{out}}
Line 3,692: Line 3,692:
=={{header|Nim}}==
=={{header|Nim}}==
A simple algorithm efficient enough to get the number of humble numbers with 18 digits in less than four seconds. To get further, we would have to use big numbers and a more efficient algorithm.
A simple algorithm efficient enough to get the number of humble numbers with 18 digits in less than four seconds. To get further, we would have to use big numbers and a more efficient algorithm.
<lang Nim>import sets, strformat
<syntaxhighlight lang="nim">import sets, strformat




Line 3,749: Line 3,749:
echo ""
echo ""
echo "Count of humble numbers with n digits:"
echo "Count of humble numbers with n digits:"
showHumbleCount(18)</lang>
showHumbleCount(18)</syntaxhighlight>


{{out}}
{{out}}
Line 3,785: Line 3,785:
float32 get wrong at 37 digits,->37 104925 instead of 104926<BR>
float32 get wrong at 37 digits,->37 104925 instead of 104926<BR>
runtime: 2 x digits => ~ runtime 2^4 <BR>
runtime: 2 x digits => ~ runtime 2^4 <BR>
<lang pascal>
<syntaxhighlight lang="pascal">
{$IFDEF FPC}
{$IFDEF FPC}
{$MODE DELPHI}
{$MODE DELPHI}
Line 4,164: Line 4,164:
first50;
first50;
GetDigitCounts(100);
GetDigitCounts(100);
End.</lang>
End.</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 4,299: Line 4,299:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use List::Util 'min';
use List::Util 'min';
Line 4,337: Line 4,337:
printf "Digits: %2d - Count: %s\n", $digits++, $count;
printf "Digits: %2d - Count: %s\n", $digits++, $count;
$count = 1;
$count = 1;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:20ex">1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre style="height:20ex">1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 4,398: Line 4,398:
It will go all the way to 100 digits if you give it time (18 mins, on 64bit - 32bit runs out of memory after printing the 99th line)<br>
It will go all the way to 100 digits if you give it time (18 mins, on 64bit - 32bit runs out of memory after printing the 99th line)<br>
I also tried a log version (similar to [[Hamming_numbers#A_much_faster_logarithmic_version|Hamming_numbers]]) but inaccuracies with floor(h[n][LOG]) crept in quite early, at just 10 digits.
I also tried a log version (similar to [[Hamming_numbers#A_much_faster_logarithmic_version|Hamming_numbers]]) but inaccuracies with floor(h[n][LOG]) crept in quite early, at just 10 digits.
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #000080;font-style:italic;">-- demo/rosetta/humble.exw</span>
<span style="color: #000080;font-style:italic;">-- demo/rosetta/humble.exw</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
Line 4,457: Line 4,457:
<span style="color: #000000;">humble</span><span style="color: #0000FF;">(</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">humble</span><span style="color: #0000FF;">(</span><span style="color: #000000;">50</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">humble</span><span style="color: #0000FF;">(</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<span style="color: #000000;">humble</span><span style="color: #0000FF;">(</span><span style="color: #000000;">42</span><span style="color: #0000FF;">,</span><span style="color: #004600;">true</span><span style="color: #0000FF;">)</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 4,513: Line 4,513:
{{Trans|ALGOL W}}This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator or clone.
{{Trans|ALGOL W}}This can be compiled with the original 8080 PL/M compiler and run under CP/M or an emulator or clone.
Only handles Humble numbers with up to 4 digits as 8080 PL/M only has unsigned 8 and 16 bit integers.
Only handles Humble numbers with up to 4 digits as 8080 PL/M only has unsigned 8 and 16 bit integers.
<lang pli>100H: /* FIND SOME HUMBLE NUMBERS - NUMBERS WITH NO PRIME FACTORS ABOVE 7 */
<syntaxhighlight lang="pli">100H: /* FIND SOME HUMBLE NUMBERS - NUMBERS WITH NO PRIME FACTORS ABOVE 7 */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
BDOS: PROCEDURE( FN, ARG ); /* CP/M BDOS SYSTEM CALL */
DECLARE FN BYTE, ARG ADDRESS;
DECLARE FN BYTE, ARG ADDRESS;
Line 4,588: Line 4,588:
CALL PRINT$H$STAT( H3, 3 );
CALL PRINT$H$STAT( H3, 3 );
CALL PRINT$H$STAT( H4, 4 );
CALL PRINT$H$STAT( H4, 4 );
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,607: Line 4,607:
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.<br><br>
Note the use of text in column 81 onwards to hide the PL/I specifics from the PL/M compiler.<br><br>
Based on the PL/M version, note PL/I does not have the "walrus operator" (:=) which allows assignments to be nested in expressions, so it can't be used in the non-PL/M specific parts of this.
Based on the PL/M version, note PL/I does not have the "walrus operator" (:=) which allows assignments to be nested in expressions, so it can't be used in the non-PL/M specific parts of this.
<lang pli>/* FIND SOME HUMBLE NUMBERS - NUMBERS WITH NO PRIME FACTORS ABOVE 7 */
<syntaxhighlight lang="pli">/* FIND SOME HUMBLE NUMBERS - NUMBERS WITH NO PRIME FACTORS ABOVE 7 */
humble_100H: procedure options (main);
humble_100H: procedure options (main);


Line 4,721: Line 4,721:
CALL PRHUMBLESTAT( H4, 4 );
CALL PRHUMBLESTAT( H4, 4 );


EOF: end humble_100H;</lang>
EOF: end humble_100H;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,736: Line 4,736:
=={{header|Python}}==
=={{header|Python}}==
{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''Humble numbers'''
<syntaxhighlight lang="python">'''Humble numbers'''


from itertools import groupby, islice
from itertools import groupby, islice
Line 4,804: Line 4,804:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>First 50 Humble numbers:
<pre>First 50 Humble numbers:
Line 4,831: Line 4,831:
Uses <code>smoothwith</code> from [[N-smooth numbers#Quackery]], and <code>searchwith</code> from [[Binary search#Quackery]].
Uses <code>smoothwith</code> from [[N-smooth numbers#Quackery]], and <code>searchwith</code> from [[Binary search#Quackery]].


<lang Quackery> ' [ 2 3 5 7 ] smoothwith
<syntaxhighlight lang="quackery"> ' [ 2 3 5 7 ] smoothwith
[ -1 peek [ 10 12 ** ] constant = ]
[ -1 peek [ 10 12 ** ] constant = ]
-1 split drop
-1 split drop
Line 4,842: Line 4,842:
say "-digit humble numbers" cr ]
say "-digit humble numbers" cr ]
drop
drop
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,865: Line 4,865:
{{trans|Go}}
{{trans|Go}}


<lang racket>#lang racket
<syntaxhighlight lang="racket">#lang racket


(define (gen-humble-numbers N (kons #f) (k0 (void)))
(define (gen-humble-numbers N (kons #f) (k0 (void)))
Line 4,908: Line 4,908:
(module+ main
(module+ main
(Humble-numbers))
(Humble-numbers))
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 4,948: Line 4,948:
{{works with|Rakudo|2019.07.1}}
{{works with|Rakudo|2019.07.1}}


<lang perl6>sub smooth-numbers (*@list) {
<syntaxhighlight lang="raku" line>sub smooth-numbers (*@list) {
cache my \Smooth := gather {
cache my \Smooth := gather {
my %i = (flat @list) Z=> (Smooth.iterator for ^@list);
my %i = (flat @list) Z=> (Smooth.iterator for ^@list);
Line 4,977: Line 4,977:
$count = 1;
$count = 1;
last if $digits > $upto;
last if $digits > $upto;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 5,033: Line 5,033:


=={{header|REXX}}==
=={{header|REXX}}==
<lang rexx>/*REXX program computes and displays humble numbers, also will display counts of sizes.*/
<syntaxhighlight lang="rexx">/*REXX program computes and displays humble numbers, also will display counts of sizes.*/
parse arg n m . /*obtain optional arguments from the CL*/
parse arg n m . /*obtain optional arguments from the CL*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
if n=='' | n=="," then n= 50 /*Not specified? Then use the default.*/
Line 5,076: Line 5,076:
$.L= $.L + 1 /*bump the digit count for this number.*/
$.L= $.L + 1 /*bump the digit count for this number.*/
end /*h*/ /*the humble numbers are in the @ array*/
end /*h*/ /*the humble numbers are in the @ array*/
return /* " count results " " " $ " */</lang>
return /* " count results " " " $ " */</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
{{out|output|text=&nbsp; when using the default inputs:}}


Line 5,152: Line 5,152:
=={{header|Ring}}==
=={{header|Ring}}==
{{Improve|Ring|Makes zero attempt at the second part of the task}}
{{Improve|Ring|Makes zero attempt at the second part of the task}}
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 5,176: Line 5,176:
see "" + numList[n] + " "
see "" + numList[n] + " "
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 5,187: Line 5,187:
Checks if each number upto limit is humble number.
Checks if each number upto limit is humble number.
{{trans|Crystal}}
{{trans|Crystal}}
<lang ruby>def humble?(i)
<syntaxhighlight lang="ruby">def humble?(i)
while i % 2 == 0; i /= 2 end
while i % 2 == 0; i /= 2 end
while i % 3 == 0; i /= 3 end
while i % 3 == 0; i /= 3 end
Line 5,209: Line 5,209:


print "\n\nOf the first #{count} humble numbers:\n"
print "\n\nOf the first #{count} humble numbers:\n"
(1..digits).each { |num| printf("%5d have %2d digits\n", humble[num], num) }</lang>
(1..digits).each { |num| printf("%5d have %2d digits\n", humble[num], num) }</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 5,228: Line 5,228:
Generate humble numbers directly.
Generate humble numbers directly.
{{trans|Zkl}}
{{trans|Zkl}}
<lang ruby>def humble(digits)
<syntaxhighlight lang="ruby">def humble(digits)
h = [1]
h = [1]
x2, x3, x5, x7 = 2, 3, 5, 7
x2, x3, x5, x7 = 2, 3, 5, 7
Line 5,252: Line 5,252:
print "First 50 Humble Numbers: \n"; (0...50).each { |i| print "#{h[i]} " }
print "First 50 Humble Numbers: \n"; (0...50).each { |i| print "#{h[i]} " }
print "\n\nOf the first #{count} humble numbers:\n"
print "\n\nOf the first #{count} humble numbers:\n"
(1..digits).each { |num| printf("%6d have %2d digits\n", counts[num], num) }</lang>
(1..digits).each { |num| printf("%6d have %2d digits\n", counts[num], num) }</syntaxhighlight>
{{out}}
{{out}}
<pre>First 50 Humble Numbers:
<pre>First 50 Humble Numbers:
Line 5,310: Line 5,310:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func smooth_generator(primes) {
<syntaxhighlight lang="ruby">func smooth_generator(primes) {


var s = primes.len.of { [1] }
var s = primes.len.of { [1] }
Line 5,338: Line 5,338:
(c, d) = (0, n.len)
(c, d) = (0, n.len)
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 5,368: Line 5,368:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>
<syntaxhighlight lang="tcl">
proc humble? x {
proc humble? x {
foreach f {2 3 5 7} {
foreach f {2 3 5 7} {
Line 5,380: Line 5,380:
}
}
puts $t1
puts $t1
</syntaxhighlight>
</lang>
Task 1:
Task 1:
{{out}}
{{out}}
Line 5,387: Line 5,387:


Task 2, took a long while due to brute force:
Task 2, took a long while due to brute force:
<lang tcl>
<syntaxhighlight lang="tcl">
proc task2 {nmax} {
proc task2 {nmax} {
puts "Distribution of digit length for the first $nmax humble numbers"
puts "Distribution of digit length for the first $nmax humble numbers"
Line 5,400: Line 5,400:
}
}
task2 4096
task2 4096
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>~ $ time ./humble.tcl
<pre>~ $ time ./humble.tcl
Line 5,421: Line 5,421:
=={{header|Visual Basic .NET}}==
=={{header|Visual Basic .NET}}==
{{trans|C#}}
{{trans|C#}}
<lang vbnet>Module Module1
<syntaxhighlight lang="vbnet">Module Module1


Function IsHumble(i As Long) As Boolean
Function IsHumble(i As Long) As Boolean
Line 5,482: Line 5,482:
End Sub
End Sub


End Module</lang>
End Module</syntaxhighlight>
{{out}}
{{out}}
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
<pre>1 2 3 4 5 6 7 8 9 10 12 14 15 16 18 20 21 24 25 27 28 30 32 35 36 40 42 45 48 49 50 54 56 60 63 64 70 72 75 80 81 84 90 96 98 100 105 108 112 120
Line 5,503: Line 5,503:
{{libheader|Wren-sort}}
{{libheader|Wren-sort}}
Wren doesn't have arbitrary precision arithmetic and 'safe' integer operations are limited to a maximum absolute value of 2^53-1 (a 16 digit number). So there is no point in trying to generate humble numbers beyond that.
Wren doesn't have arbitrary precision arithmetic and 'safe' integer operations are limited to a maximum absolute value of 2^53-1 (a 16 digit number). So there is no point in trying to generate humble numbers beyond that.
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt
import "/math" for Int, Nums
import "/math" for Int, Nums
import "/sort" for Find
import "/sort" for Find
Line 5,564: Line 5,564:
var s = (i != 1) ? "s" : ""
var s = (i != 1) ? "s" : ""
System.print("%(Fmt.dc(9, counts[i])) have %(Fmt.d(2, i)) digit%(s)")
System.print("%(Fmt.dc(9, counts[i])) have %(Fmt.d(2, i)) digit%(s)")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 5,593: Line 5,593:
{{trans|Go}}
{{trans|Go}}
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
{{libheader|GMP}} GNU Multiple Precision Arithmetic Library
<lang zkl>var [const] BI=Import("zklBigNum"); // libGMP
<syntaxhighlight lang="zkl">var [const] BI=Import("zklBigNum"); // libGMP
var one = BI(1), two = BI(2), three = BI(3),
var one = BI(1), two = BI(2), three = BI(3),
five = BI(5), seven = BI(7);
five = BI(5), seven = BI(7);
Line 5,610: Line 5,610:
}
}
h
h
}</lang>
}</syntaxhighlight>
<lang zkl>fcn __main__{
<syntaxhighlight lang="zkl">fcn __main__{
const N = 5 * 1e6; // calculate the first 1 million humble numbers, say
const N = 5 * 1e6; // calculate the first 1 million humble numbers, say
h:=humble(N);
h:=humble(N);
Line 5,624: Line 5,624:
println("%2d %,9d".fmt(n,counts[n], n));
println("%2d %,9d".fmt(n,counts[n], n));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre style="height:45ex">
<pre style="height:45ex">