Casting out nines: Difference between revisions

no edit summary
No edit summary
 
(13 intermediate revisions by 7 users not shown)
Line 142:
451 459 460 468 469 477 478 486 487 495 496
</pre>
=={{header|ABC}}==
<syntaxhighlight lang="abc">
\ casting out nines - based on the Action! sample
 
HOW TO ADD v TO n: PUT n + v IN n
 
PUT 10, 2, 0, 0 IN base, n, count, total
FOR i IN { 1 .. base ** n }:
ADD 1 TO total
IF i mod ( base - 1 ) = ( i * i ) mod ( base - 1 ):
ADD 1 TO count
WRITE i
WRITE // "Trying", count, "numbers instead of", total, "numbers saves"
WRITE 100 - ( ( 100 * count ) / total ), "%" /
</syntaxhighlight>
{{out}}
<pre>
1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100
 
Trying 23 numbers instead of 100 numbers saves 77 %
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">INT FUNC Power(INT a,b)
Line 178 ⟶ 200:
Trying 23 numbers instead of 100 numbers saves 77%
</pre>
 
=={{header|ALGOL 68}}==
{{Trans|Action!}}
Line 250 ⟶ 273:
Trying 23 numbers instead of 100 numbers saves 77.00%
</pre>
 
=={{header|BASIC}}==
==={{header|BASIC256}}===
<syntaxhighlight lang="qbasic">base = 10
c1 = 0
c2 = 0
for k = 1 to (base ^ 2) - 1
c1 += 1
if k % (base - 1) = (k * k) % (base - 1) then c2 += 1: print k; " ";
next k
print
print "Trying "; c2; " numbers instead of "; c1; " numbers saves "; 100 - (100 * c2 / c1); "%"
end</syntaxhighlight>
 
==={{header|Chipmunk Basic}}===
{{works with|Chipmunk Basic|3.6.4}}
{{works with|GW-BASIC}}
{{works with|QBasic}}
{{works with|Run BASIC}}
{{works with|Just Basic}}
<syntaxhighlight lang="qbasic">100 cls
110 bs = 10 : c1 = 0 : c2 = 0
120 for k = 1 to (bs^2)-1
130 c1 = c1+1
140 if k mod (bs-1) = (k*k) mod (bs-1) then c2 = c2+1 : print k;
150 next k
160 print
170 print "Trying ";c2;"numbers instead of ";c1;"numbers saves ";100-(100*c2/c1);"%"
180 end</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="vbnet">Public Sub Main()
Dim base10 As Integer = 10
Dim c1 As Integer = 0, c2 As Integer = 0, k As Integer
For k = 1 To base10 ^ 2
c1 += 1
If (k Mod (base10 - 1) = (k * k) Mod (base10 - 1)) Then
c2 += 1
Print k; " ";
End If
Next
Print "\nTrying "; c2; " numbers instead of "; c1; " numbers saves "; 100 - (100 * c2 / c1); "%"
End</syntaxhighlight>
 
