Strange numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(→‎{{header|Vlang}}: Rename "Vlang" in "V (Vlang)")
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(10 intermediate revisions by 6 users not shown)
Line 149:
 
Strange numbers in range 1_000_000_000 .. 1_999_999_999: 853423
</pre>
 
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.3.win32}}
<syntaxhighlight lang="algol68">
BEGIN # show some strange numbers - numbers whose digits differ by a prime #
# returns TRUE if n is strange, FALSE otherwise #
PROC is strange = ( INT n )BOOL:
BEGIN
INT d1 := ABS n MOD 10;
INT v := ABS n OVER 10;
BOOL strange := TRUE;
WHILE strange AND v > 0 DO
INT d2 = v MOD 10;
v OVERAB 10;
INT diff = ABS ( d1 - d2 );
strange := diff = 2 OR diff = 3 OR diff = 5 OR diff = 7;
d1 := d2
OD;
strange
END # is strange # ;
INT s count := 0;
FOR n FROM 101 TO 499 DO
IF is strange( n ) THEN
print( ( whole( n, -4 ) ) );
IF ( s count +:= 1 ) MOD 20 = 0 THEN print( ( newline ) ) FI
FI
OD
END
</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|ALGOL W}}==
<syntaxhighlight lang="algolw">begin % find "strange numbers" - numbers where digits differ from the next %
begin % find "strange numbers" - numbers where digits differ from the next %
% by a prime %
% returns true if n is strange, false otherwise %
logical procedure isStrange( integer value n ) ;
begin
integer rest, d0;
logical strange;
rest := abs( n );
ifstrange rest:= < 10 then falsetrue;
elsed0 begin := rest rem 10;
rest logical:= strangerest div 10;
while strange and rest integer> d0;0 do begin
strangeinteger :=d1, truediff;
d0d1 := rest rem 10;
rest := rest div 10;
whilediff strange and rest >:= 0abs( dod1 begin- d0 );
strange := diff = integer2 d1,or diff = 3 or diff = 5 or diff = 7;
d1d0 := rest rem 10;d1
end while_strange_and_rest_gt_0 ;
rest := rest div 10;
strange
diff := abs( d1 - d0 );
strange := diff = 2 or diff = 3 or diff = 5 or diff = 7;
d0 := d1
end while_strange_and_rest_gt_0 ;
strange
end if_rest_lt_10__
end isStrange ;
% test the isStrange procedure on values 100101-499 %
begin
integer strangeCount;
strangeCount := 0;
for n := 100101 until 499 do begin;
if isStrange( n ) then begin
strangeCount := strangeCount + 1;
Line 190 ⟶ 224:
end for_n
end
end.
end.</syntaxhighlight>
</syntaxhighlight>
{{out}}
<pre>
Line 435 ⟶ 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 485 ⟶ 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 513 ⟶ 609:
474 475 479 492 494
496 497</pre>
 
=={{header|BCPL}}==
<syntaxhighlight lang="bcpl">get "libhdr"
Line 956 ⟶ 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,077 ⟶ 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,045 ⟶ 2,189:
Total 28-digit strange numbers: 9,023,647,681,353,485,161
(0.0s)
</pre>
 
=={{header|PL/0}}==
PL/0 can only output 1 value per line, so the output has been manually adjusted to show multiple values per line to save space.
<syntaxhighlight lang="pascal">
var n, v, d1, d2, diff, strange;
begin
n := 100;
while n < 499 do begin
n := n + 1;
d1 := n - ( ( n / 10 ) * 10 );
v := n / 10;
strange := 1;
while strange * v > 0 do begin
d2 := v - ( ( v / 10 ) * 10 );
v := v / 10;
strange := 0;
diff := d1 - d2;
if diff < 0 then diff := - diff;
d1 := d2;
if diff = 2 then strange := 1;
if diff = 3 then strange := 1;
if diff = 5 then strange := 1;
if diff = 7 then strange := 1;
end;
if strange = 1 then ! n
end
end.
</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>
 
Line 2,220 ⟶ 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,329 ⟶ 2,550:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497
</pre>
 
=={{header|RPL}}==
{{works with|Halcyon Calc|4.2.7}}
{| class="wikitable"
! RPL code
! Comment
|-
|
'''IF''' DUP 10 < '''THEN''' DROP 0 '''ELSE'''
→STR → n
≪ 1 SF { 0 1 4 6 8 9 }
1 n SIZE 1 - '''FOR''' j
n j DUP SUB NUM n j 1 + DUP SUB NUM - ABS
'''IF''' OVER SWAP POS '''THEN''' 1 CF n SIZE 'j' STO '''END'''
'''NEXT'''
DROP 1 '''FS?'''
≫ '''END'''
≫ ''''STRG?'''' STO
|
'''STRG?''' '' n -- boolean ) ''
return false if n < 10
store locally n as a string
initialize return flag and list of non-prime numbers
scan n
get |n[j]-n[j+1]|
if not prime then clear flag and exit loop
return flag value
|}
 
{{in}}
<pre>
≪ {} 101 499 FOR j IF j STRG? THEN j + END NEXT ≫ EVAL
</pre>
 
{{out}}
<pre>
1: { 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>
 
Line 2,478 ⟶ 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,523 ⟶ 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