Combinations with repetitions/Square digit chain: Difference between revisions

m
m (→‎{{header|Phix}}: added a translation of Wren)
m (→‎{{header|Wren}}: Minor tidy)
 
(2 intermediate revisions by 2 users not shown)
Line 13:
=={{header|D}}==
{{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.
//
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>
</lang>
{{out}}
<pre>
Line 138:
=={{header|Go}}==
{{trans|Kotlin}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 191:
fmt.Println(count1, "numbers produce 1 and", limit-count1, "numbers produce 89\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 221:
of gojq would be required. The output shown below is taken from a gojq run.
 
<langsyntaxhighlight lang="jq"># For gojq:
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"</langsyntaxhighlight>
{{out}}
<pre>
Line 278:
 
=={{header|Julia}}==
<langsyntaxhighlight lang="julia">using Combinatorics
 
function iterate(m::Integer)
Line 313:
testitersquares(i)
end
</langsyntaxhighlight>{{out}}
<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.
<langsyntaxhighlight lang="scala">// version 1.1.51
 
fun endsWithOne(n: Int): Boolean {
Line 379:
println("$count1 numbers produce 1 and ${limit - count1} numbers produce 89\n")
}
}</langsyntaxhighlight>
 
{{out}}
Line 401:
=={{header|Nim}}==
{{trans|Kotlin}}
<langsyntaxhighlight Nimlang="nim">import math, strformat
 
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"</langsyntaxhighlight>
 
{{out}}
Line 452:
=={{header|Perl}}==
{{trans|Raku}}
<langsyntaxhighlight lang="perl">use strict;
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";
}</langsyntaxhighlight>
{{out}}
<pre>For k = 7 in the range 1 to 9,999,999
Line 515:
=={{header|Phix}}==
{{trans|Wren}}
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 586:
12,024,696,404,768,024 numbers produce 1 and 87,975,303,595,231,975 numbers produce 89
</pre>
Also, [[Iterated_digits_squaring#Combinatorics_version|Iterated_digits_squaringPhix]] produces some of the same numbers (just not so high).
 
=={{header|Raku}}==
(formerly Perl 6)
{{trans|Kotlin}}
<syntaxhighlight lang="raku" perl6line>use v6;
 
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";
}</langsyntaxhighlight>
 
{{out}}
Line 644:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="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>
</lang>
{{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.
<langsyntaxhighlight ecmascriptlang="wren">import "./big" for BigInt
 
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")
}</langsyntaxhighlight>
 
{{out}}
Line 748:
=={{header|zkl}}==
{{trans|Ruby}}
<langsyntaxhighlight lang="zkl">fcn countNumberChains(K){
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
}</langsyntaxhighlight>
combosKW(k,sequence) is lazy, which, in this case, is quite a bit faster than the non-lazy version.
<langsyntaxhighlight lang="zkl">foreach K in (T(7,8,11,14,17)){ countNumberChains(K) }</langsyntaxhighlight>
{{out}}
<pre>
9,476

edits