Strange numbers: Difference between revisions

Content added Content deleted
(J)
m (syntax highlighting fixup automation)
Line 16: Line 16:
{{trans|Python}}
{{trans|Python}}


<lang 11l>F is_strange(n)
<syntaxhighlight lang="11l">F is_strange(n)
V xs = String(n).map(c -> Int(c))
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)))
R all(zip(xs, xs[1..]).map((a, b) -> abs(a - b) C (2, 3, 5, 7)))
Line 26: Line 26:
print(el, end' ‘ ’)
print(el, end' ‘ ’)
I L.index % 10 == 9
I L.index % 10 == 9
print()</lang>
print()</syntaxhighlight>


{{out}}
{{out}}
Line 47: Line 47:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE Func IsStrangeNumber(INT i)
<syntaxhighlight lang="action!">BYTE Func IsStrangeNumber(INT i)
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0]
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0]
BYTE d,diff,prev
BYTE d,diff,prev
Line 81: Line 81:
OD
OD
PrintF("%E%EThere are %I strange numbers",count)
PrintF("%E%EThere are %I strange numbers",count)
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_numbers.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_numbers.png Screenshot from Atari 8-bit computer]
Line 94: Line 94:


=={{header|Ada}}==
=={{header|Ada}}==
<lang Ada>with Ada.Text_IO; use Ada.Text_IO;
<syntaxhighlight lang="ada">with Ada.Text_IO; use Ada.Text_IO;


