Strange numbers: Difference between revisions

m
→‎{{header|Wren}}: Changed to Wren S/H
m (→‎{{header|Wren}}: Changed to Wren S/H)
 
(27 intermediate revisions by 15 users not shown)
Line 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 26:
print(el, end' ‘ ’)
I L.index % 10 == 9
print()</langsyntaxhighlight>
 
{{out}}
Line 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 85 ⟶ 224:
end for_n
end
end.</lang>
</syntaxhighlight>
{{out}}
<pre>
Line 96 ⟶ 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 102 ⟶ 242:
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">--------------------- STRANGE NUMBERS --------------------
 
-- isStrange :: Int -> Bool
Line 313 ⟶ 453:
end tell
end if
end zipWith</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 330 ⟶ 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 364 ⟶ 533:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 380 ⟶ 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 408 ⟶ 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 425 ⟶ 627:
$)
wrch('*N')
$)</langsyntaxhighlight>
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
Line 436 ⟶ 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 477 ⟶ 697:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in the open interval (100, 500) are:
Line 491 ⟶ 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 545 ⟶ 817:
 
return 0;
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
Line 551 ⟶ 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
Line 561 ⟶ 881:
 
=={{header|COBOL}}==
<langsyntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. STRANGE-NUMBERS.
Line 596 ⟶ 916:
MOVE NUM TO N-OUT.
DISPLAY N-OUT.
DONE. EXIT.</langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>130
Line 685 ⟶ 1,005:
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
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|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 700 ⟶ 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 710 ⟶ 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 731 ⟶ 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 753 ⟶ 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 765 ⟶ 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 824 ⟶ 1,280:
 
main
bye</langsyntaxhighlight>
 
{{out}}
Line 842 ⟶ 1,298:
===Basic task===
{{trans|Wren}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 876 ⟶ 1,332:
}
fmt.Printf("\n%d strange numbers in all.\n", count)
}</langsyntaxhighlight>
 
{{out}}
Line 897 ⟶ 1,353:
===Stretch goal===
{{trans|Julia}}
<langsyntaxhighlight lang="go">package main
 
import "fmt"
Line 927 ⟶ 1,383:
}
fmt.Println("Found", len(strangeOnes), places, "\b-digit strange numbers beginning with", start)
}</langsyntaxhighlight>
 
{{out}}
Line 935 ⟶ 1,391:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.List.Split (chunksOf)
 
Line 958 ⟶ 1,414:
unlines
(unwords <$> chunksOf 10 (show <$> xs))
]</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 975 ⟶ 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 1,030 ⟶ 1,503:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
Line 1,050 ⟶ 1,523:
 
===Filter method===
<syntaxhighlight lang="jq">
<lang jq>
def is_strange:
def digits: tostring | explode | map([.] | implode | tonumber);
Line 1,072 ⟶ 1,545:
 
task(100; 500; is_strange)
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,091 ⟶ 1,564:
===Generator method===
`lpad` and and `nwise` are as above and so their definitions are not repeated.
<syntaxhighlight lang="jq">
<lang jq>
# input: width
# $start: maximum first digit allowed e.g. 9 for unrestricted
Line 1,113 ⟶ 1,586:
[3 | generate(4) | map(tostring) | join("") | tonumber]
| nwise(10) | map(lpad(3)) | join(" ")
</syntaxhighlight>
</lang>
{{out}}
As above, except for the header.
===Stretch goal===
Using generate/1 as already defined:
<syntaxhighlight lang="jq">
<lang jq>
def count(s): reduce s as $x (null; .+1);
count(10 | generate(1))
</langsyntaxhighlight>
 
{{out}}
Line 1,130 ⟶ 1,603:
=={{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 1,146 ⟶ 1,619:
 
filter_open_interval(100, 500, isstrange)
</langsyntaxhighlight>{{out}}
<pre>
Finding numbers matching isstrange for open interval (100, 500):
Line 1,159 ⟶ 1,632:
===Generator method===
HT: Factor for the table.
<langsyntaxhighlight lang="julia">function countstrange(places, fixedstart=1)
possibles = [
[2, 3, 5, 7],
Line 1,185 ⟶ 1,658:
countstrange(10)
@time countstrange(10)
</langsyntaxhighlight>{{out}}
<pre>
Found 853423 10-digit strange numbers with the most significant digit 1.
Line 1,195 ⟶ 1,668:
=={{header|Kotlin}}==
{{trans|Java}}
<langsyntaxhighlight lang="scala">import kotlin.math.abs
 
fun digits(n: Int): List<Int> {
Line 1,239 ⟶ 1,712:
}
println()
}</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100..500]
Line 1,255 ⟶ 1,728:
 
=={{header|MAD}}==
<langsyntaxhighlight MADlang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(D)
Line 1,288 ⟶ 1,761:
 
VECTOR VALUES F = $I4*$
END OF PROGRAM </langsyntaxhighlight>
{{out}}
<pre style='height:50ex;'>STRANGE NUMBERS
Line 1,380 ⟶ 1,853:
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>
Select[Range[101, 499],
AllTrue[Partition[IntegerDigits[#], 2, 1], Differences /* First /* PrimeQ] &
]</langsyntaxhighlight>
{{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.
 
<langsyntaxhighlight Nimlang="nim">import algorithm, sequtils
 
const PrimeDigits = [2, 3, 5, 7]
Line 1,427 ⟶ 1,946:
 
result = findStrangeNumbers(10).filterIt(it div 1_000_000_000 == 1)
echo "\nFound ", result.len, " strange numbers with 10 digits and starting with 1."</langsyntaxhighlight>
 
{{out}}
Line 1,444 ⟶ 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,530 ⟶ 2,049:
Writeln;
Writeln('Count : ',cnt);
end.</langsyntaxhighlight>
{{out}}
<pre>
Line 1,548 ⟶ 2,067:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">use strict;
use warnings;
use feature 'say';
Line 1,562 ⟶ 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,573 ⟶ 2,092:
 
=={{header|Phix}}==
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<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>
Line 1,595 ⟶ 2,114:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,602 ⟶ 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).
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<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>
<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>
Line 1,636 ⟶ 2,155:
<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>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 1,671 ⟶ 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,731 ⟶ 2,389:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Strange numbers in range [100..500]
Line 1,746 ⟶ 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,754 ⟶ 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,767 ⟶ 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,800 ⟶ 2,495:
exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</langsyntaxhighlight>
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
Line 1,816 ⟶ 2,511:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
load "stdlib.ring"
 
Line 1,842 ⟶ 2,537:
ok
next
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,855 ⟶ 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,896 ⟶ 2,633:
xs.each_slice(10) do |s|
print s, "\n"
end</langsyntaxhighlight>
{{out}}
<pre>Strange numbers in range [100 .. 500]
Line 1,912 ⟶ 2,649:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">func generate_from_prefix(limit, p, base) {
 
var seq = [p]
Line 1,939 ⟶ 2,676:
strange_numbers(500).grep { _ > 100 }.slices(10).each{
.join(' ').say
}</langsyntaxhighlight>
{{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>
Line 1,955 ⟶ 2,787:
=={{header|Wren}}==
===Basic task===
<langsyntaxhighlight ecmascriptlang="wren">var primes = [2, 3, 5, 7]
var count = 0
var d = []
Line 1,973 ⟶ 2,805:
}
if (count % 10 != 0) System.print()
System.print("\n%(count) strange numbers in all.")</langsyntaxhighlight>
 
{{out}}
Line 1,996 ⟶ 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 2,017 ⟶ 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