Casting out nines: Difference between revisions

m
syntax highlighting fixup automation
m (→‎{{header|ALGOL 68}}: another typo...)
m (syntax highlighting fixup automation)
Line 32:
{{trans|Python}}
 
<langsyntaxhighlight lang=11l>F CastOut(Base, Start, End)
V ran = (0 .< Base - 1).filter(y -> y % (@Base - 1) == (y * y) % (@Base - 1))
V (x, y) = divmod(Start, Base - 1)
Line 54:
L(v) CastOut(Base' 17, Start' 1, End' 288)
print(v, end' ‘ ’)
print()</langsyntaxhighlight>
 
{{out}}
Line 66:
{{trans|REXX}}
The program uses two ASSIST macros (XDECO,XPRNT) to keep the code as short as possible.
<langsyntaxhighlight lang=360asm>* Casting out nines 08/02/2017
CASTOUT CSECT
USING CASTOUT,R13 base register
Line 129:
XDEC DS CL12 temp for xdeco
YREGS
END CASTOUT</langsyntaxhighlight>
{{out}}
<pre>
Line 142:
 
=={{header|Action!}}==
<langsyntaxhighlight lang=Action!>INT FUNC Power(INT a,b)
INT i,res
 
Line 169:
perc=100-100*count/total
PrintF("%E%ETrying %I numbers instead of %I numbers saves %I%%",count,total,perc)
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Casting_out_nines.png Screenshot from Atari 8-bit computer]
Line 180:
=={{header|ALGOL 68}}==
{{Trans|Action!}}
<langsyntaxhighlight lang=algol68>BEGIN # casting out nines - translated from the Action! sample #
INT base = 10;
INT n = 2;
Line 198:
)
)
END</langsyntaxhighlight>
{{out}}
<pre>
Line 208:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang=rebol>N: 2
base: 10
c1: 0
Line 223:
 
print ""
print ["Trying" c2 "numbers instead of" c1 "numbers saves" 100.0 - 100.0*c2//c1 "%"]</langsyntaxhighlight>
 
{{out}}
Line 231:
 
=={{header|AWK}}==
<langsyntaxhighlight lang=AWK>
# syntax: GAWK -f CASTING_OUT_NINES.AWK
# converted from C
Line 246:
exit(0)
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 255:
=={{header|C}}==
{{trans|C++}}
<langsyntaxhighlight lang=c>#include <stdio.h>
#include <math.h>
 
Line 275:
printf("\nTring %d numbers instead of %d numbers saves %f%%\n", c2, c1, 100.0 - 100.0 * c2 / c1);
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
Line 282:
=={{header|C++}}==
===Filter===
<langsyntaxhighlight lang=cpp>// Casting Out Nines
//
// Nigel Galloway. June 24th., 2012
Line 301:
std::cout << "\nTrying " << c2 << " numbers instead of " << c1 << " numbers saves " << 100 - ((double)c2/c1)*100 << "%" <<std::endl;
return 0;
}</langsyntaxhighlight>
{{out|Produces}}
<pre>
Line 348:
in this range are included.
===C++11 For Each Generator===
<langsyntaxhighlight lang=cpp>// Casting Out Nines Generator - Compiles with gcc4.6, MSVC 11, and CLang3
//
// Nigel Galloway. June 24th., 2012
Line 391:
for (int i : co9(1,99,&r)) { std::cout << i << ' '; }
return 0;
}</langsyntaxhighlight>
{{out|Produces}}
<pre>
Line 397:
</pre>
An alternative implementation for struct ran using http://rosettacode.org/wiki/Sum_digits_of_an_integer#C.2B.2B which produces the same result is:
<langsyntaxhighlight lang=cpp>struct ran {
const int base;
std::vector<int> rs;
ran(const int base) : base(base) { for (int nz=0; nz<base-1; nz++) if(SumDigits(nz) == SumDigits(nz*nz)) rs.push_back(nz); }
};</langsyntaxhighlight>
Changing main:
<langsyntaxhighlight lang=cpp>int main() {
ran r(16);
for (int i : co9(1,255,&r)) { std::cout << i << ' '; }
return 0;
}</langsyntaxhighlight>
{{out|Produces}}
<pre>
Line 413:
</pre>
Changing main:
<langsyntaxhighlight lang=cpp>int main() {
ran r(17);
for (int i : co9(1,288,&r)) { std::cout << i << ' '; }
return 0;
}</langsyntaxhighlight>
{{out|Produces}}
<pre>
Line 425:
=={{header|C sharp|C#}}==
{{trans|Java}}
<langsyntaxhighlight lang=csharp>using System;
using System.Collections.Generic;
using System.Linq;
Line 481:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60, 61, 66, 70, 75, 76, 81, 85, 90, 91, 96, 100, 105, 106, 111, 115, 120, 121, 126, 130, 135, 136, 141, 145, 150, 151, 156, 160, 165, 166, 171, 175, 180, 181, 186, 190, 195, 196, 201, 205, 210, 211, 216, 220, 225, 226, 231, 235, 240, 241, 246, 250, 255]
Line 488:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang=lisp>;;A macro was used to ensure that the filter is inlined.
;;Larry Hignight. Last updated on 7/3/2012.
(defmacro kaprekar-number-filter (n &optional (base 10))
Line 502:
(format t "~d potential Kaprekar numbers remain (~~~$% filtered out).~%"
count (* (/ (- stop count) stop) 100))
(if collect (reverse nums))))</langsyntaxhighlight>
{{out}}
<pre>CL-USER> (test :stop 99)
Line 522:
=={{header|D}}==
{{trans|Python}}
<langsyntaxhighlight lang=d>import std.stdio, std.algorithm, std.range;
 