procedure Strange_Numbers is
procedure Strange_Numbers is
Line 134: Line 134:
end loop;
end loop;
Put_Line ("Strange numbers in range 1_000_000_000 .. 1_999_999_999: " & Count'Image);
Put_Line ("Strange numbers in range 1_000_000_000 .. 1_999_999_999: " & Count'Image);
end Strange_Numbers;</lang>
end Strange_Numbers;</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 152: Line 152:


=={{header|ALGOL W}}==
=={{header|ALGOL W}}==
<lang algolw>begin % find "strange numbers" - numbers where digits differ from the next %
<syntaxhighlight lang="algolw">begin % find "strange numbers" - numbers where digits differ from the next %
% by a prime %
% by a prime %
% returns true if n is strange, false otherwise %
% returns true if n is strange, false otherwise %
Line 190: Line 190:
end for_n
end for_n
end
end
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 201: Line 201:


=={{header|APL}}==
=={{header|APL}}==
<lang APL>(⊢(/⍨)(∧/2 3 5 7∊⍨2+/⍎¨∘⍕)¨) 100+⍳400</lang>
<syntaxhighlight lang="apl">(⊢(/⍨)(∧/2 3 5 7∊⍨2+/⍎¨∘⍕)¨) 100+⍳400</syntaxhighlight>
{{out}}
{{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
<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 207: Line 207:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang AppleScript>--------------------- STRANGE NUMBERS --------------------
<syntaxhighlight lang="applescript">--------------------- STRANGE NUMBERS --------------------


-- isStrange :: Int -> Bool
-- isStrange :: Int -> Bool
Line 418: Line 418:
end tell
end tell
end if
end if
end zipWith</lang>
end zipWith</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Strange numbers found in range [100..500]
<pre>Strange numbers found in range [100..500]
Line 437: Line 437:


=={{header|AWK}}==
=={{header|AWK}}==
<syntaxhighlight lang="awk">
<lang AWK>
# syntax: GAWK -f STRANGE_NUMBERS.AWK
# syntax: GAWK -f STRANGE_NUMBERS.AWK
BEGIN {
BEGIN {
Line 469: Line 469:
}
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 485: Line 485:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang basic>10 DEFINT I,J,K: DEFSTR S
<syntaxhighlight lang="basic">10 DEFINT I,J,K: DEFSTR S
20 FOR I=100 TO 500
20 FOR I=100 TO 500
30 S=STR$(I)
30 S=STR$(I)
Line 493: Line 493:
70 NEXT J
70 NEXT J
80 PRINT I,
80 PRINT I,
90 NEXT I</lang>
90 NEXT I</syntaxhighlight>
{{out}}
{{out}}
<pre> 130 131 135 136 138
<pre> 130 131 135 136 138
Line 514: Line 514:
496 497</pre>
496 497</pre>
=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


let dgtprime(x) = x=2 | x=3 | x=5 | x=7
let dgtprime(x) = x=2 | x=3 | x=5 | x=7
Line 530: Line 530:
$)
$)
wrch('*N')
wrch('*N')
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
<pre> 130 131 135 136 138 141 142 146 147 149
Line 543: Line 543:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn>↑‿10⥊((∧´(|¯1↓«-⊢)∊⟨2,3,5,7⟩˙)○•Fmt)¨⊸/100+↕400</lang>
<syntaxhighlight lang="bqn">↑‿10⥊((∧´(|¯1↓«-⊢)∊⟨2,3,5,7⟩˙)○•Fmt)¨⊸/100+↕400</syntaxhighlight>
{{out}}
{{out}}
The unused positions in the table are filled with <code>0</code>s, because that's the behaviour
The unused positions in the table are filled with <code>0</code>s, because that's the behaviour
Line 562: Line 562:
=={{header|C}}==
=={{header|C}}==
{{trans|Go}}
{{trans|Go}}
<lang c>#include <stdbool.h>
<syntaxhighlight lang="c">#include <stdbool.h>
#include <stdio.h>
#include <stdio.h>


Line 600: Line 600:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Strange numbers in the open interval (100, 500) are:
<pre>Strange numbers in the open interval (100, 500) are:
Line 616: Line 616:


===Extended task===
===Extended task===
<lang c>#include <stdio.h>
<syntaxhighlight lang="c">#include <stdio.h>


const int next_digit[] = {
const int next_digit[] = {
Line 655: Line 655:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>% ./a.out | fmt
<pre>% ./a.out | fmt
Line 669: Line 669:
=={{header|C++}}==
=={{header|C++}}==
{{trans|Java}}
{{trans|Java}}
<lang cpp>#include <algorithm>
<syntaxhighlight lang="cpp">#include <algorithm>
#include <iostream>
#include <iostream>
#include <vector>
#include <vector>
Line 720: Line 720:


return 0;
return 0;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Strange numbers in range [100..500]
<pre>Strange numbers in range [100..500]
Line 736: Line 736:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>digits = iter (n: int) yields (int)
<syntaxhighlight lang="clu">digits = iter (n: int) yields (int)
while n>0 do
while n>0 do
yield(n // 10)
yield(n // 10)
Line 771: Line 771:
end
end
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
<pre>130 131 135 136 138 141 142 146 147 149
Line 784: Line 784:


=={{header|COBOL}}==
=={{header|COBOL}}==
<lang cobol> IDENTIFICATION DIVISION.
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION.
PROGRAM-ID. STRANGE-NUMBERS.
PROGRAM-ID. STRANGE-NUMBERS.
Line 819: Line 819:
MOVE NUM TO N-OUT.
MOVE NUM TO N-OUT.
DISPLAY N-OUT.
DISPLAY N-OUT.
DONE. EXIT.</lang>
DONE. EXIT.</syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'>130
<pre style='height:50ex;'>130
Line 910: Line 910:


=={{header|Cowgol}}==
=={{header|Cowgol}}==
<lang cowgol>include "cowgol.coh";
<syntaxhighlight lang="cowgol">include "cowgol.coh";


sub abs(n: int8): (r: uint8) is
sub abs(n: int8): (r: uint8) is
Line 945: Line 945:
n := n + 1;
n := n + 1;
end loop;
end loop;
print_nl();</lang>
print_nl();</syntaxhighlight>
{{out}}
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
<pre>130 131 135 136 138 141 142 146 147 149
Line 958: Line 958:


=={{header|Draco}}==
=={{header|Draco}}==
<lang draco>proc nonrec strange(word n) bool:
<syntaxhighlight lang="draco">proc nonrec strange(word n) bool:
bool is_strange;
bool is_strange;
short da, db, diff;
short da, db, diff;
Line 986: Line 986:
fi
fi
od
od
corp</lang>
corp</syntaxhighlight>
{{out}}
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
<pre>130 131 135 136 138 141 142 146 147 149
Line 999: Line 999:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>
<syntaxhighlight lang="fsharp">
// Strange numbers. Nigel Galloway: February 25th., 2021
// 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))
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 ""
[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}}
{{out}}
<pre>
<pre>
Line 1,012: Line 1,012:
===Identifying and filtering===
===Identifying and filtering===
{{works with|Factor|0.99 2020-08-14}}
{{works with|Factor|0.99 2020-08-14}}
<lang factor>USING: grouping io kernel math.ranges math.statistics
<syntaxhighlight lang="factor">USING: grouping io kernel math.ranges math.statistics
math.text.utils math.vectors prettyprint sequences ;
math.text.utils math.vectors prettyprint sequences ;


Line 1,022: Line 1,022:
100 500 (a,b) [ strange? ] filter dup
100 500 (a,b) [ strange? ] filter dup
10 group [ [ pprint bl ] each nl ] each nl
10 group [ [ pprint bl ] each nl ] each nl
length pprint " strange numbers found." print</lang>
length pprint " strange numbers found." print</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,043: Line 1,043:
Since we know the next possible digits based on the current last digit, we can construct strange numbers with a backtracking algorithm.
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}}
{{works with|Factor|0.99 2020-08-14}}
<lang factor>USING: backtrack compiler.tree.propagation.call-effect
<syntaxhighlight lang="factor">USING: backtrack compiler.tree.propagation.call-effect
formatting io kernel sequences sequences.generalizations
formatting io kernel sequences sequences.generalizations
tools.memory.private tools.time ;
tools.memory.private tools.time ;
Line 1,065: Line 1,065:
dup length commas write
dup length commas write
" 10-digit strange numbers beginning with 1:" print
" 10-digit strange numbers beginning with 1:" print
[ first2 ] [ last2 ] bi "%u\n%u\n...\n%u\n%u\n" printf</lang>
[ first2 ] [ last2 ] bi "%u\n%u\n...\n%u\n%u\n" printf</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,079: Line 1,079:


=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
<lang freebasic>function is_strange( n as ulongint ) as boolean
<syntaxhighlight lang="freebasic">function is_strange( n as ulongint ) as boolean
dim as uinteger d, ld, a
dim as uinteger d, ld, a
if n<10 then return true 'arbitrary, but makes the recursion easier
if n<10 then return true 'arbitrary, but makes the recursion easier
Line 1,098: Line 1,098:
if is_strange(n) then c+=1
if is_strange(n) then c+=1
next n
next n
print using "There are ####### 10-digit strange numbers beginning with 1.";c</lang>
print using "There are ####### 10-digit strange numbers beginning with 1.";c</syntaxhighlight>
{{out}}<pre>
{{out}}<pre>
Strange numbers between 100 and 500
Strange numbers between 100 and 500
Line 1,106: Line 1,106:


=={{header|Forth}}==
=={{header|Forth}}==
<lang forth>\ tests whether n is prime for 0 <= n < 10
<syntaxhighlight lang="forth">\ tests whether n is prime for 0 <= n < 10
: prime? ( n -- ? )
: prime? ( n -- ? )
1 swap lshift 0xac and 0<> ;
1 swap lshift 0xac and 0<> ;
Line 1,136: Line 1,136:


main
main
bye</lang>
bye</syntaxhighlight>


{{out}}
{{out}}
Line 1,154: Line 1,154:
===Basic task===
===Basic task===
{{trans|Wren}}
{{trans|Wren}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,188: Line 1,188:
}
}
fmt.Printf("\n%d strange numbers in all.\n", count)
fmt.Printf("\n%d strange numbers in all.\n", count)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,209: Line 1,209:
===Stretch goal===
===Stretch goal===
{{trans|Julia}}
{{trans|Julia}}
<lang go>package main
<syntaxhighlight lang="go">package main


import "fmt"
import "fmt"
Line 1,239: Line 1,239:
}
}
fmt.Println("Found", len(strangeOnes), places, "\b-digit strange numbers beginning with", start)
fmt.Println("Found", len(strangeOnes), places, "\b-digit strange numbers beginning with", start)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,247: Line 1,247:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (intercalate)
<syntaxhighlight lang="haskell">import Data.List (intercalate)
import Data.List.Split (chunksOf)
import Data.List.Split (chunksOf)


Line 1,270: Line 1,270:
unlines
unlines
(unwords <$> chunksOf 10 (show <$> xs))
(unwords <$> chunksOf 10 (show <$> xs))
]</lang>
]</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Strange numbers found in range [100..500]
<pre>Strange numbers found in range [100..500]
Line 1,290: Line 1,290:
=={{header|J}}==
=={{header|J}}==


