Casting out nines: Difference between revisions

no edit summary
No edit summary
 
(5 intermediate revisions by 2 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 1,881 ⟶ 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 2,036 ⟶ 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 2,056 ⟶ 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) {
3

edits