Strange numbers: Difference between revisions

Dialects of BASIC moved to the BASIC section.
(→‎{{header|ALGOL W}}: Simplify - treat single digit numbers as strange, as in other samples)
(Dialects of BASIC moved to the BASIC section.)
Line 520:
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<syntaxhighlight lang="basic">10 DEFINT I,J,K: DEFSTR S
<syntaxhighlight lang="freebasic">function is_strange( n as ulongint ) as boolean
20 FOR I=100 TO 500
dim as uinteger d, ld, a
30 S=STR$(I)
if n<10 then return true 'arbitrary, but makes the recursion easier
40 FOR J=2 TO LEN(S)-1
d = n mod 10
50 K=ABS(VAL(MID$(S,J,1))-VAL(MID$(S,J+1,1)))
60 IF K<>2 AND K<>3ld AND= K<>5(n ANDmod K<>7100) THEN\ 9010
a = abs(d - ld)
70 NEXT J
if a = 2 or a = 3 or a = 5 or a = 7 then return is_strange(n\10) else return false
80 PRINT I,
end function
90 NEXT I</syntaxhighlight>
 
print "Strange numbers between 100 and 500"
for n as uinteger = 101 to 499
if is_strange(n) then print n;" ";
next n
print : print
 
dim as integer c = 0
for n as ulongint = 1000000000 to 1999999999
if is_strange(n) then c+=1
next n
print using "There are ####### 10-digit strange numbers beginning with 1.";c</syntaxhighlight>
{{out}}<pre>
Strange numbers between 100 and 500
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497
 
There are 853423 10-digit strange numbers beginning with 1.</pre>
 
==={{header|GW-BASIC}}===
{{works with|BASICA}}
{{works with|QuickBASIC}}
<syntaxhighlight lang="basicgwbasic">10 DEFINTREM I,J,K:Strange DEFSTR Snumbers
20 DEFINT I-K: DEFSTR S
2030 FOR I = 100 TO 500
3040 S = STR$(I)
4050 FOR J = 2 TO LEN(S) - 1
5060 K = ABS(VAL(MID$(S, J, 1)) - VAL(MID$(S, J + 1, 1)))
70 IF K <> 2 AND K <> 3 AND K <> 5 AND K <> 7 THEN 100
7080 NEXT J
8090 PRINT I,
100 NEXT I
90110 NEXT IEND</syntaxhighlight>
{{out}}
<pre> 130 131 135 136 138
Line 548 ⟶ 580:
474 475 479 492 494
496 497</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
Line 1,112 ⟶ 1,145:
{ 1 8 6 9 7 9 7 9 7 9 }
</pre>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">function is_strange( n as ulongint ) as boolean
dim as uinteger d, ld, a
if n<10 then return true 'arbitrary, but makes the recursion easier
d = n mod 10
ld = (n mod 100) \ 10
a = abs(d - ld)
if a = 2 or a = 3 or a = 5 or a = 7 then return is_strange(n\10) else return false
end function
 
print "Strange numbers between 100 and 500"
for n as uinteger = 101 to 499
if is_strange(n) then print n;" ";
next n
print : print
 
dim as integer c = 0
for n as ulongint = 1000000000 to 1999999999
if is_strange(n) then c+=1
next n
print using "There are ####### 10-digit strange numbers beginning with 1.";c</syntaxhighlight>
{{out}}<pre>
Strange numbers between 100 and 500
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 413 414 416 418 420 424 425 427 429 461 463 464 468 469 470 472 474 475 479 492 494 496 497
 
There are 853423 10-digit strange numbers beginning with 1.</pre>
 
=={{header|Forth}}==
512

edits