uint[] castOut(in uint base=10, in uint start=1, in uint end=999999) {
Line 548:
castOut(10, 1, 99).writeln;
castOut(17, 1, 288).writeln;
}</langsyntaxhighlight>
{{out|Output (some newlines added)}}
<pre>[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60,
Line 563:
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang=freebasic>Const base10 = 10
Dim As Integer c1 = 0, c2 = 0, k = 1
 
Line 573:
Print
Print Using "Intentar ## numeros en lugar de ### numeros ahorra un ##.##%"; c2; c1; 100-(100*c2/c1)
Sleep</langsyntaxhighlight>
{{out}}
<pre>
Line 582:
 
=={{header|Free Pascal}}==
<langsyntaxhighlight lang=pascal>program castout9;
{$ifdef fpc}{$mode delphi}{$endif}
uses generics.collections;
Line 623:
co9(1, 10, 99, [1,9,45,55,99]);
co9(1, 10, 1000, [1,9,45,55,99,297,703,999]);
end.</langsyntaxhighlight>
<pre>
Output:
Line 638:
 
=={{header|Go}}==
<langsyntaxhighlight lang=go>package main
 
import (
Line 805:
fmt.Println("Valid subset.")
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 821:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang=haskell>co9 n
| n <= 8 = n
| otherwise = co9 $ sum $ filter (/= 9) $ digits 10 n
Line 827:
task2 = filter (\n -> co9 n == co9 (n ^ 2)) [1 .. 100]
 
task3 k = filter (\n -> n `mod` k == n ^ 2 `mod` k) [1 .. 100]</langsyntaxhighlight>
 
Auxillary function, returning digits of a number for given base
<langsyntaxhighlight lang=haskell>digits base = map (`mod` base) . takeWhile (> 0) . iterate (`div` base)</langsyntaxhighlight>
 
or using unfolding:
 
<langsyntaxhighlight lang=haskell>digits base = Data.List.unfoldr modDiv
where modDiv 0 = Nothing
modDiv n = let (q, r) = (n `divMod` base) in Just (r, q)</langsyntaxhighlight>
'''Output'''
Line 867:
=={{header|J}}==
This is an implementation of: "given two numbers which mark the beginning and end of a range of integers, and another number which denotes an integer base, return numbers from within the range where the number is equal (modulo the base minus 1) to its square". At the time of this writing, this task is a draft task and this description does not precisely match the task description on this page. Eventually, either the task description will change to match this implementation (which means this paragraph should be removed) or the task description will change to conflict with this implementation (so this entire section should be re-written).
<langsyntaxhighlight lang=J>castout=: 1 :0
[: (#~ ] =&((m-1)&|) *:) <. + [: i. (+*)@-~
)</langsyntaxhighlight>
Example use:
<langsyntaxhighlight lang=J> 0 (10 castout) 100
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</langsyntaxhighlight>
Alternate implementation:
<langsyntaxhighlight lang=J>castout=: 1 :0
[: (#~ 0 = (m-1) | 0 _1 1&p.) <. + [: i. (+*)@-~
)</langsyntaxhighlight>
Note that about half of the code here is the code that implements "range of numbers". If we factor that out, and represent the desired values directly the code becomes much simpler:
<langsyntaxhighlight lang=J> (#~ 0=9|0 _1 1&p.) i.101
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100
(#~ ] =&(9&|) *:) i. 101
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</langsyntaxhighlight>
And, of course, we can name parts of these expressions. For example:
<langsyntaxhighlight lang=J> (#~ ] =&(co9=: 9&|) *:) i. 101
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</langsyntaxhighlight>
Or, if you prefer:
<langsyntaxhighlight lang=J>co9=: 9&|
(#~ ] =&co9 *:) i. 101
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</langsyntaxhighlight>
 
=={{header|Java}}==
{{trans|D}}
{{works with|Java|8}}
<langsyntaxhighlight lang=java>import java.util.*;
import java.util.stream.IntStream;
 
Line 925:
}
}
}</langsyntaxhighlight>
 
<pre>[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60, 61, 66,
Line 938:
===ES5===
Assuming the context of a web page:
<langsyntaxhighlight lang=JavaScript>function main(s, e, bs, pbs) {
bs = bs || 10;
pbs = pbs || 10
Line 984:
main(1, 16 * 16 - 1, 16)
main(1, 17 * 17 - 1, 17)
main(parseInt('10', 17), parseInt('gg', 17), 17, 17)</langsyntaxhighlight>
{{Out}}
<pre>start:1 end:99 base:10 printBase:10
Line 1,009:
===ES6===
{{Trans|Haskell}}
<langsyntaxhighlight lang=JavaScript>(() => {
'use strict';
 
Line 1,054:
.filter(n => (n % k) === (squared(n) % k)))(16)
});
})();</langsyntaxhighlight>
{{Out}}
<pre>{
Line 1,110:
 
'''Definition of co9''':
<langsyntaxhighlight lang=jq>def co9:
def digits: tostring | explode | map(. - 48); # "0" is 48
if . == 9 then 0
elif 0 <= . and . <= 8 then .
else digits | add | co9
end;</langsyntaxhighlight>
 
For convenience, we also define a function to check whether co9(i) equals co9(i*i)
for a given integer, i:
<langsyntaxhighlight lang=jq>def co9_equals_co9_squared: co9 == ((.*.)|co9);</langsyntaxhighlight>
 
'''Example''':
Line 1,125:
Integers in 1 .. 100 satisfying co9(i) == co9(i*i):
 
<langsyntaxhighlight lang=jq>[range (1;101) | select( co9_equals_co9_squared )</langsyntaxhighlight>
produces:
[1,9,10,18,19,27,28,36,37,45,46,54,55,63,64,72,73,81,82,90,91,99,100]
Line 1,134:
co9_equals_co9_squared condition is by inspection. For the range 1..100 considered above, we have:
 
<langsyntaxhighlight lang=jq>[ range(1;101) | select(is_kaprekar) ]</langsyntaxhighlight>
 
[1,9,45,55,99]
Line 1,140:
To check the condition programmatically for a given range of integers, we can
define a function which will emit any exceptions, e.g.
<langsyntaxhighlight lang=jq>def verify:
range(1; .)
| select(is_kaprekar and (co9_equals_co9_squared | not));</langsyntaxhighlight>
 
For example, running (1000 | verify) produces an empty stream.
Line 1,151:
proportion of integers, i, in 1 .. n such that i % (b-1) == (i*i) % (b-1):
 
<langsyntaxhighlight lang=jq>def proportion(base):
def count(stream): reduce stream as $i (0; . + 1);
. as $n
| (base - 1) as $b
| count( range(1; 1+$n) | select( . % $b == (.*.) % $b) ) / $n ;</langsyntaxhighlight>
For example:
 
Line 1,161:
 
produces:
<langsyntaxhighlight lang=sh>0.3
0.27
0.267
0.2667
0.26667</langsyntaxhighlight>
 
=={{header|Julia}}==
<langsyntaxhighlight lang=Julia>co9(x) = x == 9 ? 0 :
1<=x<=8 ? x :
co9(sum(digits(x)))</langsyntaxhighlight>
iskaprekar is defined in the task
[[Kaprekar_numbers#Julia]].
Line 1,185:
=={{header|Kotlin}}==
{{trans|D}}
<langsyntaxhighlight lang=scala>// version 1.1.3
 
fun castOut(base: Int, start: Int, end: Int): List<Int> {
Line 1,209:
println()
println(castOut(17, 1, 288))
}</langsyntaxhighlight>
 
{{out}}
Line 1,222:
=={{header|Lua}}==
{{trans|C}}
<langsyntaxhighlight lang=lua>local N = 2
local base = 10
local c1 = 0
Line 1,236:
 
print()
print(string.format("Trying %d numbers instead of %d numbers saves %f%%", c2, c1, 100.0 - 100.0 * c2 / c1))</langsyntaxhighlight>
{{out}}
<pre>1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
Line 1,243:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Task 1: Simple referenced implementation that handles any base:
<langsyntaxhighlight lang=mathematica>Co9[n_, b_: 10] :=
With[{ans = FixedPoint[Total@IntegerDigits[#, b] &, n]},
If[ans == b - 1, 0, ans]];</langsyntaxhighlight>
 
{{out|Task 1 output}}
<langsyntaxhighlight lang=mathematica>Co9 /@ (vals = {1235, 2345, 4753})
{2, 5, 1}
 
Total[Co9 /@ vals] == Co9[Total[vals]]
True</langsyntaxhighlight>
 
Task 2:
<langsyntaxhighlight lang=mathematica>task2 = Select[Range@100, Co9[#] == Co9[#^2] &] </langsyntaxhighlight>
 
{{out|Task 2 output}}
Line 1,264:
Task 3:
Defines the efficient co9 using Mod.
<langsyntaxhighlight lang=mathematica>Co9eff[n_, b_: 10] := Mod[n, b - 1]; </langsyntaxhighlight>
 
{{out|Task 3 output}}
Testing bases 10 and 17
<langsyntaxhighlight lang=mathematica>task2 == Select[Range@100, Co9eff[#] == Co9eff[#^2] &]
True
 
Select[Range@100, Co9eff[#, 17] == Co9eff[#^2, 17] &]
{1, 16, 17, 32, 33, 48, 49, 64, 65, 80, 81, 96, 97}</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang=nim>import sequtils
 
iterator castOut(base = 10, start = 1, ending = 999_999): int =
Line 1,297:
inc x
 
echo toSeq(castOut(base=16, start=1, ending=255))</langsyntaxhighlight>
{{out}}
<pre>@[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60, 61, 66, 70, 75, 76, 81, 85, 90, 91, 96, 100, 105, 106, 111, 115, 120, 121, 126, 130, 135, 136, 141, 145, 150, 151, 156, 160, 165, 166, 171, 175, 180, 181, 186, 190, 195, 196, 201, 205, 210, 211, 216, 220, 225, 226, 231, 235, 240, 241, 246, 250, 255]</pre>
 
=={{header|Objeck}}==
<langsyntaxhighlight lang=objeck>class CastingNines {
function : Main(args : String[]) ~ Nil {
base := 10;
Line 1,321:
->As(Float)*100))->PrintLine("%");
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,329:
=={{header|PARI/GP}}==
{{trans|C++}}
<langsyntaxhighlight lang=parigp>{base=10;
N=2;
c1=c2=0;
Line 1,340:
);
print("\nTrying "c2" numbers instead of "c1" numbers saves " 100.-(c2/c1)*100 "%")}
</syntaxhighlight>
</lang>
{{out|Produces}}
<pre>
Line 1,355:
 
=={{header|Perl}}==
<langsyntaxhighlight lang=perl>sub co9 { # Follows the simple procedure asked for in Part 1
my $n = shift;
return $n if $n < 10;
Line 1,387:
printf "[@l]\nIn base %d, trying %d numbers instead of %d saves %.4f%%\n\n",
$base, $n, $N, 100-($n/$N)*100;
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,405:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight lang=Phix>(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">procedure</span> <span style="color: #000000;">co9</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">start</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">base</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">integer</span> <span style="color: #000000;">lim</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">kaprekars</span><span style="color: #0000FF;">)</span>
Line 1,433:
<span style="color: #000000;">co9</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0(17)10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">17</span><span style="color: #0000FF;">*</span><span style="color: #000000;">17</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">0(17)3d</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0(17)d4</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0(17)gg</span><span style="color: #0000FF;">})</span>
<span style="color: #000000;">co9</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">10</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">1000</span><span style="color: #0000FF;">,</span> <span style="color: #0000FF;">{</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">45</span><span style="color: #0000FF;">,</span><span style="color: #000000;">55</span><span style="color: #0000FF;">,</span><span style="color: #000000;">99</span><span style="color: #0000FF;">,</span><span style="color: #000000;">297</span><span style="color: #0000FF;">,</span><span style="color: #000000;">703</span><span style="color: #0000FF;">,</span><span style="color: #000000;">999</span><span style="color: #0000FF;">})</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,450:
 
=={{header|Picat}}==
<langsyntaxhighlight lang=Picat>go =>
Base10 = 10,
foreach(N in [2,6])
Line 1,481:
end,
printf("Trying %d numbers instead of %d numbers saves %2.3f%%\n", C2, C1, 100 - ((C2/C1)*100)),
nl.</langsyntaxhighlight>
 
{{out}}
Line 1,499:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight lang=PicoLisp>(de kaprekar (N)
(let L (cons 0 (chop (* N N)))
(for ((I . R) (cdr L) R (cdr R))
Line 1,534:
(range 1 100) ) )
(bye)</langsyntaxhighlight>
 
{{out}}
Line 1,549:
=={{header|Python}}==
This works slightly differently, generating the "wierd" (as defined by Counting Out Nines) numbers which may be Kaprekar, rather than filtering all numbers in a range.
<langsyntaxhighlight lang=Python># Casting out Nines
#
# Nigel Galloway: June 27th., 2012,
Line 1,567:
 
for V in CastOut(Base=16,Start=1,End=255):
print(V, end=' ')</langsyntaxhighlight>
Produces:
<pre>
Line 1,585:
<code>kaprekar</code> is defined at [[Kaprekar numbers#Quackery]].
 
<langsyntaxhighlight lang=Quackery> [ true unrot swap
witheach
[ over find
Line 1,648:
dup echo cr
say "Is the former a subset of the latter? "
subset iff [ say "Yes." ] else [ say "No." ]</langsyntaxhighlight>
 
{{out}}
Line 1,680:
 
=={{header|Racket}}==
<langsyntaxhighlight lang=racket>#lang racket
(require math)
 
Line 1,690:
(with-modulus 9
(for/fold ([sum 0]) ([d (digits n)])
(mod+ sum d))))</langsyntaxhighlight>
 
=={{header|Raku}}==
Line 1,696:
{{trans|Python}}
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang=raku perl6line>sub cast-out(\BASE = 10, \MIN = 1, \MAX = BASE**2 - 1) {
my \B9 = BASE - 1;
my @ran = ($_ if $_ % B9 == $_**2 % B9 for ^B9);
Line 1,711:
say cast-out;
say cast-out 16;
say cast-out 17;</langsyntaxhighlight>
{{out}}
<pre>(1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99)
Line 1,718:
 
=={{header|REXX}}==
<langsyntaxhighlight lang=rexx>/*REXX program demonstrates the casting─out─nines algorithm (with Kaprekar numbers). */
parse arg LO HI base . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then do; LO=1; HI=1000; end /*Not specified? Then use the default*/
Line 1,757:
end /*m*/ /* [↑] found a Kaprekar number. */
end /*j*/
return strip($) /*return Kaprekar numbers to invoker. */</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,783:
 
=={{header|Ring}}==
<langsyntaxhighlight lang=ring>
# Project : Casting out nines
 
Line 1,821:
svect = left(svect, len(svect) - 2)
see svect + "}" + nl
</syntaxhighlight>
</lang>
Output:
<pre>
Line 1,839:
=={{header|Ruby}}==
{{trans|C}}
<langsyntaxhighlight lang=ruby>N = 2
base = 10
c1 = 0
Line 1,853:
 
puts
print "Trying %d numbers instead of %d numbers saves %f%%" % [c2, c1, 100.0 - 100.0 * c2 / c1]</langsyntaxhighlight>
{{out}}
<pre>1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
Line 1,859:
 
=={{header|Rust}}==
<langsyntaxhighlight lang=Rust>fn compare_co9_efficiency(base: u64, upto: u64) {
let naive_candidates: Vec<u64> = (1u64..upto).collect();
let co9_candidates: Vec<u64> = naive_candidates.iter().cloned()
Line 1,879:
compare_co9_efficiency(10, 100);
compare_co9_efficiency(16, 256);
}</langsyntaxhighlight>
 
{{out}}
Line 1,889:
=={{header|Scala}}==
Code written in scala follows functional paradigm of programming, finds list of candidates for Kaprekar numbers within given range.
<langsyntaxhighlight lang=Scala>
object kaprekar{
// PART 1
Line 1,906:
}
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,919:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang=seed7>$ include "seed7_05.s7i";
 
const func bitset: castOut (in integer: base, in integer: start, in integer: ending) is func
Line 1,955:
begin
writeln(castOut(16, 1, 255));
end func;</langsyntaxhighlight>
 
{{out}}
Line 1,964:
=={{header|Sidef}}==
{{trans|Raku}}
<langsyntaxhighlight lang=ruby>func cast_out(base = 10, min = 1, max = (base**2 - 1)) {
 
var b9 = base-1
Line 1,986:
say cast_out().join(' ')
say cast_out(16).join(' ')
say cast_out(17).join(' ')</langsyntaxhighlight>
{{out}}
<pre>
Line 1,995:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang=tcl>proc co9 {x} {
while {[string length $x] > 1} {
set x [tcl::mathop::+ {*}[split $x ""]]
Line 2,033:
}
puts $satisfying
puts "Trying [llength $satisfying] numbers instead of 255 numbers saves [percent [llength $satisfying] 255]"</langsyntaxhighlight>
{{out}}With some newlines inserted…
<pre>
Line 2,048:
=={{header|Visual Basic .NET}}==
{{trans|Java}}
<langsyntaxhighlight lang=vbnet>Module Module1
Sub Print(ls As List(Of Integer))
Dim iter = ls.GetEnumerator
Line 2,089:
End Sub
 
End Module</langsyntaxhighlight>
{{out}}
<pre>[1, 6, 10, 15, 16, 21, 25, 30, 31, 36, 40, 45, 46, 51, 55, 60, 61, 66, 70, 75, 76, 81, 85, 90, 91, 96, 100, 105, 106, 111, 115, 120, 121, 126, 130, 135, 136, 141, 145, 150, 151, 156, 160, 165, 166, 171, 175, 180, 181, 186, 190, 195, 196, 201, 205, 210, 211, 216, 220, 225, 226, 231, 235, 240, 241, 246, 250, 255]
Line 2,097:
=={{header|Wren}}==
{{trans|D}}
<langsyntaxhighlight lang=ecmascript>var castOut = Fn.new { |base, start, end|
var b = base - 1
var ran = (0...b).where { |n| n % b == (n * n) % b }
Line 2,118:
System.print(castOut.call(10, 1, 99))
System.print()
System.print(castOut.call(17, 1, 288))</langsyntaxhighlight>
 
{{out}}
Line 2,131:
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang=zkl>fcn castOut(base=10, start=1, end=999999){
base-=1;
ran:=(0).filter(base,'wrap(n){ n%base == (n*n)%base });
Line 2,142:
}
// doesn't get here
}</langsyntaxhighlight>
<langsyntaxhighlight lang=zkl>castOut(16, 1, 255).toString(*).println("\n-----");
castOut(10, 1, 99).toString(*).println("\n-----");
castOut(17, 1, 288).toString(*).println();</langsyntaxhighlight>
{{out}}
<pre>
Line 2,162:
=={{header|ZX Spectrum Basic}}==
{{trans|C++}}
<langsyntaxhighlight lang=zxbasic>10 LET Base=10
20 LET N=2
30 LET c1=0
Line 2,175:
160 STOP
170 DEF FN m(a,b)=a-INT (a/b)*b
</syntaxhighlight>
</lang>
 
{{omit from|GUISS}}
10,327

edits