Combinations with repetitions/Square digit chain: Difference between revisions

Content added Content deleted
m (→‎{{header|Phix}}: corrected link)
m (syntax highlighting fixup automation)
Line 13: Line 13:
=={{header|D}}==
=={{header|D}}==
{{improve|D|See talk page}}
{{improve|D|See talk page}}
<syntaxhighlight lang="d">
<lang d>
// Count how many number chains for Natural Numbers < 10**K end with a value of 1.
// Count how many number chains for Natural Numbers < 10**K end with a value of 1.
//
//
Line 117: Line 117:
writefln ("\n(k=%d) In the range 1 to %d\n%d translate to 1 and %d translate to 89\n", K, (cast (ulong) (10))^^K-1,z,(cast (ulong) (10))^^K-1-z);
writefln ("\n(k=%d) In the range 1 to %d\n%d translate to 1 and %d translate to 89\n", K, (cast (ulong) (10))^^K-1,z,(cast (ulong) (10))^^K-1-z);
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 138: Line 138:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 191: Line 191:
fmt.Println(count1, "numbers produce 1 and", limit-count1, "numbers produce 89\n")
fmt.Println(count1, "numbers produce 1 and", limit-count1, "numbers produce 89\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 221: Line 221:
of gojq would be required. The output shown below is taken from a gojq run.
of gojq would be required. The output shown below is taken from a gojq run.


<lang jq># For gojq:
<syntaxhighlight lang="jq"># For gojq:
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);


Line 258: Line 258:
| ((10|power($k)) - 1) as $limit
| ((10|power($k)) - 1) as $limit
| "For k = \($k) in the range 1 to \($limit)",
| "For k = \($k) in the range 1 to \($limit)",
"\(.count1) numbers produce 1 and \($limit - .count1) numbers produce 89.\n"</lang>
"\(.count1) numbers produce 1 and \($limit - .count1) numbers produce 89.\n"</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 278: Line 278:


=={{header|Julia}}==
=={{header|Julia}}==
<lang julia>using Combinatorics
<syntaxhighlight lang="julia">using Combinatorics


function iterate(m::Integer)
function iterate(m::Integer)
Line 313: Line 313:
testitersquares(i)
testitersquares(i)
end
end
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
For k = 2, in the range 1 to 99,
For k = 2, in the range 1 to 99,
Line 338: Line 338:


So the following generalizes that code to deal with values of k up to 17 (which requires 64 bit integers) and to count numbers where the squared digits sum sequence eventually ends in 1 rather than 89, albeit the sum of both must of course be 10 ^ k - 1.
So the following generalizes that code to deal with values of k up to 17 (which requires 64 bit integers) and to count numbers where the squared digits sum sequence eventually ends in 1 rather than 89, albeit the sum of both must of course be 10 ^ k - 1.
<lang scala>// version 1.1.51
<syntaxhighlight lang="scala">// version 1.1.51


