Casting out nines: Difference between revisions
Content added Content deleted
Basicgames (talk | contribs) |
(Added various BASIC dialects (BASIC256, Chipmunk Basic,Gambas, GW-BASIC, MSX Basic, PureBasic, QBasic, Run BASIC, True BASIC, XBasic and Yabasic)) |
||
Line 250: | Line 250: | ||
Trying 23 numbers instead of 100 numbers saves 77.00% |
Trying 23 numbers instead of 100 numbers saves 77.00% |
||
</pre> |
</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}}== |
=={{header|C}}== |
||
{{trans|C++}} |
{{trans|C++}} |