<lang J>isStrange=: ([: */ 2 3 5 7 e.&:|~ 2 -/\ 10 #.inv ])"0
<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</lang>
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 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.
Line 1,300: Line 1,300:
Task examples:
Task examples:


<lang J> (#~ 100&<:) I. isStrange i.500
<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 ...
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
#{{ ;<@{{x,"1 0 I. ({:x){y }}&strangePair"1 y}}^:9 ,:1
853423</lang>
853423</syntaxhighlight>


=={{header|Java}}==
=={{header|Java}}==
{{trans|Python}}
{{trans|Python}}
<lang Java>import java.util.LinkedList;
<syntaxhighlight lang="java">import java.util.LinkedList;
import java.util.List;
import java.util.List;
import java.util.function.BiPredicate;
import java.util.function.BiPredicate;
Line 1,359: Line 1,359:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Strange numbers in range [100..500]
<pre>Strange numbers in range [100..500]
Line 1,379: Line 1,379:


===Filter method===
===Filter method===
<syntaxhighlight lang="jq">
<lang jq>
def is_strange:
def is_strange:
def digits: tostring | explode | map([.] | implode | tonumber);
def digits: tostring | explode | map([.] | implode | tonumber);
Line 1,401: Line 1,401:


task(100; 500; is_strange)
task(100; 500; is_strange)
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,420: Line 1,420:
===Generator method===
===Generator method===
`lpad` and and `nwise` are as above and so their definitions are not repeated.
`lpad` and and `nwise` are as above and so their definitions are not repeated.
<syntaxhighlight lang="jq">
<lang jq>
# input: width
# input: width
# $start: maximum first digit allowed e.g. 9 for unrestricted
# $start: maximum first digit allowed e.g. 9 for unrestricted
Line 1,442: Line 1,442:
[3 | generate(4) | map(tostring) | join("") | tonumber]
[3 | generate(4) | map(tostring) | join("") | tonumber]
| nwise(10) | map(lpad(3)) | join(" ")
| nwise(10) | map(lpad(3)) | join(" ")
</syntaxhighlight>
</lang>
{{out}}
{{out}}
As above, except for the header.
As above, except for the header.
===Stretch goal===
===Stretch goal===
Using generate/1 as already defined:
Using generate/1 as already defined:
<syntaxhighlight lang="jq">
<lang jq>
def count(s): reduce s as $x (null; .+1);
def count(s): reduce s as $x (null; .+1);
count(10 | generate(1))
count(10 | generate(1))
</lang>
</syntaxhighlight>


{{out}}
{{out}}
Line 1,459: Line 1,459:
=={{header|Julia}}==
=={{header|Julia}}==
===Filter method===
===Filter method===
<lang julia>isstrange(n::Integer) = (d = digits(n); all(i -> abs(d[i] - d[i + 1]) ∈ [2, 3, 5, 7], 1:length(d)-1))
<syntaxhighlight 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)
function filter_open_interval(start, stop, f, doprint=true, rowlength=92)
Line 1,475: Line 1,475:


filter_open_interval(100, 500, isstrange)
filter_open_interval(100, 500, isstrange)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Finding numbers matching isstrange for open interval (100, 500):
Finding numbers matching isstrange for open interval (100, 500):
Line 1,488: Line 1,488:
===Generator method===
===Generator method===
HT: Factor for the table.
HT: Factor for the table.
<lang julia>function countstrange(places, fixedstart=1)
<syntaxhighlight lang="julia">function countstrange(places, fixedstart=1)
possibles = [
possibles = [
[2, 3, 5, 7],
[2, 3, 5, 7],
Line 1,514: Line 1,514:
countstrange(10)
countstrange(10)
@time countstrange(10)
@time countstrange(10)
</lang>{{out}}
</syntaxhighlight>{{out}}
<pre>
<pre>
Found 853423 10-digit strange numbers with the most significant digit 1.
Found 853423 10-digit strange numbers with the most significant digit 1.
Line 1,524: Line 1,524:
=={{header|Kotlin}}==
=={{header|Kotlin}}==
{{trans|Java}}
{{trans|Java}}
<lang scala>import kotlin.math.abs
<syntaxhighlight lang="scala">import kotlin.math.abs


fun digits(n: Int): List<Int> {
fun digits(n: Int): List<Int> {
Line 1,568: Line 1,568:
}
}
println()
println()
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>Strange numbers in range [100..500]
<pre>Strange numbers in range [100..500]
Line 1,584: Line 1,584:


=={{header|MAD}}==
=={{header|MAD}}==
<lang MAD> NORMAL MODE IS INTEGER
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER
INTERNAL FUNCTION(D)
INTERNAL FUNCTION(D)
Line 1,617: Line 1,617:


VECTOR VALUES F = $I4*$
VECTOR VALUES F = $I4*$
END OF PROGRAM </lang>
END OF PROGRAM </syntaxhighlight>
{{out}}
{{out}}
<pre style='height:50ex;'>STRANGE NUMBERS
<pre style='height:50ex;'>STRANGE NUMBERS
Line 1,709: Line 1,709:


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
<lang Mathematica>
Select[Range[101, 499],
Select[Range[101, 499],
AllTrue[Partition[IntegerDigits[#], 2, 1], Differences /* First /* PrimeQ] &
AllTrue[Partition[IntegerDigits[#], 2, 1], Differences /* First /* PrimeQ] &
]</lang>
]</syntaxhighlight>
{{out}}
{{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>
<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}}==
=={{header|Modula-2}}==
<lang modula2>MODULE StrangeNumbers;
<syntaxhighlight lang="modula2">MODULE StrangeNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
FROM InOut IMPORT WriteCard, WriteLn;


Line 1,750: Line 1,750:
END;
END;
WriteLn
WriteLn
END StrangeNumbers.</lang>
END StrangeNumbers.</syntaxhighlight>
{{out}}
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
<pre> 130 131 135 136 138 141 142 146 147 149
Line 1,765: Line 1,765:
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.
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.


<lang Nim>import algorithm, sequtils
<syntaxhighlight lang="nim">import algorithm, sequtils


const PrimeDigits = [2, 3, 5, 7]
const PrimeDigits = [2, 3, 5, 7]
Line 1,802: Line 1,802:


result = findStrangeNumbers(10).filterIt(it div 1_000_000_000 == 1)
result = findStrangeNumbers(10).filterIt(it div 1_000_000_000 == 1)
echo "\nFound ", result.len, " strange numbers with 10 digits and starting with 1."</lang>
echo "\nFound ", result.len, " strange numbers with 10 digits and starting with 1."</syntaxhighlight>


{{out}}
{{out}}
Line 1,819: Line 1,819:
Not dynamically memorizing of all numbers saves much time 200ms -> 4ms.<BR>
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
Count is about 4.6 to the power of Digitscount -> first digit 1 followed by 9 digit -> 4.6^9 = 922190
<lang pascal>program strangenumbers;
<syntaxhighlight lang="pascal">program strangenumbers;


const
const
Line 1,905: Line 1,905:
Writeln;
Writeln;
Writeln('Count : ',cnt);
Writeln('Count : ',cnt);
end.</lang>
end.</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,923: Line 1,923:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>use strict;
<syntaxhighlight lang="perl">use strict;
use warnings;
use warnings;
use feature 'say';
use feature 'say';
Line 1,937: Line 1,937:
my $cnt = my @strange = grep { is_strange($_) } $low+1 .. $high-1;
my $cnt = my @strange = grep { is_strange($_) } $low+1 .. $high-1;
say "Between $low and $high there are $cnt strange numbers:\n" .
say "Between $low and $high there are $cnt strange numbers:\n" .
(sprintf "@{['%5d' x $cnt]}", @strange[0..$cnt-1]) =~ s/(.{80})/$1\n/gr;</lang>
(sprintf "@{['%5d' x $cnt]}", @strange[0..$cnt-1]) =~ s/(.{80})/$1\n/gr;</syntaxhighlight>
{{out}}
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
<pre>Between 100 and 500 there are 87 strange numbers:
Line 1,948: Line 1,948:


=={{header|Phix}}==
=={{header|Phix}}==
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="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: #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>
<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,970: Line 1,970:
<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: #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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 1,977: Line 1,977:
=== stretch goal ===
=== 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).
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).
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="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: #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>
<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 2,011: Line 2,011:
<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>
<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>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,048: Line 2,048:


=={{header|PL/I}}==
=={{header|PL/I}}==
<lang pli>strangeNumbers: procedure options(main);
<syntaxhighlight lang="pli">strangeNumbers: procedure options(main);
strange: procedure(nn) returns(bit);
strange: procedure(nn) returns(bit);
declare (n, nn) fixed;
declare (n, nn) fixed;
Line 2,076: Line 2,076:
end;
end;
end;
end;
end strangeNumbers;</lang>
end strangeNumbers;</syntaxhighlight>
{{out}}
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
<pre> 130 131 135 136 138 141 142 146 147 149
Line 2,089: Line 2,089:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang pli>100H:
<syntaxhighlight lang="pli">100H:
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
EXIT: PROCEDURE; GO TO 0; END EXIT;
Line 2,134: Line 2,134:
END;
END;
CALL EXIT;
CALL EXIT;
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
<pre>130 131 135 136 138 141 142 146 147 149
Line 2,147: Line 2,147:


=={{header|Python}}==
=={{header|Python}}==
<lang python>'''Strange Numbers'''
<syntaxhighlight lang="python">'''Strange Numbers'''




Line 2,205: Line 2,205:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Strange numbers in range [100..500]
<pre>Strange numbers in range [100..500]
Line 2,222: Line 2,222:


=={{header|Raku}}==
=={{header|Raku}}==
<lang perl6>sub infix:<′>(\x,\y) { abs(x - y) ∈ (2, 3, 5, 7) }
<syntaxhighlight lang="raku" line>sub infix:<′>(\x,\y) { abs(x - y) ∈ (2, 3, 5, 7) }


my ($low,$high) = 100, 500;
my ($low,$high) = 100, 500;
Line 2,228: Line 2,228:


say "Between $low and $high there are {+@strange} strange numbers:\n";
say "Between $low and $high there are {+@strange} strange numbers:\n";
print @strange.batch(20)».fmt("%4d").join: "\n";</lang>
print @strange.batch(20)».fmt("%4d").join: "\n";</syntaxhighlight>
{{out}}
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
<pre>Between 100 and 500 there are 87 strange numbers:
Line 2,241: Line 2,241:
Some optimization was added to bypass calculating the absolute value of adjacent decimal digit differences, &nbsp;
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.
<br>as well as parsing of a number to determine the differences.
<lang rexx>/*REXX pgm lists strange integers (within a range) whose decimal digs differ by a prime.*/
<syntaxhighlight 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. */
numeric digits 20 /*allow processing of larger numbers. */
parse arg LO HI bd . /*obtain optional arguments from the CL*/
parse arg LO HI bd . /*obtain optional arguments from the CL*/
Line 2,274: Line 2,274:
exit 0
exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</lang>
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:}}
{{out|output|text=&nbsp; when using the default inputs:}}
<pre>
<pre>
Line 2,290: Line 2,290:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
load "stdlib.ring"
load "stdlib.ring"


Line 2,316: Line 2,316:
ok
ok
next
next
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,333: Line 2,333:
=={{header|Ruby}}==
=={{header|Ruby}}==
{{trans|Java}}
{{trans|Java}}
<lang ruby>def digits(n)
<syntaxhighlight lang="ruby">def digits(n)
result = []
result = []
while n > 0
while n > 0
Line 2,370: Line 2,370:
xs.each_slice(10) do |s|
xs.each_slice(10) do |s|
print s, "\n"
print s, "\n"
end</lang>
end</syntaxhighlight>
{{out}}
{{out}}
<pre>Strange numbers in range [100 .. 500]
<pre>Strange numbers in range [100 .. 500]
Line 2,386: Line 2,386:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>func generate_from_prefix(limit, p, base) {
<syntaxhighlight lang="ruby">func generate_from_prefix(limit, p, base) {


var seq = [p]
var seq = [p]
Line 2,413: Line 2,413:
strange_numbers(500).grep { _ > 100 }.slices(10).each{
strange_numbers(500).grep { _ > 100 }.slices(10).each{
.join(' ').say
.join(' ').say
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 2,430: Line 2,430:
===Basic task===
===Basic task===
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn is_prime(nn int) bool {
<syntaxhighlight lang="vlang">fn is_prime(nn int) bool {
mut n := nn
mut n := nn
if n < 0 {
if n < 0 {
Line 2,461: Line 2,461:
}
}
println("\n$count strange numbers in all.")
println("\n$count strange numbers in all.")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,482: Line 2,482:
=={{header|Wren}}==
=={{header|Wren}}==
===Basic task===
===Basic task===
<lang ecmascript>var primes = [2, 3, 5, 7]
<syntaxhighlight lang="ecmascript">var primes = [2, 3, 5, 7]
var count = 0
var count = 0
var d = []
var d = []
Line 2,500: Line 2,500:
}
}
if (count % 10 != 0) System.print()
if (count % 10 != 0) System.print()
System.print("\n%(count) strange numbers in all.")</lang>
System.print("\n%(count) strange numbers in all.")</syntaxhighlight>


{{out}}
{{out}}
Line 2,523: Line 2,523:
... though I've generated the 'possibles' table rather than hard-code it.
... though I've generated the 'possibles' table rather than hard-code it.
Runtime about 0.085 seconds which is quick for Wren.
Runtime about 0.085 seconds which is quick for Wren.
<lang ecmascript>var diffs = [-7, -5, -3, -2, 2, 3, 5, 7]
<syntaxhighlight lang="ecmascript">var diffs = [-7, -5, -3, -2, 2, 3, 5, 7]
var possibles = List.filled(10, null)
var possibles = List.filled(10, null)
for (i in 0..9) {
for (i in 0..9) {
Line 2,544: Line 2,544:
}
}


System.print("Found %(strangeOnes.count) %(places)-digit strange numbers beginning with %(start).")</lang>
System.print("Found %(strangeOnes.count) %(places)-digit strange numbers beginning with %(start).")</syntaxhighlight>


{{out}}
{{out}}
Line 2,553: Line 2,553:
=={{header|XPL0}}==
=={{header|XPL0}}==
Takes 49 seconds on Raspberry Pi 4.
Takes 49 seconds on Raspberry Pi 4.
<lang XPL0>func Strange(N);
<syntaxhighlight lang="xpl0">func Strange(N);
int N, A, B, D;
int N, A, B, D;
[N:= N/10;
[N:= N/10;
Line 2,580: Line 2,580:
Cnt:= Cnt+1;
Cnt:= Cnt+1;
IntOut(0, Cnt);
IntOut(0, Cnt);
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}