fun endsWithOne(n: Int): Boolean {
fun endsWithOne(n: Int): Boolean {
Line 379: Line 379:
println("$count1 numbers produce 1 and ${limit - count1} numbers produce 89\n")
println("$count1 numbers produce 1 and ${limit - count1} numbers produce 89\n")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 401: Line 401:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang Nim>import math, strformat
<syntaxhighlight lang="nim">import math, strformat


func endsWithOne(n: Natural): bool =
func endsWithOne(n: Natural): bool =
Line 432: Line 432:
let limit = 10^k - 1
let limit = 10^k - 1
echo &"For k = {k} in the range 1 to {limit}"
echo &"For k = {k} in the range 1 to {limit}"
echo &"{count1} numbers produce 1 and {limit - count1} numbers produce 89\n"</lang>
echo &"{count1} numbers produce 1 and {limit - count1} numbers produce 89\n"</syntaxhighlight>


{{out}}
{{out}}
Line 452: Line 452:
=={{header|Perl}}==
=={{header|Perl}}==
{{trans|Raku}}
{{trans|Raku}}
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 496: Line 496:
say "For k = $k in the range 1 to " . comma $limit;
say "For k = $k in the range 1 to " . comma $limit;
say comma($count1) . ' numbers produce 1 and ' . comma($limit-$count1) . " numbers produce 89\n";
say comma($count1) . ' numbers produce 1 and ' . comma($limit-$count1) . " numbers produce 89\n";
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>For k = 7 in the range 1 to 9,999,999
<pre>For k = 7 in the range 1 to 9,999,999
Line 515: Line 515:
=={{header|Phix}}==
=={{header|Phix}}==
{{trans|Wren}}
{{trans|Wren}}
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<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>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
<span style="color: #008080;">include</span> <span style="color: #004080;">mpfr</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 568: Line 568:
<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;">"%s numbers produce 1 and %s numbers produce 89\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<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;">"%s numbers produce 1 and %s numbers produce 89\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">c</span><span style="color: #0000FF;">,</span><span style="color: #000000;">s</span><span style="color: #0000FF;">})</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 591: Line 591:
(formerly Perl 6)
(formerly Perl 6)
{{trans|Kotlin}}
{{trans|Kotlin}}
<lang perl6>use v6;
<syntaxhighlight lang="raku" line>use v6;


sub endsWithOne($n --> Bool) {
sub endsWithOne($n --> Bool) {
Line 629: Line 629:
say "For k = $k in the range 1 to $limit";
say "For k = $k in the range 1 to $limit";
say "$count1 numbers produce 1 and ",$limit-$count1," numbers produce 89";
say "$count1 numbers produce 1 and ",$limit-$count1," numbers produce 89";
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 644: Line 644:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>
<syntaxhighlight lang="ruby">
# Count how many number chains for Natural Numbers < 10**K end with a value of 1.
# Count how many number chains for Natural Numbers < 10**K end with a value of 1.
#
#
Line 667: Line 667:
}
}
puts "\nk=(#{K}) in the range 1 to #{10**K-1}\n#{z} numbers produce 1 and #{10**K-1-z} numbers produce 89"
puts "\nk=(#{K}) in the range 1 to #{10**K-1}\n#{z} numbers produce 1 and #{10**K-1-z} numbers produce 89"
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 690: Line 690:
{{libheader|Wren-big}}
{{libheader|Wren-big}}
As Wren doesn't have 64 bit integers, it is necessary to use BigInt here to process k = 17.
As Wren doesn't have 64 bit integers, it is necessary to use BigInt here to process k = 17.
<lang ecmascript>import "/big" for BigInt
<syntaxhighlight lang="ecmascript">import "/big" for BigInt


var endsWithOne = Fn.new { |n|
var endsWithOne = Fn.new { |n|
Line 726: Line 726:
System.print("For k = %(k) in the range 1 to %(limit)")
System.print("For k = %(k) in the range 1 to %(limit)")
System.print("%(count1) numbers produce 1 and %(limit - count1) numbers produce 89\n")
System.print("%(count1) numbers produce 1 and %(limit - count1) numbers produce 89\n")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 748: Line 748:
=={{header|zkl}}==
=={{header|zkl}}==
{{trans|Ruby}}
{{trans|Ruby}}
<lang zkl>fcn countNumberChains(K){
<syntaxhighlight lang="zkl">fcn countNumberChains(K){
F:=(K+1).pump(List,fcn(n){ (1).reduce(n,'*,1) }); #Some small factorials
F:=(K+1).pump(List,fcn(n){ (1).reduce(n,'*,1) }); #Some small factorials
g:=fcn(n){
g:=fcn(n){
Line 769: Line 769:
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
z
z
}</lang>
}</syntaxhighlight>
combosKW(k,sequence) is lazy, which, in this case, is quite a bit faster than the non-lazy version.
combosKW(k,sequence) is lazy, which, in this case, is quite a bit faster than the non-lazy version.
<lang zkl>foreach K in (T(7,8,11,14,17)){ countNumberChains(K) }</lang>
<syntaxhighlight lang="zkl">foreach K in (T(7,8,11,14,17)){ countNumberChains(K) }</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>