Strange numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
(Add BASIC)
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(38 intermediate revisions by 22 users not shown)
Line 1:
{{draft task|Prime Numbers}}
'''n'''   is a '''strange number''' if every adjacent decimal digit differs from its neighbour by a prime number.
 
;Definition:
'''n'''   is a   '''strange number'''   (expressed in base ten)   if every adjacent decimal digit differs from its neighbour by a prime number.
 
 
;Task:
Show all strange numbers for &nbsp; 100 &nbsp; < &nbsp; '''n''' &nbsp; < &nbsp; 500
 
 
;Stretch goal
Line 11 ⟶ 16:
{{trans|Python}}
 
<langsyntaxhighlight lang="11l">F is_strange(n)
V xs = String(n).map(c -> Int(c))
R all(zip(xs, xs[1..]).map((a, b) -> abs(a - b) C (2, 3, 5, 7)))
Line 21 ⟶ 26:
print(el, end' ‘ ’)
I L.index % 10 == 9
print()</langsyntaxhighlight>
 
{{out}}
Line 39 ⟶ 44:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497
</pre>
 
=={{header|Action!}}==
<syntaxhighlight lang="action!">BYTE Func IsStrangeNumber(INT i)
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0]
BYTE d,diff,prev
 
prev=255
WHILE i#0
DO
d=i MOD 10
IF prev#255 THEN
IF prev>d THEN
diff=prev-d
ELSE
diff=d-prev
FI
IF primes(diff)=0
THEN RETURN (0)
FI
FI
prev=d
i==/10
OD
RETURN (1)
 
PROC Main()
INT i,count=[0]
 
FOR i=101 TO 499
DO
IF IsStrangeNumber(i) THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I strange numbers",count)
RETURN</syntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_numbers.png Screenshot from Atari 8-bit computer]
<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
 
There are 87 strange numbers
</pre>
 
=={{header|Ada}}==
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;
 
procedure Strange_Numbers is
 
function Is_Strange (A : Natural) return Boolean is
Last : Natural := A mod 10;
Reminder : Natural := A / 10;
Current : Natural;
begin
while Reminder /= 0 loop
Current := Reminder mod 10;
exit when abs (Current - Last) not in 2 | 3 | 5 | 7;
Last := Current;
Reminder := Reminder / 10;
end loop;
return Reminder = 0;
end Is_Strange;
 
