Esthetic numbers: Difference between revisions
m
syntax highlighting fixup automation
Drkameleon (talk | contribs) (Added Arturo implementation) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 41:
{{trans|Nim}}
<
I n == 0 {R 0B}
V i = n % b
Line 104:
listEsths(100'000'000'000, 101'010'101'010, 130'000'000'000, 123'456'789'898, 7, 0B)
listEsths(100'000'000'000'000, 101'010'101'010'101, 130'000'000'000, 123'456'789'898'989, 5, 0B)
listEsths(100'000'000'000'000'000, 101'010'101'010'101'010, 130'000'000'000'000'000, 123'456'789'898'989'898, 4, 0B)</
{{out}}
Line 195:
=={{header|ALGOL 68}}==
As with other solutions here, uses brute-force for the main task, generates the numbers for the stretch goal.
<
# returns TRUE if n is esthetic in the specified base, FALSE otherwise #
PRIO ISESTHETIC = 1;
Line 289:
print( ( newline ) );
print( ( "Found ", whole( e count, 0 ), " esthetic numbers", newline ) )
END</
{{out}}
<pre>
Line 349:
=={{header|Arturo}}==
<
if n=0 -> return false
k: n % b
Line 399:
loop split.every: 16 select 1000..9999 'num -> esthetic? num 10 'row [
print map to [:string] row 'item -> pad item 4
]</
{{out}}
Line 456:
=={{header|C}}==
{{trans|Go}}
<
#include <string.h>
#include <locale.h>
Line 573:
setlocale(LC_NUMERIC, oldLocale);
return 0;
}</
{{out}}
Line 582:
=={{header|C++}}==
{{trans|D}}
<
#include <iostream>
#include <sstream>
Line 701:
listEsths((uint64_t)1e17, 101'010'101'010'101'010, 13 * (uint64_t)1e16, 123'456'789'898'989'898, 4, false);
return 0;
}</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 774:
=={{header|D}}==
{{trans|Go}}
<
import std.stdio;
Line 872:
listEsths(cast(ulong) 1e14, 101_010_101_010_101, 13*cast(ulong) 1e13, 123_456_789_898_989, 5, false);
listEsths(cast(ulong) 1e17, 101_010_101_010_101_010, 13*cast(ulong) 1e16, 123_456_789_898_989_898, 4, false);
}</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 945:
=={{header|F_Sharp|F#}}==
===The functions===
<
// Generate Esthetic Numbers. Nigel Galloway: March 21st., 2020
let rec fN Σ n g = match g with h::t -> match List.head h with
Line 958:
let EtoS n = let g = "0123456789abcdef".ToCharArray()
n |> List.map(fun n->g.[n]) |> List.rev |> Array.ofList |> System.String
</syntaxhighlight>
===The Tasks===
; Esthetic numbers in bases 2 through 16
<
[2..16]|>List.iter(fun n->printfn "\nBase %d" n; Esthetic n|>Seq.skip(4*n-1)|>Seq.take((6-4)*n+1)|>Seq.iter(EtoS >> printfn "%s"))
</syntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 1,284:
; Base 10 esthetic numbers with a magnitude between 1000 and 9999
<
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<1000)|>Seq.takeWhile(fun n->n<9999)|>Seq.iter(printfn "%d");;
</syntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 1,354:
; Base 10 esthetic numbers with a magnitude between 1.0e8 and 1.3e8
<
Esthetic 10|>Seq.map(EtoS>>int)|>Seq.skipWhile(fun n->n<100000000)|>Seq.takeWhile(fun n->n< 130000000)|>Seq.iter(printfn "%d")
</syntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 1,489:
; Base 10 esthetic numbers with a magnitude between 1.0e11 and 1.3e11
<
Esthetic 10|>Seq.map(EtoS>>int64)|>Seq.skipWhile(fun n->n<100000000000L)|>Seq.takeWhile(fun n->n<130000000000L)|>Seq.iter(printfn "%d")
</syntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 2,411:
The <code>bfs</code> word is an adaptation of the algorithm from the Geeks for Geeks reference. It has been changed to work with any base. In summary, this algorithm constructs esthetic numbers directly using a breadth first search. For example, we know the only two esthetic numbers that can be constructed from 23 are 232 and 234, or in other words, the last digit of 23 ± 1 appended to 23. This forms a tree for each digit in a given base where each node is an esthetic number and can have at most two children. This method is very fast and has been used to find the count of esthetic numbers in base 10 between zero and one quadrillion.
{{works with|Factor|0.99 2020-01-23}}
<
locals make math math.order math.parser math.ranges
math.text.english prettyprint sequences sorting strings ;
Line 2,464:
"Count of base 10 esthetic numbers between zero and one quadrillion:"
print 0 1e15 10 esthetics length .</
{{out}}
<pre style="height:60ex">
Line 2,548:
=={{header|Forth}}==
{{works with|Gforth}}
<
\ esthetic number in that base or one less than a power of base.
: next_esthetic_number { n base -- n }
Line 2,589:
main
bye</
{{out}}
Line 2,666:
=={{header|FreeBASIC}}==
<
dim shared as string*16 digits = "0123456789ABCDEF"
Line 2,728:
make_base i, 10, number
if is_esthetic(number) then print number;" ";
next i</
{{out}}
<pre>
Line 2,771:
For the last two parts (stretched up to 1.0e17/1.3e17), a 'depth first search' approach
is used with the obvious range limitations imposed for extra speed.
<
import (
Line 2,880:
listEsths(1e14, 101_010_101_010_101, 13*1e13, 123_456_789_898_989, 5, false)
listEsths(1e17, 101_010_101_010_101_010, 13*1e16, 123_456_789_898_989_898, 4, false)
}</
{{out}}
Line 2,972:
=={{header|Haskell}}==
<
import Control.Monad (replicateM, foldM, mzero)
Line 3,008:
showInBase b = foldMap (pure . digit) . toBase b
where digit = genericIndex (['0'..'9'] <> ['a'..'z'])
</syntaxhighlight>
'''Tasks implementation'''
Line 3,102:
Implementation (brute force):
<
gen=: {{r=.$k=.1 while.y>#r do. r=.r,k#~u k
k=.1+({:k)+i.2*#k end.y{.r}}</
Task examples:
<
taskB=: {{;:inv y tobase&.> (<:4*y)}. y&isesthetic gen 6*y}}
Line 3,144:
(#~ isesthetic) 1000+i.1000
1010 1012 1210 1212 1232 1234</
Stretch goal (the slow way):
<
126
q:126
2 3 3 7</
(Result e not displayed here -- it's the same as <tt>next^:8]1</tt> below.)
Line 3,159:
For example, we can generate a directed graph, from one digit to the one or two viable adjacent digits, and then build up a result based on all viable values of the rightmost digit of the current list of partially built candidates:
<
next=: [:; (0 10#.],.graph {::~10|])each
Line 3,180:
123432323 123432343 123432345 123434321 123434323 123434343 123434345 123434543 123434545
123434565 123434567 123454321 123454323 123454343 123454345 123454543 123454545 123454565
123454567 123456543 123456545 123456565 123456567 123456765 123456767 123456787 123456789</
(We organize the 126 values of the stretch goal into 14 rows of 9 columns (126=14*9) using the expression <tt>14 9 $</tt> ...)
Line 3,186:
=={{header|Java}}==
{{trans|Kotlin}}
<
import java.util.stream.IntStream;
import java.util.stream.LongStream;
Line 3,291:
listEsths((long) 1e17, 101_010_101_010_101_010L, 13 * (long) 1e16, 123_456_789_898_989_898L, 4, false);
}
}</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 3,364:
=={{header|JavaScript}}==
===JavaScript :: Procedural===
<
let arr = inp.toString(base).split('');
if (arr.length == 1) return false;
Line 3,402:
console.log( collectEsthetics(10, [1e8, 1.3e8]),
(new Date() - d) / 1000 + ' s' );</
{{out}}<pre>2:
> Array(4) [ "1010101010", "10101010101", "101010101010", "1010101010101" ]
Line 3,517:
Constrained generation – more efficient than a filter over a larger space.
{{Trans|Haskell}}
<
"use strict";
Line 3,798:
// MAIN ---
return main();
})();</
{{Out}}
<pre>Esthetics [8..12] for base 2:
Line 3,904:
=={{header|Julia}}==
Illustrates both brute force and iterative methods of generating numbers in the sequence.
<
import Base.iterate, Base.IteratorSize, Base.IteratorEltype
Line 4,011:
println("\nTotal esthetic numbers in interval: $count")
end
</
<pre>
For base 2, the esthetic numbers indexed from 8 to 12 are:
Line 4,143:
=={{header|Kotlin}}==
{{trans|D}}
<
fun isEsthetic(n: Long, b: Long): Boolean {
Line 4,240:
listEsths(1e14.toLong(), 101_010_101_010_101, 13 * 1e13.toLong(), 123_456_789_898_989, 5, false);
listEsths(1e17.toLong(), 101_010_101_010_101_010, 13 * 1e16.toLong(), 123_456_789_898_989_898, 4, false);
}</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 4,313:
=={{header|Lua}}==
{{trans|C++}}
<
local BASE = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Line 4,423:
listEsths(1e11, 101010101010, 13 * 1e10, 123456789898, 7, false)
listEsths(1e14, 101010101010101, 13 * 1e13, 123456789898989, 5, false)
listEsths(1e17, 101010101010101010, 13 * 1e16, 123456789898989898, 4, false)</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 4,495:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<
EstheticNumbersRangeHelper[power_, mima : {mi_, max_}, b_ : 10] := Module[{steps, cands},
steps = Tuples[{-1, 1}, power - 1];
Line 4,515:
Table[{b, EstheticNumbersRange[{1, If[b == 2, 100000, If[b == 3, 100000, b^4]]}, b][[4 b ;; 6 b]]}, {b, 2, 16}] // Grid
EstheticNumbersRange[{1000, 9999}]
EstheticNumbersRange[{10^8, 1.3 10^8}]</
{{out}}
<pre>2 {10101010,101010101,1010101010,10101010101,101010101010}
Line 4,539:
=={{header|Nim}}==
{{trans|Kotlin}}
<
func isEsthetic(n, b: int64): bool =
Line 4,620:
listEsths(100_000_000_000, 101_010_101_010, 130_000_000_000, 123_456_789_898, 7, false)
listEsths(100_000_000_000_000, 101_010_101_010_101, 130_000_000_000, 123_456_789_898_989, 5, false)
listEsths(100_000_000_000_000_000, 101_010_101_010_101_010, 130_000_000_000_000_000, 123_456_789_898_989_898, 4, false)</
{{out}}
Line 4,708:
=={{header|Pascal}}==
Simple brute force, but fast and simple counting for complete first digit ranges.
<
{$IFDEF FPC}
{$MODE DELPHI} {$OPTIMIZATION ON,ALL} {$codealign proc=16}
Line 4,908:
writeln(Numb2USA(IntToStr(Dgtcnt[64][0]-Dgtcnt[63][0])):28);
end.
</syntaxhighlight>
{{out}}
<pre style="height:30ex">
Line 5,006:
{{libheader|ntheory}}
{{trans|Sidef}}
<
use warnings;
use experimental qw(signatures);
Line 5,055:
for (my @list = between_esthetic(1e8, 1.3e8) ; @list ;) {
say join(' ', splice(@list, 0, 9));
}</
{{out}}
<pre>
Line 5,128:
=={{header|Phix}}==
Simple string based approach, very fast, manages stretch goal and much further in the blink of an eye.
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">aleph</span> <span style="color: #0000FF;">=</span> <span style="color: #008000;">"0123456789ABCDEF"</span>
Line 5,198:
<span style="color: #0000FF;">{</span><span style="color: #000000;">comma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">f</span><span style="color: #0000FF;">),</span><span style="color: #000000;">comma</span><span style="color: #0000FF;">(</span><span style="color: #000000;">t</span><span style="color: #0000FF;">),</span><span style="color: #000000;">res</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 5,231:
In other words we can count the total number of 62-digit esthetic numbers with at most 574 additions from a
table of at most 610 entries, in practice obviously just do all 610.
<!--<
<span style="color: #008080;">function</span> <span style="color: #000000;">count_esthetic</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">len</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
<span style="color: #000080;font-style:italic;">-- a start digit of 0 means sum all 1..9</span>
Line 5,251:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,26d esthetic numbers with max 65 digits\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">sum</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">64</span><span style="color: #0000FF;">),</span><span style="color: #000000;">count_esthetic</span><span style="color: #0000FF;">))})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</
<small>(It turns out that [1..9] is the same as [2..10] anyway, by symmetry, but technically the latter would be the more correct expression for digits 1..9)</small>
{{out}}
Line 5,280:
=={{header|PicoLisp}}==
<
(let Lst
(make
Line 5,326:
(while (cut 9 'L)
(mapc '((L) (prin L " ")) @)
(prinl) ) )</
{{out}}
Line 5,371:
=={{header|Prolog}}==
{{works with|SWI Prolog}}
<
forall(between(2, 16, Base),
(Min_index is Base * 4, Max_index is Base * 6,
Line 5,424:
D is C mod Base,
(D == 0 -> E = 1 ; E is D - 1),
N is C * Base + E).</
{{out}}
Line 5,503:
===Python :: Procedural===
{{works with|Python|3.9.5}}
<
from itertools import dropwhile, islice, takewhile
from textwrap import wrap
Line 5,590:
task_3(100_000_000, 130_000_000)
</syntaxhighlight>
{{out}}
<pre>======
Line 5,708:
===Python :: Functional===
{{Trans|Haskell}}
<
from functools import reduce
Line 5,963:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Esthetics [8..12] for base 2:
Line 6,056:
{{works with|Rakudo|2020.02}}
<syntaxhighlight lang="raku"
sub esthetic($base = 10) {
Line 6,082:
my $top = @array.first: * > $hi, :k;
@array[^$top].grep: * > $lo
}</
{{out}}
<div style="font-size:70%;">
Line 6,163:
=={{header|REXX}}==
<
parse arg baseL baseH range /*obtain optional arguments from the CL*/
if baseL=='' | baseL=="," then baseL= 2 /*Not specified? Then use the default.*/
Line 6,223:
if (z==vv(@.pe,-1)|z==vv(@.pe,1))&(z==vv(@.ae,-1)|z==vv(@.ae,1)) then iterate
return 0
end /*e*/; return 1</
{{out|output|text= when using the default inputs:}}
<pre>
Line 6,280:
=={{header|Ring}}==
<
basePlus = []
decList = [0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15]
Line 6,345:
binList = substr(binList,nl,"")
return binList
</syntaxhighlight>
{{out}}
<pre>
Line 6,402:
=={{header|Ruby}}==
{{trans|Kotlin}}
<
if n == 0 then
return false
Line 6,494:
end
main()</
{{out}}
<pre>Base 2: 8th to 12th esthetic numbers:
Line 6,566:
=={{header|Rust}}==
<
// radix_fmt = "1.0"
Line 6,666:
println!();
}
}</
{{out}}
Line 6,759:
=={{header|Sidef}}==
<
var v = root.digits2num(b)
Line 6,796:
say "\nBase 10 esthetic numbers between 100,000,000 and 130,000,000:"
between_esthetic(1e8, 13e7).slices(9).each { .join(' ').say }</
{{out}}
<pre>
Line 6,869:
=={{header|Swift}}==
<
func take(_ n: Int) -> [Element] {
var res = [Element]()
Line 7,000:
print("Esthetics between \(Int(1e17)) and \(13 * Int(1e16)):")
printSlice(of: getEsthetics(from: Int(1e17), to: 13 * Int(1e16)))</
{{out}}
Line 7,079:
{{trans|Go}}
{{libheader|Wren-fmt}}
<
var isEsthetic = Fn.new { |n, b|
Line 7,151:
listEsths.call(1e8, 101010101, 13*1e7, 123456789, 9, true)
listEsths.call(1e11, 101010101010, 13*1e10, 123456789898, 7, false)
listEsths.call(1e14, 101010101010101, 13*1e13, 123456789898989, 5, false)</
{{out}}
|