==={{header|GW-BASIC}}===
The [[#MSX-BASIC|MSX-BASIC]] solution works without any changes.
 
==={{header|MSX Basic}}===
{{works with|MSX BASIC|any}}
{{works with|BASICA}}
{{works with|Chipmunk Basic}}
{{works with|GW-BASIC}}
{{works with|PC-BASIC|any}}
{{works with|QBasic}}
<syntaxhighlight lang="qbasic">100 CLS
110 BS = 10 : C1 = 0 : C2 = 0
120 FOR K = 1 TO (BS^2)-1
130 C1 = C1+1
140 IF K MOD (BS-1) = (K*K) MOD (BS-1) THEN C2 = C2+1 : PRINT K;
150 NEXT K
160 PRINT
170 PRINT USING "Trying ## numbers instead of ### numbers saves ##.##%";C2;C1;100-(100*C2/C1)
180 END</syntaxhighlight>
 
==={{header|PureBasic}}===
<syntaxhighlight lang="purebasic">OpenConsole()
Define.i base, c1, c2, k
 
base = 10
c1 = 0
c2 = 0
For k = 1 To Pow(base, 2) - 1
c1 + 1
If k % (base - 1) = (k * k) % (base - 1)
c2 + 1
Print(Str(k) + " ")
EndIf
Next k
 
PrintN(#CRLF$ + "Trying " + Str(c2) + " numbers instead of " + Str(c1) + " numbers saves " + Str(100 - (100 * c2 / c1)) + "%")
 
PrintN(#CRLF$ + "Press ENTER to exit"): Input()
CloseConsole()</syntaxhighlight>
 
==={{header|QBasic}}===
{{works with|QBasic|1.1}}
{{works with|QuickBasic|4.5}}
<syntaxhighlight lang="qbasic">CLS
bs = 10: c1 = 0: c2 = 0
FOR k = 1 TO (bs ^ 2) - 1
c1 = c1 + 1
IF k MOD (bs - 1) = (k * k) MOD (bs - 1) THEN c2 = c2 + 1: PRINT k;
NEXT k
PRINT
PRINT USING "Trying ## numbers instead of ### numbers saves ##.##%"; c2; c1; 100 - (100 * c2 / c1)</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
{{works with|Liberty BASIC}}
<syntaxhighlight lang="vb">base = 10
c1 = 0
c2 = 0
for k = 1 to (base ^ 2) - 1
c1 = c1 + 1
if k mod (base - 1) = (k * k) mod (base - 1) then c2 = c2 + 1: print k; " ";
next k
print
print "Trying "; using("##", c2); " numbers instead of "; using("###", c1); " numbers saves "; using("##.##", (100 - (100 * c2 / c1))); "%"
end</syntaxhighlight>
 
==={{header|True BASIC}}===
<syntaxhighlight lang="qbasic">LET bs = 10
LET c1 = 0
LET c2 = 0
FOR k = 1 TO (bs^2)-1
LET c1 = c1 + 1
IF REMAINDER(k,(bs-1)) = REMAINDER((k*k),(bs-1)) THEN
LET c2 = c2 + 1
PRINT k;
END IF
NEXT k
PRINT
PRINT USING "Trying ## numbers instead of ### numbers saves ##.##%": c2, c1, 100-(100*c2/c1)
END</syntaxhighlight>
 
==={{header|XBasic}}===
{{works with|Windows XBasic}}
<syntaxhighlight lang="qbasic">PROGRAM "Casting out nines"
VERSION "0.0000"
 
DECLARE FUNCTION Entry ()
 
FUNCTION Entry ()
bs = 10
c1 = 0
c2 = 0
FOR k = 1 TO (bs ** 2) - 1
INC c1
IF k MOD (bs - 1) = (k * k) MOD (bs - 1) THEN INC c2: PRINT k;
NEXT k
PRINT
PRINT "Trying "; c2; " numbers instead of "; c1; " numbers saves "; 100 - (100 * c2 / c1); "%"
END FUNCTION
END PROGRAM</syntaxhighlight>
 
==={{header|Yabasic}}===
<syntaxhighlight lang="vb">base = 10
c1 = 0
c2 = 0
for k = 1 to (base ^ 2) - 1
c1 = c1 + 1
if mod(k, (base - 1)) = mod((k * k), (base - 1)) then c2 = c2 + 1: print k, " "; : fi
next k
print "\nTrying ", c2 using("##"), " numbers instead of ", c1 using("###"), " numbers saves ", (100 - (100 * c2 / c1)) using("##.##"), "%"
end</syntaxhighlight>
 
=={{header|C}}==
{{trans|C++}}
Line 513 ⟶ 696:
36 potential Kaprekar numbers remain (~87.50% filtered out).
(1 16 17 32 33 48 49 64 65 80 ...)</pre>
 
=={{header|Craft Basic}}==
<syntaxhighlight lang="basic">precision 4
 
define base = 10, c1 = 0, c2 = 0
 
for k = 1 to (base ^ 2) - 1
 
let c1 = c1 + 1
 
if k % (base - 1) = (k * k) % (base - 1) then
 
let c2 = c2 + 1
print k
 
endif
 
next k
 
print "trying ", c2, " numbers instead of ", c1, " numbers saves ", 100 - (100 * c2 / c1), "%"</syntaxhighlight>
{{out| Output}}<pre>1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
trying 22 numbers instead of 99 numbers saves 77.7778%
</pre>
 
=={{header|D}}==
{{trans|Python}}
Line 554 ⟶ 761:
240, 241, 256, 257, 272, 273, 288]</pre>
 
 
=={{header|EasyLang}}==
{{trans|AWK}}
<syntaxhighlight>
base = 10
for k = 1 to base * base - 1
c1 += 1
if k mod (base - 1) = (k * k) mod (base - 1)
c2 += 1
write k & " "
.
.
print ""
print "Trying " & c2 & " numbers instead of " & c1 & " numbers saves " & 100 - 100 * c2 / c1
</syntaxhighlight>
 
=={{header|Free Pascal}}==
Line 610 ⟶ 832:
Trying 223 numbers instead of 1000 saves 77.70,%.
</pre>
 
 
=={{header|FreeBASIC}}==
Line 1,683 ⟶ 1,904:
0...99 with property "n 16 mod n 2 ** 16 mod =": [ 0 1 16 17 32 33 48 49 64 65 80 81 96 97 ]
Is the former a subset of the latter? Yes.
</pre>
 
=={{header|R}}==
<syntaxhighlight lang="R">
co9 <- function(base) {
x <- 1:(base^2-1)
x[(x %% (base-1)) == (x^2 %% (base-1))]
}
Map(co9,c(10,16,17))
</syntaxhighlight>
{{out}}
<pre>
[[1]]
[1] 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
 
[[2]]
[1] 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
[27] 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
[53] 196 201 205 210 211 216 220 225 226 231 235 240 241 246 250 255
 
[[3]]
[1] 1 16 17 32 33 48 49 64 65 80 81 96 97 112 113 128 129 144 145 160 161 176 177 192 193 208
[27] 209 224 225 240 241 256 257 272 273 288
</pre>
=={{header|Racket}}==
Line 1,838 ⟶ 2,082:
Trying 223 numbers instead of 1000 saves 77.70%
</pre>
=={{header|RPL}}==
====Task part 1: naive approach====
« '''WHILE''' DUP 9 > '''REPEAT'''
→STR 0
1 3 PICK SIZE '''FOR''' j
OVER j DUP SUB STR→ +
'''NEXT'''
SWAP DROP
'''END'''
» '<span style="color:blue">CO9</span>' STO <span style="color:grey">''@ ( n → remainder )''</span>
====Kaprekar number checker (any base)====
{{works with|RPL|HP48-R}}
« OVER SQ → n b n2
« 1 CF
1 n2 LN b LN / IP 1 + '''FOR''' j
n2 b j ^ MOD LASTARG / IP
'''IF''' OVER '''THEN'''
'''IF''' + n == '''THEN''' 1 SF n 'j' STO '''END'''
'''ELSE''' DROP2 '''END'''
'''NEXT'''
1 FS?
» » '<span style="color:blue">ISBKAR?</span>' STO <span style="color:grey">''@ ( n base → boolean )''</span>
====Task parts 2 & 3====
« { } → n base kaprekar
« 1 n '''FOR''' j
'''IF''' j base ISBKAR? '''THEN''' 'kaprekar' j STO+ '''END'''
'''NEXT'''
{ }
1 n '''FOR''' k
'''IF''' k base 1 - MOD LASTARG SWAP SQ SWAP MOD == '''THEN''' k + '''END'''
'''NEXT'''
0
1 kaprekar SIZE '''FOR''' j
'''IF''' OVER kaprekar j GET POS NOT '''THEN''' 1 + '''END'''
'''NEXT'''
"Missing K#" →TAG
1 3 PICK SIZE n / - "% saved" →TAG
» » '<span style="color:blue">CASTOUT</span>' STO <span style="color:grey">''@ ( span base → results )''</span>
 
255 10 <span style="color:blue">CASTOUT</span>
255 17 <span style="color:blue">CASTOUT</span>
{{out}}
<pre>
6: { 1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99 100 108 109 117 118 126 127 135 136 144 145 153 154 162 163 171 172 180 181 189 190 198 199 207 208 216 217 225 226 234 235 243 244 252 253 }
5: Missing K#:0
4: % saved: .776470588235
3: { 1 16 17 32 33 48 49 64 65 80 81 96 97 112 113 128 129 144 145 160 161 176 177 192 193 208 209 224 225 240 241 }
2: Missing K#:0
1: % saved: .878431372549
</pre>
 
=={{header|Ruby}}==
{{trans|C}}
Line 1,858 ⟶ 2,153:
<pre>1 9 10 18 19 27 28 36 37 45 46 54 55 63 64 72 73 81 82 90 91 99
Trying 22 numbers instead of 99 numbers saves 77.777778%</pre>
 
=={{header|Rust}}==
<syntaxhighlight lang="rust">fn compare_co9_efficiency(base: u64, upto: u64) {
Line 2,104 ⟶ 2,400:
b := []int{len: base - 1, init: index}
ran := b.filter(it % b.len == (it * it) % b.len)
mut x, mut k := start / (base - 1)b.len, 0
mut result := []int{}
for {
Line 2,130 ⟶ 2,426:
=={{header|Wren}}==
{{trans|D}}
<syntaxhighlight lang="ecmascriptwren">var castOut = Fn.new { |base, start, end|
var b = base - 1
var ran = (0...b).where { |n| n % b == (n * n) % b }
Line 2,161 ⟶ 2,457:
[1, 16, 17, 32, 33, 48, 49, 64, 65, 80, 81, 96, 97, 112, 113, 128, 129, 144, 145, 160, 161, 176, 177, 192, 193, 208, 209, 224, 225, 240, 241, 256, 257, 272, 273, 288]
</pre>
 
=={{header|XPL0}}==
{{trans|C}}
3

edits