Count : Natural := 0;
begin
for A in 101 .. 499 loop
if Is_Strange (A) then
Put (A'Image);
Count := Count + 1;
if Count mod 10 = 0 then
New_Line;
end if;
end if;
end loop;
New_Line;
Put_Line ("Strange numbers in range 101 .. 499: " & Count'Image);
New_Line;
 
Count := 0;
for A in 1_000_000_000 .. 1_999_999_999 loop
if Is_Strange (A) then
Count := Count + 1;
end if;
end loop;
Put_Line ("Strange numbers in range 1_000_000_000 .. 1_999_999_999: " & Count'Image);
end Strange_Numbers;</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
Strange numbers in range 101 .. 499: 87
 
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">
<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 80 ⟶ 224:
end for_n
end
end.</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 91 ⟶ 236:
 
=={{header|APL}}==
<langsyntaxhighlight APLlang="apl">(⊢(/⍨)(∧/2 3 5 7∊⍨2+/⍎¨∘⍕)¨) 100+⍳400</langsyntaxhighlight>
{{out}}
<pre>111 112 114 116 120 121 123 125 141 143 161 202 203 205 207 211 212 214 216 230 232 234 250 252 302 303 305 307 320 321
Line 97 ⟶ 242:
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">--------------------- STRANGE NUMBERS --------------------
 
-- isStrange :: Int -> Bool
Line 308 ⟶ 453:
end tell
end if
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 325 ⟶ 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}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRANGE_NUMBERS.AWK
BEGIN {
Line 359 ⟶ 533:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 375 ⟶ 549:
 
=={{header|BASIC}}==
==={{header|FreeBASIC}}===
<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</lang>
 
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 403 ⟶ 609:
474 475 479 492 494
496 497</pre>
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
let dgtprime(x) = x=2 | x=3 | x=5 | x=7
Line 420 ⟶ 627:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
Line 431 ⟶ 638:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|BQN}}==
<syntaxhighlight lang="bqn">↑‿10⥊((∧´(|¯1↓«-⊢)∊⟨2,3,5,7⟩˙)○•Fmt)¨⊸/100+↕400</syntaxhighlight>
{{out}}
The unused positions in the table are filled with <code>0</code>s, because that's the behaviour
of <code>↑‿10⥊</code>, and writing custom tabulation code would take much more code than the
actual task.
<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 0 0 0
┘</pre>
 
=={{header|C}}==
{{trans|Go}}
<langsyntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
 
Line 472 ⟶ 697:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in the open interval (100, 500) are:
Line 486 ⟶ 711:
 
87 strange numbers in all.</pre>
 
===Extended task===
<syntaxhighlight lang="c">#include <stdio.h>
 
const int next_digit[] = {
0x7532, 0x8643, 0x97540, 0x86510, 0x97621,
0x87320, 0x98431, 0x95420, 0x6531, 0x7642
};
 
void gen(char *p, int i, const char c)
{
p[i] = c;
 
if (p[i + 1] == '\0')
puts(p);
else
for (int d = next_digit[p[i++] - '0']; d; d >>= 4)
gen(p, i, '0' + (d&15));
}
 
int main(void)
{
// show between 100 and 500
char buf[4] = {"Hi!"};
 
for (char c = '1'; c < '5'; c++)
gen(buf, 0, c);
 
// count 10 digit ones
unsigned int table[10][10] = {{0}};
 
for (int j = 0; j < 10; j++) table[0][j] = 1U;
 
for (int i = 1; i < 10; i++)
for (int j = 0; j < 10; j++)
for (int d = next_digit[j]; d; d >>= 4)
table[i][j] += table[i - 1][d&15];
 
printf("\n%u 10-digits starting with 1\n", table[9][1]);
 
return 0;
}</syntaxhighlight>
{{out}}
<pre>% ./a.out | fmt
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
 
853423 10-digits starting with 1</pre>
 
=={{header|C++}}==
{{trans|Java}}
<langsyntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <vector>
Line 540 ⟶ 817:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
Line 546 ⟶ 823:
 
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|CLU}}==
<syntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
yield(n // 10)
n := n / 10
end
end digits
 
strange = proc (n: int) returns (bool)
last: int := -1
for d: int in digits(n) do
if last ~= -1 then
diff: int := int$abs(last-d)
if diff~=2 cand diff~=3 cand diff~=5 cand diff~=7 then
return(false)
end
end
last := d
end
return(true)
end strange
 
start_up = proc ()
po: stream := stream$primary_output()
col: int := 0
for n: int in int$from_to(100, 500) do
if ~strange(n) then continue end
stream$putright(po, int$unparse(n), 3)
col := col + 1
if col = 10 then
stream$putc(po, '\n')
col := 0
else
stream$putc(po, ' ')
end
end
end start_up</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|COBOL}}==
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. STRANGE-NUMBERS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 COMPUTATION.
02 NUM PIC 999.
02 DIGITS REDEFINES NUM PIC 9 OCCURS 3 TIMES.
02 DIGIT-PRIME PIC 9.
88 PRIME VALUES 2 3 5 7.
02 CUR-DIGIT PIC 9.
01 OUTPUT-FORMAT.
02 N-OUT PIC ZZ9.
PROCEDURE DIVISION.
BEGIN.
PERFORM STRANGE-TEST
VARYING NUM FROM 100 BY 1
UNTIL NUM IS GREATER THAN 500.
STOP RUN.
STRANGE-TEST SECTION.
BEGIN.
SET CUR-DIGIT TO 1.
STEP.
IF DIGITS(CUR-DIGIT) IS LESS THAN DIGITS(CUR-DIGIT + 1)
SUBTRACT DIGITS(CUR-DIGIT + 1) FROM DIGITS(CUR-DIGIT)
GIVING DIGIT-PRIME
ELSE
SUBTRACT DIGITS(CUR-DIGIT) FROM DIGITS(CUR-DIGIT + 1)
GIVING DIGIT-PRIME.
IF PRIME NEXT SENTENCE ELSE GO TO DONE.
ADD 1 TO CUR-DIGIT.
IF CUR-DIGIT IS LESS THAN 3 GO TO STEP.
MOVE NUM TO N-OUT.
DISPLAY N-OUT.
DONE. EXIT.</syntaxhighlight>
{{out}}
<pre style='height:50ex;'>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|Cowgol}}==
<syntaxhighlight lang="cowgol">include "cowgol.coh";
 
sub abs(n: int8): (r: uint8) is
if n<0 then n := -n; end if;
r := n as uint8;
end sub;
 
sub strange(n: uint16): (s: uint8) is
s := 1;
while n >= 10 loop
var da: int8 := (n % 10) as int8;
n := n / 10;
var db: int8 := (n % 10) as int8;
var diff := abs(da-db);
if diff!=2 and diff!=3 and diff!=5 and diff!=7 then
s := 0;
return;
end if;
end loop;
end sub;
 
var n: uint16 := 100;
var col: uint8 := 0;
while n <= 500 loop
if strange(n) != 0 then
print_i16(n);
print_char(' ');
col := col + 1;
if col == 10 then
print_nl();
col := 0;
end if;
end if;
n := n + 1;
end loop;
print_nl();</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|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}}==
<syntaxhighlight lang="draco">proc nonrec strange(word n) bool:
bool is_strange;
short da, db, diff;
is_strange := true;
while is_strange and n >= 10 do
da := n % 10;
n := n / 10;
db := n % 10;
diff := |(da-db);
is_strange := is_strange and
(diff=2 or diff=3 or diff=5 or diff=7)
od;
is_strange
corp
 
