Casting out nines: Difference between revisions
m
syntax highlighting fixup automation
m (→{{header|ALGOL 68}}: another typo...) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 32:
{{trans|Python}}
<
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()</
{{out}}
Line 66:
{{trans|REXX}}
The program uses two ASSIST macros (XDECO,XPRNT) to keep the code as short as possible.
<
CASTOUT CSECT
USING CASTOUT,R13 base register
Line 129:
XDEC DS CL12 temp for xdeco
YREGS
END CASTOUT</
{{out}}
<pre>
Line 142:
=={{header|Action!}}==
<
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</
{{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!}}
<
INT base = 10;
INT n = 2;
Line 198:
)
)
END</
{{out}}
<pre>
Line 208:
=={{header|Arturo}}==
<
base: 10
c1: 0
Line 223:
print ""
print ["Trying" c2 "numbers instead of" c1 "numbers saves" 100.0 - 100.0*c2//c1 "%"]</
{{out}}
Line 231:
=={{header|AWK}}==
<
# syntax: GAWK -f CASTING_OUT_NINES.AWK
# converted from C
Line 246:
exit(0)
}
</syntaxhighlight>
{{out}}
<pre>
Line 255:
=={{header|C}}==
{{trans|C++}}
<
#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;
}</
{{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===
<
//
// 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;
}</
{{out|Produces}}
<pre>
Line 348:
in this range are included.
===C++11 For Each Generator===
<
//
// Nigel Galloway. June 24th., 2012
Line 391:
for (int i : co9(1,99,&r)) { std::cout << i << ' '; }
return 0;
}</
{{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:
<
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); }
};</
Changing main:
<
ran r(16);
for (int i : co9(1,255,&r)) { std::cout << i << ' '; }
return 0;
}</
{{out|Produces}}
<pre>
Line 413:
</pre>
Changing main:
<
ran r(17);
for (int i : co9(1,288,&r)) { std::cout << i << ' '; }
return 0;
}</
{{out|Produces}}
<pre>
Line 425:
=={{header|C sharp|C#}}==
{{trans|Java}}
<
using System.Collections.Generic;
using System.Linq;
Line 481:
}
}
}</
{{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}}==
<
;;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))))</
{{out}}
<pre>CL-USER> (test :stop 99)
Line 522:
=={{header|D}}==
{{trans|Python}}
<
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;
}</
{{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}}==
<
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</
{{out}}
<pre>
Line 582:
=={{header|Free Pascal}}==
<
{$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.</
<pre>
Output:
Line 638:
=={{header|Go}}==
<
import (
Line 805:
fmt.Println("Valid subset.")
}
}</
{{out}}
<pre>
Line 821:
=={{header|Haskell}}==
<
| 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]</
Auxillary function, returning digits of a number for given base
<
or using unfolding:
<
where modDiv 0 = Nothing
modDiv n = let (q, r) = (n `divMod` base) in Just (r, q)</
'''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).
<
[: (#~ ] =&((m-1)&|) *:) <. + [: i. (+*)@-~
)</
Example use:
<
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</
Alternate implementation:
<
[: (#~ 0 = (m-1) | 0 _1 1&p.) <. + [: i. (+*)@-~
)</
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:
<
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</
And, of course, we can name parts of these expressions. For example:
<
0 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100</
Or, if you prefer:
<
(#~ ] =&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</
=={{header|Java}}==
{{trans|D}}
{{works with|Java|8}}
<
import java.util.stream.IntStream;
Line 925:
}
}
}</
<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:
<
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)</
{{Out}}
<pre>start:1 end:99 base:10 printBase:10
Line 1,009:
===ES6===
{{Trans|Haskell}}
<
'use strict';
Line 1,054:
.filter(n => (n % k) === (squared(n) % k)))(16)
});
})();</
{{Out}}
<pre>{
Line 1,110:
'''Definition of co9''':
<
def digits: tostring | explode | map(. - 48); # "0" is 48
if . == 9 then 0
elif 0 <= . and . <= 8 then .
else digits | add | co9
end;</
For convenience, we also define a function to check whether co9(i) equals co9(i*i)
for a given integer, i:
<
'''Example''':
Line 1,125:
Integers in 1 .. 100 satisfying co9(i) == co9(i*i):
<
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:
<
[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.
<
range(1; .)
| select(is_kaprekar and (co9_equals_co9_squared | not));</
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):
<
def count(stream): reduce stream as $i (0; . + 1);
. as $n
| (base - 1) as $b
| count( range(1; 1+$n) | select( . % $b == (.*.) % $b) ) / $n ;</
For example:
Line 1,161:
produces:
<
0.27
0.267
0.2667
0.26667</
=={{header|Julia}}==
<
1<=x<=8 ? x :
co9(sum(digits(x)))</
iskaprekar is defined in the task
[[Kaprekar_numbers#Julia]].
Line 1,185:
=={{header|Kotlin}}==
{{trans|D}}
<
fun castOut(base: Int, start: Int, end: Int): List<Int> {
Line 1,209:
println()
println(castOut(17, 1, 288))
}</
{{out}}
Line 1,222:
=={{header|Lua}}==
{{trans|C}}
<
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))</
{{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:
<
With[{ans = FixedPoint[Total@IntegerDigits[#, b] &, n]},
If[ans == b - 1, 0, ans]];</
{{out|Task 1 output}}
<
{2, 5, 1}
Total[Co9 /@ vals] == Co9[Total[vals]]
True</
Task 2:
<
{{out|Task 2 output}}
Line 1,264:
Task 3:
Defines the efficient co9 using Mod.
<
{{out|Task 3 output}}
Testing bases 10 and 17
<
True
Select[Range@100, Co9eff[#, 17] == Co9eff[#^2, 17] &]
{1, 16, 17, 32, 33, 48, 49, 64, 65, 80, 81, 96, 97}</
=={{header|Nim}}==
<
iterator castOut(base = 10, start = 1, ending = 999_999): int =
Line 1,297:
inc x
echo toSeq(castOut(base=16, start=1, ending=255))</
{{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}}==
<
function : Main(args : String[]) ~ Nil {
base := 10;
Line 1,321:
->As(Float)*100))->PrintLine("%");
}
}</
{{out}}
Line 1,329:
=={{header|PARI/GP}}==
{{trans|C++}}
<
N=2;
c1=c2=0;
Line 1,340:
);
print("\nTrying "c2" numbers instead of "c1" numbers saves " 100.-(c2/c1)*100 "%")}
</syntaxhighlight>
{{out|Produces}}
<pre>
Line 1,355:
=={{header|Perl}}==
<
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;
}</
{{out}}
<pre>
Line 1,405:
=={{header|Phix}}==
<!--<
<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>
<!--</
{{out}}
<pre>
Line 1,450:
=={{header|Picat}}==
<
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.</
{{out}}
Line 1,499:
=={{header|PicoLisp}}==
<
(let L (cons 0 (chop (* N N)))
(for ((I . R) (cdr L) R (cdr R))
Line 1,534:
(range 1 100) ) )
(bye)</
{{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.
<
#
# Nigel Galloway: June 27th., 2012,
Line 1,567:
for V in CastOut(Base=16,Start=1,End=255):
print(V, end=' ')</
Produces:
<pre>
Line 1,585:
<code>kaprekar</code> is defined at [[Kaprekar numbers#Quackery]].
<
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." ]</
{{out}}
Line 1,680:
=={{header|Racket}}==
<
(require math)
Line 1,690:
(with-modulus 9
(for/fold ([sum 0]) ([d (digits n)])
(mod+ sum d))))</
=={{header|Raku}}==
Line 1,696:
{{trans|Python}}
{{works with|Rakudo|2015.12}}
<syntaxhighlight lang=raku
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;</
{{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}}==
<
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. */</
{{out|output|text= when using the default inputs:}}
<pre>
Line 1,783:
=={{header|Ring}}==
<
# Project : Casting out nines
Line 1,821:
svect = left(svect, len(svect) - 2)
see svect + "}" + nl
</syntaxhighlight>
Output:
<pre>
Line 1,839:
=={{header|Ruby}}==
{{trans|C}}
<
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]</
{{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}}==
<
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);
}</
{{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.
<
object kaprekar{
// PART 1
Line 1,906:
}
}
</syntaxhighlight>
{{out}}
<pre>
Line 1,919:
=={{header|Seed7}}==
<
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;</
{{out}}
Line 1,964:
=={{header|Sidef}}==
{{trans|Raku}}
<
var b9 = base-1
Line 1,986:
say cast_out().join(' ')
say cast_out(16).join(' ')
say cast_out(17).join(' ')</
{{out}}
<pre>
Line 1,995:
=={{header|Tcl}}==
<
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]"</
{{out}}With some newlines inserted…
<pre>
Line 2,048:
=={{header|Visual Basic .NET}}==
{{trans|Java}}
<
Sub Print(ls As List(Of Integer))
Dim iter = ls.GetEnumerator
Line 2,089:
End Sub
End Module</
{{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}}
<
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))</
{{out}}
Line 2,131:
=={{header|zkl}}==
{{trans|D}}
<
base-=1;
ran:=(0).filter(base,'wrap(n){ n%base == (n*n)%base });
Line 2,142:
}
// doesn't get here
}</
<
castOut(10, 1, 99).toString(*).println("\n-----");
castOut(17, 1, 288).toString(*).println();</
{{out}}
<pre>
Line 2,162:
=={{header|ZX Spectrum Basic}}==
{{trans|C++}}
<
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>
{{omit from|GUISS}}
|