Combinations with repetitions/Square digit chain: Difference between revisions
Combinations with repetitions/Square digit chain (view source)
Revision as of 11:35, 20 November 2023
, 5 months ago→{{header|Wren}}: Minor tidy
m (→{{header|Phix}}: corrected link) |
m (→{{header|Wren}}: Minor tidy) |
||
(One intermediate revision by one other user not shown) | |||
Line 13:
=={{header|D}}==
{{improve|D|See talk page}}
<syntaxhighlight lang="d">
// Count how many number chains for Natural Numbers < 10**K end with a value of 1.
//
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);
}
</syntaxhighlight>
{{out}}
<pre>
Line 138:
=={{header|Go}}==
{{trans|Kotlin}}
<
import (
Line 191:
fmt.Println(count1, "numbers produce 1 and", limit-count1, "numbers produce 89\n")
}
}</
{{out}}
Line 221:
of gojq would be required. The output shown below is taken from a gojq run.
<
def power($b): . as $in | reduce range(0;$b) as $i (1; . * $in);
Line 258:
| ((10|power($k)) - 1) as $limit
| "For k = \($k) in the range 1 to \($limit)",
"\(.count1) numbers produce 1 and \($limit - .count1) numbers produce 89.\n"</
{{out}}
<pre>
Line 278:
=={{header|Julia}}==
<
function iterate(m::Integer)
Line 313:
testitersquares(i)
end
</
<pre>
For k = 2, in the range 1 to 99,
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.
<
fun endsWithOne(n: Int): Boolean {
Line 379:
println("$count1 numbers produce 1 and ${limit - count1} numbers produce 89\n")
}
}</
{{out}}
Line 401:
=={{header|Nim}}==
{{trans|Kotlin}}
<
func endsWithOne(n: Natural): bool =
Line 432:
let limit = 10^k - 1
echo &"For k = {k} in the range 1 to {limit}"
echo &"{count1} numbers produce 1 and {limit - count1} numbers produce 89\n"</
{{out}}
Line 452:
=={{header|Perl}}==
{{trans|Raku}}
<
use warnings;
use feature 'say';
Line 496:
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";
}</
{{out}}
<pre>For k = 7 in the range 1 to 9,999,999
Line 515:
=={{header|Phix}}==
{{trans|Wren}}
<!--<
<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>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</
{{out}}
<pre>
Line 591:
(formerly Perl 6)
{{trans|Kotlin}}
<syntaxhighlight lang="raku"
sub endsWithOne($n --> Bool) {
Line 629:
say "For k = $k in the range 1 to $limit";
say "$count1 numbers produce 1 and ",$limit-$count1," numbers produce 89";
}</
{{out}}
Line 644:
=={{header|Ruby}}==
<
# Count how many number chains for Natural Numbers < 10**K end with a value of 1.
#
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"
</syntaxhighlight>
{{out}}
<pre>
Line 690:
{{libheader|Wren-big}}
As Wren doesn't have 64 bit integers, it is necessary to use BigInt here to process k = 17.
<
var endsWithOne = Fn.new { |n|
Line 726:
System.print("For k = %(k) in the range 1 to %(limit)")
System.print("%(count1) numbers produce 1 and %(limit - count1) numbers produce 89\n")
}</
{{out}}
Line 748:
=={{header|zkl}}==
{{trans|Ruby}}
<
F:=(K+1).pump(List,fcn(n){ (1).reduce(n,'*,1) }); #Some small factorials
g:=fcn(n){
Line 769:
println("%,d numbers produce 1 and %,d numbers produce 89".fmt(z,(10).pow(K)-1-z));
z
}</
combosKW(k,sequence) is lazy, which, in this case, is quite a bit faster than the non-lazy version.
<
{{out}}
<pre>
|