proc nonrec main() void:
word n, col;
col := 0;
for n from 100 upto 500 do
if strange(n) then
write(n:3, ' ');
col := col + 1;
if col = 10 then
writeln();
col := 0
fi
fi
od
corp</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
Line 556 ⟶ 1,170:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">
// Strange numbers. Nigel Galloway: February 25th., 2021
let N=Array.init 10(fun n->[2;3;5;7]|>List.collect(fun g->[n+g;n-g])|>List.filter((<) -1)|>List.filter((>)10))
[1..4]|>List.collect(fun g->N.[g]|>List.map(fun n->g*10+n%10))|>List.collect(fun n->N.[n%10]|>List.map(fun g->n*10+g%10))|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 569 ⟶ 1,183:
===Identifying and filtering===
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: grouping io kernel math.ranges math.statistics
math.text.utils math.vectors prettyprint sequences ;
 
Line 579 ⟶ 1,193:
100 500 (a,b) [ strange? ] filter dup
10 group [ [ pprint bl ] each nl ] each nl
length pprint " strange numbers found." print</langsyntaxhighlight>
{{out}}
<pre>
Line 600 ⟶ 1,214:
Since we know the next possible digits based on the current last digit, we can construct strange numbers with a backtracking algorithm.
{{works with|Factor|0.99 2020-08-14}}
<langsyntaxhighlight lang="factor">USING: backtrack compiler.tree.propagation.call-effect
formatting io kernel sequences sequences.generalizations
tools.memory.private tools.time ;
Line 622 ⟶ 1,236:
dup length commas write
" 10-digit strange numbers beginning with 1:" print
[ first2 ] [ last2 ] bi "%u\n%u\n...\n%u\n%u\n" printf</langsyntaxhighlight>
{{out}}
<pre>
Line 634 ⟶ 1,248:
{ 1 8 6 9 7 9 7 9 7 9 }
</pre>
 
=={{header|FreeBASIC}}==
<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</lang>
{{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}}==
<langsyntaxhighlight lang="forth">\ tests whether n is prime for 0 <= n < 10
: prime? ( n -- ? )
1 swap lshift 0xac and 0<> ;
Line 693 ⟶ 1,280:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 711 ⟶ 1,298:
===Basic task===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 745 ⟶ 1,332:
}
fmt.Printf("\n%d strange numbers in all.\n", count)
}</langsyntaxhighlight>
 
{{out}}
Line 766 ⟶ 1,353:
===Stretch goal===
{{trans|Julia}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 796 ⟶ 1,383:
}
fmt.Println("Found", len(strangeOnes), places, "\b-digit strange numbers beginning with", start)
}</langsyntaxhighlight>
 
{{out}}
Line 804 ⟶ 1,391:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.List.Split (chunksOf)
 
Line 827 ⟶ 1,414:
unlines
(unwords <$> chunksOf 10 (show <$> xs))
]</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 844 ⟶ 1,431:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497</pre>
 
=={{header|J}}==
 
