Strange numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|ALGOL W}}: Simplify - treat single digit numbers as strange, as in other samples)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(6 intermediate revisions by 5 users not shown)
Line 470:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|Arturo}}==
<syntaxhighlight lang="arturo">strange?: function [n][
digs: digits n
loop 1..dec size digs 'd [
if not? contains? [2 3 5 7] abs digs\[d] - digs\[d-1] ->
return false
]
return true
]
 
strangeNums: select 100..500 => strange?
 
print "Strange numbers in 100..500:"
loop split.every: 10 strangeNums 'x ->
print map x 's -> pad to :string s 4</syntaxhighlight>
 
{{out}}
 
<pre>Strange numbers in 100..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 </pre>
 
=={{header|AWK}}==
Line 520 ⟶ 549:
 
=={{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="gwbasic">10 REM Strange numbers
20 DEFINT I-K: DEFSTR S
30 FOR I = 100 TO 500
40 S = STR$(I)
50 FOR J = 2 TO LEN(S) - 1
60 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
80 NEXT J
90 PRINT I,
100 NEXT I
110 END</syntaxhighlight>
{{out}}
<pre> 130 131 135 136 138
Line 548 ⟶ 609:
474 475 479 492 494
496 497</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
Line 991 ⟶ 1,053:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|Delphi}}==
{{works with|Delphi|6.0}}
{{libheader|SysUtils,StdCtrls}}
 
 
<syntaxhighlight lang="Delphi">
{This code is normally in a separate library, but it is included here for clarity}
 
procedure GetDigits(N: integer; var IA: TIntegerDynArray);
{Get an array of the integers in a number}
{Numbers returned from least to most significant}
var T,I,DC: integer;
begin
DC:=Trunc(Log10(N))+1;
SetLength(IA,DC);
for I:=0 to DC-1 do
begin
T:=N mod 10;
N:=N div 10;
IA[I]:=T;
end;
end;
 
function IsStrangeNumber(N: integer): boolean;
{Test if the difference between digits is prime}
var Digits: TIntegerDynArray;
var I: integer;
begin
Result:=False;
{Get digits}
GetDigits(N,Digits);
{test if the difference between digits is prime}
for I:=0 to High(Digits)-1 do
if not IsPrime(abs(Digits[I+1]-Digits[I])) then exit;
Result:=True
end;
 
 
procedure ShowStrangeNumbers(Memo: TMemo);
var I,Cnt: integer;
var S: string;
begin
S:='';
Cnt:=0;
for I:=100 to 500-1 do
if IsStrangeNumber(I) then
begin
Inc(Cnt);
S:=S+Format('%5d',[I]);
if (Cnt mod 10)=0 then S:=S+CRLF;
end;
Memo.Lines.Add('Count = '+IntToStr(Cnt));
Memo.Lines.Add(S);
end;
 
 
</syntaxhighlight>
{{out}}
<pre>
Count = 87
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
Elapsed Time: 2.428 ms.
 
</pre>
 
 
=={{header|Draco}}==
Line 1,112 ⟶ 1,248:
{ 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}}==
Line 2,295 ⟶ 2,404:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|Quackery}}==
 
<syntaxhighlight lang="Quackery"> [ ' [ 2 3 5 7 11 13 17 ]
find 7 < ] is prime ( n --> b )
 
[ 10 /mod
swap 10 /mod
tuck - abs prime not iff
[ 2drop false ] done
- abs prime ] is strange ( n --> b )
 
[] 399 times
[ i^ 101 +
dup strange iff
join else drop ]
dup size echo
say " strange numbers:"
cr cr
witheach
[ echo
i^ 10 mod 9 = iff cr else sp ]</syntaxhighlight>
 
{{out}}
 
<pre>87 strange numbers:
 
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
</pre>
 
=={{header|Raku}}==
Line 2,595 ⟶ 2,741:
 
87 strange numbers in all.
</pre>
 
=={{header|VTL-2}}==
<syntaxhighlight lang="vtl2">
100 K=0
110 N=100
120 #=N>499*9999
130 N=N+1
140 V=N/10
150 D=%
160 S=1
170 #=S*V=0*280
180 V=V/10
190 E=%
200 S=0
210 F=E<D*(D-E)+((E>D)*(E-D))
220 D=E
230 S=F=2+S
240 S=F=3+S
250 S=F=5+S
260 S=F=7+S
270 #=170
280 #=S=0*350
290 ?=N
300 $=32
310 K=K+1
320 #=K<10*350
330 ?=""
340 K=0
350 #=120
</syntaxhighlight>
{{out}}
<pre>
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
</pre>
 
=={{header|Wren}}==
===Basic task===
<syntaxhighlight lang="ecmascriptwren">var primes = [2, 3, 5, 7]
var count = 0
var d = []
Line 2,640 ⟶ 2,828:
... though I've generated the 'possibles' table rather than hard-code it.
Runtime about 0.085 seconds which is quick for Wren.
<syntaxhighlight lang="ecmascriptwren">var diffs = [-7, -5, -3, -2, 2, 3, 5, 7]
var possibles = List.filled(10, null)
for (i in 0..9) {
9,476

edits