<syntaxhighlight lang="j">isStrange=: ([: */ 2 3 5 7 e.&:|~ 2 -/\ 10 #.inv ])"0
 
strangePair=: (1 p: ::0:"0 i.10),. 0 1}.isStrange (+ 10&*)"0/~ i.10</syntaxhighlight>
 
To test if a number is strange, we check if the absolute value of the differences of adjacent pairs of digits are 2, 3, 5 or 7.
 
To count the number of ten digit strange numbers which begin with 1, we first build a table which associates each digit with the digits which have a prime difference from it. Then we take a list of lists where there's only one list which has the single element 1, for each list we find the digits which have a prime difference from the last digit in that list and append those digits each to copies of that list. Since each iteration here adds a single digit, nine iterations give us lists representing 10 digit numbers. We count those lists and that gives us the number needed for the stretch goal.
 
Task examples:
 
<syntaxhighlight lang="j"> (#~ 100&<:) I. isStrange i.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 ...
#{{ ;<@{{x,"1 0 I. ({:x){y }}&strangePair"1 y}}^:9 ,:1
853423</syntaxhighlight>
 
=={{header|Java}}==
{{trans|Python}}
<langsyntaxhighlight Javalang="java">import java.util.LinkedList;
import java.util.List;
import java.util.function.BiPredicate;
Line 899 ⟶ 1,503:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
Line 913 ⟶ 1,517:
425 427 429 461 463 464 468 469 470 472
474 475 479 492 494 496 497 </pre>
 
=={{header|jq}}==
{{works with|jq}}
'''Works with gojq, the Go implementation of jq'''
 
===Filter method===
<syntaxhighlight lang="jq">
def is_strange:
def digits: tostring | explode | map([.] | implode | tonumber);
digits
| . as $d
| [2, 3, 5, 7] as $primes
| all( range(1; length);
($d[.] - $d[. - 1]) | length | IN( $primes[]));
 
# Pretty-printing
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
 
def lpad($len): tostring | ($len - length) as $l | (" " * $l)[:$l] + .;
 
def task($start; $stop; filter):
"Finding numbers matching $f for open interval \($start), \($stop):\n",
( [range($start; $stop) | select(filter) ]
| nwise(10) | map(lpad(3)) | join(" ") );
 
task(100; 500; is_strange)
</syntaxhighlight>
{{out}}
<pre>
Finding numbers matching $f for open interval 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>
 
 
===Generator method===
`lpad` and and `nwise` are as above and so their definitions are not repeated.
<syntaxhighlight lang="jq">
# input: width
# $start: maximum first digit allowed e.g. 9 for unrestricted
# output: the specified stream of strange numbers
def generate($start):
# The next permissible digit
def nxt: . as $i | range(0;10) | select($i - . | length | IN(2, 3, 5, 7));
 
# input: width
# $first: first digit
# output: an array of $n digits
def gen($first):
. as $n
| if $n == 0 then []
elif $n == 1 then [$first]
else ($n - 1) | gen($first) | . + ((.[-1]|nxt) | [.])
end;
gen( range(1; $start+1) );
 
[3 | generate(4) | map(tostring) | join("") | tonumber]
| nwise(10) | map(lpad(3)) | join(" ")
</syntaxhighlight>
{{out}}
As above, except for the header.
===Stretch goal===
Using generate/1 as already defined:
<syntaxhighlight lang="jq">
def count(s): reduce s as $x (null; .+1);
count(10 | generate(1))
</syntaxhighlight>
 
{{out}}
<pre>
853423
</pre>
 
=={{header|Julia}}==
===Filter method===
<langsyntaxhighlight lang="julia">isstrange(n::Integer) = (d = digits(n); all(i -> abs(d[i] - d[i + 1]) ∈ [2, 3, 5, 7], 1:length(d)-1))
 
function filter_open_interval(start, stop, f, doprint=true, rowlength=92)
Line 932 ⟶ 1,619:
 
filter_open_interval(100, 500, isstrange)
</langsyntaxhighlight>{{out}}
<pre>
Finding numbers matching isstrange for open interval (100, 500):
Line 945 ⟶ 1,632:
===Generator method===
HT: Factor for the table.
<langsyntaxhighlight lang="julia">function countstrange(places, fixedstart=1)
possibles = [
[2, 3, 5, 7],
Line 971 ⟶ 1,658:
countstrange(10)
@time countstrange(10)
</langsyntaxhighlight>{{out}}
<pre>
Found 853423 10-digit strange numbers with the most significant digit 1.
Line 978 ⟶ 1,665:
0.014545 seconds (139 allocations: 13.298 MiB, 29.39% gc time)
</pre>
 
=={{header|Kotlin}}==
{{trans|Java}}
<syntaxhighlight lang="scala">import kotlin.math.abs
 
fun digits(n: Int): List<Int> {
var nn = n
val result = mutableListOf<Int>()
while (nn > 0) {
val rem = nn % 10
result.add(0, rem)
nn /= 10
}
return result
}
 
fun isStrange(n: Int): Boolean {
val test = { a: Int, b: Int ->
val abs = abs(a - b)
abs == 2 || abs == 3 || abs == 5 || abs == 7
}
val xs = digits(n)
for (i in 1 until xs.size) {
if (!test(xs[i - 1], xs[i])) {
return false
}
}
return true
}
 
fun main() {
val xs = (100 until 500)
.filter(::isStrange)
.toList()
println("Strange numbers in range [100..500]")
println("(Total: ${xs.size})")
println()
for (i in xs.indices) {
val x = xs[i]
print(x)
if ((i + 1) % 10 == 0) {
println()
} else {
print(' ')
}
}
println()
}</syntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
(Total: 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 </pre>
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(D)
Line 1,013 ⟶ 1,761:
 
VECTOR VALUES F = $I4*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>STRANGE NUMBERS
Line 1,103 ⟶ 1,851:
496
497</pre>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
Select[Range[101, 499],
AllTrue[Partition[IntegerDigits[#], 2, 1], Differences /* First /* PrimeQ] &
]</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|Modula-2}}==
<syntaxhighlight lang="modula2">MODULE StrangeNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
 
VAR n, col: CARDINAL;
 
PROCEDURE strange(n: CARDINAL): BOOLEAN;
VAR dl, dr, diff: INTEGER;
BEGIN
WHILE n >= 10 DO
dl := n MOD 10;
n := n DIV 10;
dr := n MOD 10;
diff := ABS(dl-dr);
IF (diff#2) AND (diff#3) AND (diff#5) AND (diff#7) THEN
RETURN FALSE
END
END;
RETURN TRUE
END strange;
 
BEGIN
col := 0;
FOR n := 100 TO 500 DO
IF strange(n) THEN
WriteCard(n, 4);
col := col + 1;
IF col = 10 THEN
WriteLn;
col := 0
END
END
END;
WriteLn
END StrangeNumbers.</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|Nim}}==
Our program finds the strange numbers with a given number of digits. Filtering is done afterwards. It consumes a lot of memory but is able to find the result in less than a second on our small laptop.
 
<syntaxhighlight lang="nim">import algorithm, sequtils
 
const PrimeDigits = [2, 3, 5, 7]
 
type
Digit = 0..9
DigitSeq = seq[Digit]
 
func toInt(s: DigitSeq): int =
## Convert a sequence of digits to an int.
for d in s:
result = 10 * result + d
 
 
proc findStrangeNumbers(ndigits: Positive): seq[int] =
## Return the list of strange numbers with "ndigits" digits.
var list: seq[DigitSeq] = toSeq(1..9).mapIt(@[Digit it]) # Starting digits.
for _ in 2..ndigits:
var newList: seq[DigitSeq] # List with one more digit.
for dseq in list:
let last = dseq[^1]
for p in PrimeDigits:
if last - p >= 0:
newList.add dseq & (last - p)
if last + p <= 9:
newList.add dseq & (last + p)
list = move(newList) # "newList" becomes the current list.
result = list.map(toInt)
 
 
var result = sorted(findStrangeNumbers(3).filterIt(it < 500))
echo "Found ", result.len, " strange numbers between 101 and 499."
for i, n in result:
stdout.write n, if (i + 1) mod 15 == 0: '\n' else: ' '
echo()
 
result = findStrangeNumbers(10).filterIt(it div 1_000_000_000 == 1)
echo "\nFound ", result.len, " strange numbers with 10 digits and starting with 1."</syntaxhighlight>
 
{{out}}
<pre>Found 87 strange numbers between 101 and 499.
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
 
Found 853423 strange numbers with 10 digits and starting with 1.</pre>
 
=={{header|Pascal}}==
Line 1,108 ⟶ 1,963:
Not dynamically memorizing of all numbers saves much time 200ms -> 4ms.<BR>
Count is about 4.6 to the power of Digitscount -> first digit 1 followed by 9 digit -> 4.6^9 = 922190
<langsyntaxhighlight lang="pascal">program strangenumbers;
 
const
Line 1,194 ⟶ 2,049:
Writeln;
Writeln('Count : ',cnt);
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,212 ⟶ 2,067:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,226 ⟶ 2,081:
my $cnt = my @strange = grep { is_strange($_) } $low+1 .. $high-1;
say "Between $low and $high there are $cnt strange numbers:\n" .
(sprintf "@{['%5d' x $cnt]}", @strange[0..$cnt-1]) =~ s/(.{80})/$1\n/gr;</langsyntaxhighlight>
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
Line 1,237 ⟶ 2,092:
 
=={{header|Phix}}==
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant diff = {-7,-5,-3,-2,2,3,5,7},
<span style="color: #008080;">constant</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
poss = apply(true,sq_add,{tagset(9,0),{diff}}),
<span style="color: #000000;">poss</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">}}),</span>
nxts = apply(true,filter,{poss,{"in"},{{0,9}},{"[]"}})
<span style="color: #000000;">nxts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">poss</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">}},{</span><span style="color: #008000;">"[]"</span><span style="color: #0000FF;">}})</span>
-- nxts is {{2,3,5,7},{3,4,6,8}..., as per the factor and other entries
<span style="color: #000080;font-style:italic;">-- nxts is {{2,3,5,7},{3,4,6,8}..., as per the factor and other entries</span>
 
function strange(integer left, sequence digits, res={}, part={})
<span style="color: #008080;">function</span> <span style="color: #000000;">strange</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #000000;">part</span><span style="color: #0000FF;">={})</span>
for i=1 to length(digits) do
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
integer di = digits[i]
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
if left=1 then
<span style="color: #004080;">integer</span> <span style="color: #000000;">di</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
string fmt = join(repeat("%d",length(part)+1),"")
<span style="color: #008080;">if</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">then</span>
res = append(res,sprintf(fmt,part&di))
<span style="color: #004080;">string</span> <span style="color: #000000;">fmt</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #008000;">"%d"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">part</span><span style="color: #0000FF;">)+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">),</span><span style="color: #008000;">""</span><span style="color: #0000FF;">)</span>
else
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">append</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sprintf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">fmt</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">part</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">di</span><span style="color: #0000FF;">))</span>
res = strange(left-1,nxts[di+1],res,part&di)
<span style="color: #008080;">else</span>
end if
<span style="color: #000000;">res</span> <span style="color: #0000FF;">&=</span> <span style="color: #000000;">strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nxts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">di</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #7060A8;">deep_copy</span><span style="color: #0000FF;">(</span><span style="color: #000000;">part</span><span style="color: #0000FF;">)&</span><span style="color: #000000;">di</span><span style="color: #0000FF;">)</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
return res
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
end function
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
 
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
sequence res = strange(3,tagset(4)) -- (3 digit numbers beginning 1..4)
printf(1,"%d strange numbers found: %s\n",{length(res),join(shorten(res,"",5),",")})</lang>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (3 digit numbers beginning 1..4)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d strange numbers found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)})</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,263 ⟶ 2,121:
=== stretch goal ===
Dunno when to quit, me. As there does not appear to be much interest in which 1-digits are strange, I've shown three ways to set ones (output of 0, 10, or 4).
<!--<syntaxhighlight lang="phix">(phixonline)-->
<lang Phix>constant diff = {-7,-5,-3,-2,2,3,5,7},
<span style="color: #008080;">constant</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
poss = apply(true,sq_add,{tagset(9,0),{diff}}),
<span style="color: #000000;">poss</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">}}),</span>
nxts = apply(true,filter,{poss,{"in"},{{0,9}},{"[]"}}),
<span style="color: #000000;">nxts</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">filter</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">poss</span><span style="color: #0000FF;">,{</span><span style="color: #008000;">"in"</span><span style="color: #0000FF;">},{{</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">9</span><span style="color: #0000FF;">}},{</span><span style="color: #008000;">"[]"</span><span style="color: #0000FF;">}}),</span>
-- nxts is {{2,3,5,7},{3,4,6,8}..., as per the factor and other entries
<span style="color: #000080;font-style:italic;">-- nxts is {{2,3,5,7},{3,4,6,8}..., as per the factor and other entries
-- ones = columnize({columnize({repeat(0,10),tagset(9,0)}),repeat(0,10)}),
-- ones = columnize({columnize({repeat(0,10),tagset(9,0)}),repeat(10,10)}),
-- ones = columnize({columnize({repeat(0,10),tagset(9,0)}),applyrepeat(tagset(91,0),is_prime10)}),</span>
<span style="color: #000000;">ones</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">0</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">is_prime</span><span style="color: #0000FF;">)}),</span>
twos = columnize({columnize({repeat(1,10),tagset(9,0)}),apply(nxts,length)}),
<span style="color: #000000;">twos</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">columnize</span><span style="color: #0000FF;">({</span><span style="color: #7060A8;">repeat</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)}),</span><span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #000000;">nxts</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">)}),</span>
dict = new_dict(ones&twos)
<span style="color: #000000;">dict</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">new_dict</span><span style="color: #0000FF;">(</span><span style="color: #000000;">ones</span><span style="color: #0000FF;">&</span><span style="color: #000000;">twos</span><span style="color: #0000FF;">)</span>
 
function count_strange(integer left, sequence digits, atom res=0)
<span style="color: #008080;">function</span> <span style="color: #000000;">count_strange</span><span style="color: #0000FF;">(</span><span style="color: #004080;">integer</span> <span style="color: #000000;">left</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">sequence</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">,</span> <span style="color: #004080;">atom</span> <span style="color: #000000;">res</span><span style="color: #0000FF;">=</span><span style="color: #000000;">0</span><span style="color: #0000FF;">)</span>
for i=1 to length(digits) do
<span style="color: #008080;">for</span> <span style="color: #000000;">i</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">digits</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
integer di = digits[i]
<span style="color: #004080;">integer</span> <span style="color: #000000;">di</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">digits</span><span style="color: #0000FF;">[</span><span style="color: #000000;">i</span><span style="color: #0000FF;">]</span>
if getd_index({left-1,di},dict)!=NULL then
<span style="color: #008080;">if</span> <span style="color: #7060A8;">getd_index</span><span style="color: #0000FF;">({</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">di</span><span style="color: #0000FF;">},</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)!=</span><span style="color: #004600;">NULL</span> <span style="color: #008080;">then</span>
res += getd({left-1,di},dict)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">+=</span> <span style="color: #7060A8;">getd</span><span style="color: #0000FF;">({</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">di</span><span style="color: #0000FF;">},</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
else
<span atom prev style="color: res#008080;">else</span>
<span style="color: #004080;">atom</span> <span style="color: #000000;">prev</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">res</span>
res = count_strange(left-1,nxts[di+1],res)
<span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count_strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">nxts</span><span style="color: #0000FF;">[</span><span style="color: #000000;">di</span><span style="color: #0000FF;">+</span><span style="color: #000000;">1</span><span style="color: #0000FF;">],</span><span style="color: #000000;">res</span><span style="color: #0000FF;">)</span>
setd({left-1,di},res-prev,dict)
<span style="color: #7060A8;">setd</span><span style="color: #0000FF;">({</span><span style="color: #000000;">left</span><span style="color: #0000FF;">-</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #000000;">di</span><span style="color: #0000FF;">},</span><span style="color: #000000;">res</span><span style="color: #0000FF;">-</span><span style="color: #000000;">prev</span><span style="color: #0000FF;">,</span><span style="color: #000000;">dict</span><span style="color: #0000FF;">)</span>
end if
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
return res
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
end function
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
atom t0 = time()
 
<span style="color: #004080;">atom</span> <span style="color: #000000;">t0</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">time</span><span style="color: #0000FF;">()</span>
atom count = count_strange(10,tagset(1))
<span style="color: #004080;">atom</span> <span style="color: #000000;">count</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">count_strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">10</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">))</span>
printf(1,"There are %,d %d-digit strange numbers beginning with 1\n\n",{count,10})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"There are %,d %d-digit strange numbers beginning with 1\n\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">count</span><span style="color: #0000FF;">,</span><span style="color: #000000;">10</span><span style="color: #0000FF;">})</span>
 
for n=1 to iff(machine_bits()=32?23:28) do
<span style="color: #008080;">for</span> <span style="color: #000000;">n</span><span style="color: #0000FF;">=</span><span style="color: #000000;">1</span> <span style="color: #008080;">to</span> <span style="color: #008080;">iff</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">machine_bits</span><span style="color: #0000FF;">()=</span><span style="color: #000000;">32</span><span style="color: #0000FF;">?</span><span style="color: #000000;">23</span><span style="color: #0000FF;">:</span><span style="color: #000000;">28</span><span style="color: #0000FF;">)</span> <span style="color: #008080;">do</span>
printf(1,"Total %d-digit strange numbers: %,d\n",{n,count_strange(n,tagset(9,n>1))})
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"Total %d-digit strange numbers: %,d\n"</span><span style="color: #0000FF;">,{</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #000000;">count_strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">n</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">n</span><span style="color: #0000FF;">></span><span style="color: #000000;">1</span><span style="color: #0000FF;">))})</span>
end for
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
 
printf(1,"(%s)\n",elapsed(time()))</lang>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(%s)\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</syntaxhighlight>-->
{{out}}
<pre>
Line 1,330 ⟶ 2,190:
(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>
 
=={{header|PL/I}}==
<syntaxhighlight lang="pli">strangeNumbers: procedure options(main);
strange: procedure(nn) returns(bit);
declare (n, nn) fixed;
n = nn;
do while(n >= 10);
declare (dl, dr, diff) fixed;
dl = mod(n, 10);
n = n/10;
dr = mod(n, 10);
diff = abs(dl-dr);
if diff^=2 & diff^=3 & diff^=5 & diff^=7 then
return('0'b);
end;
return('1'b);
end strange;
declare (n, col) fixed;
col = 0;
do n=100 to 500;
if strange(n) then do;
put edit(n) (F(4));
col = col + 1;
if col = 10 then do;
col = 0;
put skip;
end;
end;
end;
end strangeNumbers;</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|PL/M}}==
<syntaxhighlight lang="pli">100H:
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
PRINT: PROCEDURE (S); DECLARE S ADDRESS; CALL BDOS(9,S); END PRINT;
 
PR$NUM: PROCEDURE (N);
DECLARE S (7) BYTE INITIAL ('..... $');
DECLARE N ADDRESS, I BYTE;
I = 5;
DIGIT: S(I := I-1) = '0' + N MOD 10;
IF (N := N/10) > 0 THEN GO TO DIGIT;
CALL PRINT(.S(I));
END PR$NUM;
 
ABS$DIFF: PROCEDURE (A, B) BYTE;
DECLARE (A, B) BYTE;
IF A > B
THEN RETURN A - B;
ELSE RETURN B - A;
END ABS$DIFF;
 
STRANGE: PROCEDURE (N) BYTE;
DECLARE N ADDRESS, (D1, D2, DIFF) BYTE;
DO WHILE N>=10;
D1 = N MOD 10;
N = N / 10;
D2 = N MOD 10;
DIFF = ABS$DIFF(D1, D2);
IF DIFF<>2 AND DIFF<>3 AND DIFF<>5 AND DIFF<>7 THEN
RETURN 0;
END;
RETURN -1;
END STRANGE;
 
DECLARE N ADDRESS, COL BYTE INITIAL (0);
DO N = 100 TO 500;
IF STRANGE(N) THEN DO;
CALL PR$NUM(N);
IF (COL := COL+1) = 10 THEN DO;
CALL PRINT(.(13,10,'$'));
COL = 0;
END;
END;
END;
CALL EXIT;
EOF</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|Python}}==
<langsyntaxhighlight lang="python">'''Strange Numbers'''
 
 
Line 1,390 ⟶ 2,389:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers in range [100..500]
Line 1,405 ⟶ 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}}==
<syntaxhighlight lang="raku" perl6line>sub infix:<′>(\x,\y) { abs(x - y) ∈ (2, 3, 5, 7) }
 
my ($low,$high) = 100, 500;
Line 1,413 ⟶ 2,449:
 
say "Between $low and $high there are {+@strange} strange numbers:\n";
print @strange.batch(20)».fmt("%4d").join: "\n";</langsyntaxhighlight>
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
Line 1,426 ⟶ 2,462:
Some optimization was added to bypass calculating the absolute value of adjacent decimal digit differences, &nbsp;
<br>as well as parsing of a number to determine the differences.
<langsyntaxhighlight lang="rexx">/*REXX pgm lists strange integers (within a range) whose decimal digs differ by a prime.*/
numeric digits 20 /*allow processing of larger numbers. */
parse arg LO HI bd . /*obtain optional arguments from the CL*/
Line 1,456 ⟶ 2,492:
commas(HI) ' (inclusive)'
say
if show then say strip($)</lang>
exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</syntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,472 ⟶ 2,511:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,498 ⟶ 2,537:
ok
next
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,511 ⟶ 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>
 
=={{header|Ruby}}==
{{trans|Java}}
<langsyntaxhighlight lang="ruby">def digits(n)
result = []
while n > 0
Line 1,552 ⟶ 2,633:
xs.each_slice(10) do |s|
print s, "\n"
end</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100 .. 500]
Line 1,566 ⟶ 2,647:
[425, 427, 429, 461, 463, 464, 468, 469, 470, 472]
[474, 475, 479, 492, 494, 496, 497]</pre>
 
=={{header|Sidef}}==
<syntaxhighlight lang="ruby">func generate_from_prefix(limit, p, base) {
 
var seq = [p]
 
for d in (base-1 -> primes) {
for k in ([1, -1]) {
var r = p[0]+(k*d)
next if (r < 0)
next if (r >= base)
var t = [r, p...]
if (t.digits2num(base) <= limit) {
seq << __FUNC__(limit, t, base)...
}
}
}
 
return seq
}
 
func strange_numbers(limit, base = 10, digits = @(^base)) {
digits.map {|p| generate_from_prefix(limit, [p], base)... }\
.map {|t| t.digits2num(base) }\
.sort.uniq
}
 
strange_numbers(500).grep { _ > 100 }.slices(10).each{
.join(' ').say
}</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|V (Vlang)}}==
===Basic task===
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn is_prime(nn int) bool {
mut n := nn
if n < 0 {
n = -n
}
return n == 2 || n == 3 || n == 5 || n == 7
}
fn main() {
mut count := 0
mut d := []int{}
println("Strange numbers in the open interval (100, 500) are:\n")
for i in 101..500{
d = d[..0]
mut j := i
for j > 0 {
d << j%10
j /= 10
}
if is_prime(d[0]-d[1]) && is_prime(d[1]-d[2]) {
print("$i ")
count++
if count%10 == 0 {
println('')
}
}
}
if count%10 != 0 {
println('')
}
println("\n$count strange numbers in all.")
}</syntaxhighlight>
 
{{out}}
<pre>
Strange numbers in the open interval (100, 500) are:
 
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
 
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===
<langsyntaxhighlight ecmascriptlang="wren">var primes = [2, 3, 5, 7]
var count = 0
var d = []
Line 1,587 ⟶ 2,805:
}
if (count % 10 != 0) System.print()
System.print("\n%(count) strange numbers in all.")</langsyntaxhighlight>
 
{{out}}
Line 1,610 ⟶ 2,828:
... though I've generated the 'possibles' table rather than hard-code it.
Runtime about 0.085 seconds which is quick for Wren.
<langsyntaxhighlight ecmascriptlang="wren">var diffs = [-7, -5, -3, -2, 2, 3, 5, 7]
var possibles = List.filled(10, null)
for (i in 0..9) {
Line 1,631 ⟶ 2,849:
}
 
System.print("Found %(strangeOnes.count) %(places)-digit strange numbers beginning with %(start).")</langsyntaxhighlight>
 
{{out}}
<pre>
Found 853423 10-digit strange numbers beginning with 1
</pre>
 
=={{header|XPL0}}==
Takes 49 seconds on Raspberry Pi 4.
<syntaxhighlight lang="xpl0">func Strange(N);
int N, A, B, D;
[N:= N/10;
A:= rem(0);
loop [N:= N/10;
B:= rem(0);
D:= abs(A-B);
if D#2 & D#3 & D#5 & D#7 then return false;
if N = 0 then return true;
A:= B;
];
];
 
int Cnt, N;
[Cnt:= 0;
for N:= 100 to 500-1 do
if Strange(N) then
[Cnt:= Cnt+1;
IntOut(0, N);
if rem(Cnt/20) = 0 then CrLf(0) else ChOut(0, ^ );
];
CrLf(0);
Cnt:= 0;
for N:= 1_000_000_000 to 1_999_999_999 do
if Strange(N) then
Cnt:= Cnt+1;
IntOut(0, Cnt);
]</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
853423
</pre>
9,476

edits