Strange plus numbers: Difference between revisions
(Added Easylang) |
|||
(16 intermediate revisions by 10 users not shown) | |||
Line 13: | Line 13: | ||
{{trans|Python}} |
{{trans|Python}} |
||
< |
<syntaxhighlight lang="11l">F is_strange_plus(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) -> a + b C (2, 3, 5, 7, 11, 13, 17))) |
R all(zip(xs, xs[1..]).map((a, b) -> a + b C (2, 3, 5, 7, 11, 13, 17))) |
||
Line 23: | Line 23: | ||
print(el, end' ‘ ’) |
print(el, end' ‘ ’) |
||
I L.index % 10 == 9 |
I L.index % 10 == 9 |
||
print()</ |
print()</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 42: | Line 42: | ||
=={{header|Action!}}== |
=={{header|Action!}}== |
||
< |
<syntaxhighlight lang="action!">BYTE Func IsStrangePlusNumber(INT i) |
||
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0] |
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0] |
||
BYTE d,prev,first,sum |
BYTE d,prev,first,sum |
||
Line 77: | Line 77: | ||
OD |
OD |
||
PrintF("%E%EThere are %I strange plus numbers",count) |
PrintF("%E%EThere are %I strange plus numbers",count) |
||
RETURN</ |
RETURN</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_plus_numbers.png Screenshot from Atari 8-bit computer] |
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_plus_numbers.png Screenshot from Atari 8-bit computer] |
||
Line 90: | Line 90: | ||
=={{header|ALGOL 68}}== |
=={{header|ALGOL 68}}== |
||
Does not attempt to generalise beyond 3 digit numbers. |
Does not attempt to generalise beyond 3 digit numbers. |
||
< |
<syntaxhighlight lang="algol68">BEGIN # find numbers where the sum of the first 2 digits is prime and also # |
||
# the sum of the second 2 digits is prime # |
# the sum of the second 2 digits is prime # |
||
# considers numbers n where 100 < n < 500 # |
# considers numbers n where 100 < n < 500 # |
||
Line 105: | Line 105: | ||
FI |
FI |
||
OD |
OD |
||
END</ |
END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 119: | Line 119: | ||
=={{header|ALGOL W}}== |
=={{header|ALGOL W}}== |
||
{{Trans|ALGOL 68}} |
{{Trans|ALGOL 68}} |
||
< |
<syntaxhighlight lang="pascal">begin % find numbers where the sum of the first 2 digits is prime and also % |
||
% the sum of the second 2 digits is prime % |
% the sum of the second 2 digits is prime % |
||
% considers numbers n where 100 < n < 500 % |
% considers numbers n where 100 < n < 500 % |
||
Line 139: | Line 139: | ||
end for_n |
end for_n |
||
end. |
end. |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 153: | Line 153: | ||
=={{header|APL}}== |
=={{header|APL}}== |
||
{{works with|Dyalog APL}} |
{{works with|Dyalog APL}} |
||
< |
<syntaxhighlight lang="apl">(∧⌿ 2 3 5 7 11 13 17 ∊⍨ 2 +⌿ 10 (⊥⍣¯1) X)/X←100+⍳399</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 163: | Line 163: | ||
=={{header|AppleScript}}== |
=={{header|AppleScript}}== |
||
< |
<syntaxhighlight lang="applescript">------------------- STRANGE PLUS NUMBERS ----------------- |
||
-- isStrangePlus :: Int -> Bool |
-- isStrangePlus :: Int -> Bool |
||
Line 362: | Line 362: | ||
end tell |
end tell |
||
end if |
end if |
||
end zipWith</ |
end zipWith</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>'Strange Plus' numbers found in range [100..500] |
<pre>'Strange Plus' numbers found in range [100..500] |
||
Line 377: | Line 377: | ||
389 411 412 414 416 430 432 434 438 470 |
389 411 412 414 416 430 432 434 438 470 |
||
474 476 492 494 498</pre> |
474 476 492 494 498</pre> |
||
=={{header|Arturo}}== |
|||
<syntaxhighlight lang="arturo">strangeNums: select 101..499 'x -> |
|||
and? -> prime? sum first.n:2 digits x |
|||
-> prime? sum last.n:2 digits x |
|||
loop split.every: 5 strangeNums 'x -> |
|||
print map x 's -> pad to :string s 3</syntaxhighlight> |
|||
{{out}} |
|||
<pre>111 112 114 116 120 |
|||
121 123 125 129 141 |
|||
143 147 149 161 165 |
|||
167 202 203 205 207 |
|||
211 212 214 216 230 |
|||
232 234 238 250 252 |
|||
256 258 292 294 298 |
|||
302 303 305 307 320 |
|||
321 323 325 329 341 |
|||
343 347 349 383 385 |
|||
389 411 412 414 416 |
|||
430 432 434 438 470 |
|||
474 476 492 494 498</pre> |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<syntaxhighlight lang="awk"> |
|||
<lang AWK> |
|||
# syntax: GAWK -f STRANGE_PLUS_NUMBERS.AWK |
# syntax: GAWK -f STRANGE_PLUS_NUMBERS.AWK |
||
BEGIN { |
BEGIN { |
||
Line 405: | Line 430: | ||
return(1) |
return(1) |
||
} |
} |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 419: | Line 444: | ||
=={{header|BASIC}}== |
=={{header|BASIC}}== |
||
< |
<syntaxhighlight lang="gwbasic">10 DEFINT A-Z |
||
20 FOR I=100 TO 500 |
20 FOR I=100 TO 500 |
||
30 N=I |
30 N=I |
||
Line 428: | Line 453: | ||
80 R=N MOD 10 |
80 R=N MOD 10 |
||
90 IF INSTR("CDFHLNR",CHR$(L+R+65)) THEN 50 |
90 IF INSTR("CDFHLNR",CHR$(L+R+65)) THEN 50 |
||
100 NEXT I</ |
100 NEXT I</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 111 112 114 116 120 |
<pre> 111 112 114 116 120 |
||
Line 445: | Line 470: | ||
=={{header|BCPL}}== |
=={{header|BCPL}}== |
||
< |
<syntaxhighlight lang="bcpl">get "libhdr" |
||
let smallprime(n) = n=2 | n=3 | n=5 | n=7 | n=11 | n=13 | n=17 |
let smallprime(n) = n=2 | n=3 | n=5 | n=7 | n=11 | n=13 | n=17 |
||
Line 462: | Line 487: | ||
$) |
$) |
||
wrch('*N') |
wrch('*N') |
||
$) </ |
$) </syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 473: | Line 498: | ||
=={{header|BQN}}== |
=={{header|BQN}}== |
||
< |
<syntaxhighlight lang="bqn">∘‿13⥊(∧´2‿3‿5‿7‿11‿13‿17∊˜(+˝∘⍉2↕•Fmt-'0'˙))¨⊸/100+↕400</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>┌─ |
<pre>┌─ |
||
Line 486: | Line 511: | ||
Generalized solution: a number is strange iff the sum of two consecutive digits is always prime. Numbers < 10 are considered non-strange. |
Generalized solution: a number is strange iff the sum of two consecutive digits is always prime. Numbers < 10 are considered non-strange. |
||
< |
<syntaxhighlight lang="c">#include <stdio.h> |
||
static int p[19] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, |
static int p[19] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0, |
||
Line 509: | Line 534: | ||
} |
} |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 523: | Line 548: | ||
=={{header|C++}}== |
=={{header|C++}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="cpp">#include <iostream> |
||
#include <vector> |
#include <vector> |
||
Line 563: | Line 588: | ||
test(101, 499); |
test(101, 499); |
||
return 0; |
return 0; |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 574: | Line 599: | ||
=={{header|CLU}}== |
=={{header|CLU}}== |
||
< |
<syntaxhighlight lang="clu">small_prime = proc (n: int) returns (bool) |
||
small_primes = sequence[int]$[2,3,5,7,11,13,17] |
small_primes = sequence[int]$[2,3,5,7,11,13,17] |
||
for p: int in sequence[int]$elements(small_primes) do |
for p: int in sequence[int]$elements(small_primes) do |
||
Line 602: | Line 627: | ||
end |
end |
||
end |
end |
||
end start_up</ |
end start_up</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 111 112 114 116 120 121 123 125 129 141 |
<pre> 111 112 114 116 120 121 123 125 129 141 |
||
Line 613: | Line 638: | ||
=={{header|COBOL}}== |
=={{header|COBOL}}== |
||
< |
<syntaxhighlight lang="cobol"> IDENTIFICATION DIVISION. |
||
PROGRAM-ID. STRANGE-PLUS-NUMBERS. |
PROGRAM-ID. STRANGE-PLUS-NUMBERS. |
||
Line 651: | Line 676: | ||
DISPLAY ROW, |
DISPLAY ROW, |
||
MOVE SPACES TO ROW, |
MOVE SPACES TO ROW, |
||
MOVE 1 TO PTR.</ |
MOVE 1 TO PTR.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 662: | Line 687: | ||
=={{header|Comal}}== |
=={{header|Comal}}== |
||
< |
<syntaxhighlight lang="comal">0010 FUNC small'prime#(n#) |
||
0020 RETURN n#=2 OR (n# MOD 2<>0 AND n#<>1 AND n#<>9 AND n#<>15) |
0020 RETURN n#=2 OR (n# MOD 2<>0 AND n#<>1 AND n#<>9 AND n#<>15) |
||
0030 ENDFUNC small'prime# |
0030 ENDFUNC small'prime# |
||
Line 687: | Line 712: | ||
0240 ENDFOR i# |
0240 ENDFOR i# |
||
0250 PRINT |
0250 PRINT |
||
0260 END</ |
0260 END</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 698: | Line 723: | ||
=={{header|Cowgol}}== |
=={{header|Cowgol}}== |
||
< |
<syntaxhighlight lang="cowgol">include "cowgol.coh"; |
||
sub small_prime(n: uint8): (p: uint8) is |
sub small_prime(n: uint8): (p: uint8) is |
||
Line 731: | Line 756: | ||
cand := cand + 1; |
cand := cand + 1; |
||
end loop; |
end loop; |
||
print_nl();</ |
print_nl();</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 742: | Line 767: | ||
=={{header|Delphi}}== |
=={{header|Delphi}}== |
||
{{libheader| System.SysUtils}} |
|||
{{Trans|Go}} |
{{Trans|Go}} |
||
<syntaxhighlight lang="delphi"> |
|||
<lang Delphi> |
|||
program Strange_plus_numbers; |
program Strange_plus_numbers; |
||
Line 787: | Line 811: | ||
writeln(#10, count, ' strange plus numbers in all.'); |
writeln(#10, count, ' strange plus numbers in all.'); |
||
readln; |
readln; |
||
end.</ |
end.</syntaxhighlight> |
||
=== Alternate solution === |
|||
<syntaxhighlight lang="delphi">program StrangePlusNumbers; |
|||
const |
|||
m = 18; |
|||
var |
|||
pr: array [0..m] of Boolean; |
|||
n, k, a, b: Integer; |
|||
q: Boolean; |
|||
begin |
|||
// prime sieve |
|||
for n := 0 to m do |
|||
begin |
|||
q := n > 1; |
|||
for k := 2 to n - 1 do |
|||
if n mod k = 0 then q := False; |
|||
pr[n] := q |
|||
end; |
|||
k := 0; |
|||
for n := 101 to 499 do |
|||
begin |
|||
a := n div 10; // first two digits |
|||
b := n mod 100; // last two digits |
|||
if pr[a div 10 + a mod 10] and |
|||
pr[b div 10 + b mod 10] then |
|||
begin |
|||
Write(n); |
|||
Inc(k); |
|||
if k mod 10 = 0 then Writeln else Write(' ') |
|||
end |
|||
end |
|||
end.</syntaxhighlight> |
|||
{{out}} |
|||
<pre>111 112 114 116 120 121 123 125 129 141 |
|||
143 147 149 161 165 167 202 203 205 207 |
|||
211 212 214 216 230 232 234 238 250 252 |
|||
256 258 292 294 298 302 303 305 307 320 |
|||
321 323 325 329 341 343 347 349 383 385 |
|||
389 411 412 414 416 430 432 434 438 470 |
|||
474 476 492 494 498</pre> |
|||
=={{header|Draco}}== |
=={{header|Draco}}== |
||
< |
<syntaxhighlight lang="draco">proc small_prime(word n) bool: |
||
word primes = 0x8A2B; |
word primes = 0x8A2B; |
||
n>=2 and primes >> (n-2) & 1 = 1 |
n>=2 and primes >> (n-2) & 1 = 1 |
||
Line 820: | Line 891: | ||
fi |
fi |
||
od |
od |
||
corp</ |
corp</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 111 112 114 116 120 121 123 125 129 141 |
<pre> 111 112 114 116 120 121 123 125 129 141 |
||
Line 829: | Line 900: | ||
389 411 412 414 416 430 432 434 438 470 |
389 411 412 414 416 430 432 434 438 470 |
||
474 476 492 494 498</pre> |
474 476 492 494 498</pre> |
||
=={{header|EasyLang}}== |
|||
<syntaxhighlight> |
|||
fastfunc isprim num . |
|||
if num < 2 |
|||
return 0 |
|||
. |
|||
i = 2 |
|||
while i <= sqrt num |
|||
if num mod i = 0 |
|||
return 0 |
|||
. |
|||
i += 1 |
|||
. |
|||
return 1 |
|||
. |
|||
func strange n . |
|||
while n >= 1000 |
|||
n = n div 10 |
|||
. |
|||
d1 = n mod 10 |
|||
n = n div 10 |
|||
d2 = n mod 10 |
|||
n = n div 10 |
|||
d3 = n mod 10 |
|||
if isprim (d1 + d2) = 1 and isprim (d2 + d3) = 1 |
|||
return 1 |
|||
. |
|||
return 0 |
|||
. |
|||
for i = 100 to 499 |
|||
if strange i = 1 |
|||
write i & " " |
|||
. |
|||
. |
|||
</syntaxhighlight> |
|||
{{out}} |
|||
<pre> |
|||
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 |
|||
</pre> |
|||
=={{header|F_Sharp|F#}}== |
=={{header|F_Sharp|F#}}== |
||
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br> |
This task uses [[Extensible_prime_generator#The_functions|Extensible Prime Generator (F#)]].<br> |
||
< |
<syntaxhighlight lang="fsharp"> |
||
// Strange numbers. Nigel Galloway: February 25th., 2021 |
// Strange numbers. Nigel Galloway: February 25th., 2021 |
||
let pD=[0..99]|>List.map(fun n->(n/10,n%10))|>List.filter(fun(n,g)->isPrime(n+g)) |
let pD=[0..99]|>List.map(fun n->(n/10,n%10))|>List.filter(fun(n,g)->isPrime(n+g)) |
||
pD|>List.filter(fun(n,_)->n>0)|>List.map(fun(n,g)->(n,pD|>List.filter(fun(n,_)->n=g))) |
pD|>List.filter(fun(n,_)->n>0)|>List.map(fun(n,g)->(n,pD|>List.filter(fun(n,_)->n=g))) |
||
|>List.collect(fun(n,g)->g|>List.map(fun(g,k)->n*100+g*10+k))|>List.filter((>)500)|>List.iter(printf "%d ");printfn "" |
|>List.collect(fun(n,g)->g|>List.map(fun(g,k)->n*100+g*10+k))|>List.filter((>)500)|>List.iter(printf "%d ");printfn "" |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 845: | Line 956: | ||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
{{works with|Factor|0.99 2021-02-05}} |
{{works with|Factor|0.99 2021-02-05}} |
||
< |
<syntaxhighlight lang="factor">USING: grouping grouping.extras io kernel math math.primes |
||
math.ranges math.text.utils prettyprint sequences ; |
math.ranges math.text.utils prettyprint sequences ; |
||
Line 855: | Line 966: | ||
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 plus numbers found." print</ |
length pprint " strange plus numbers found." print</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 873: | Line 984: | ||
=={{header|Forth}}== |
=={{header|Forth}}== |
||
{{trans|C}} |
{{trans|C}} |
||
< |
<syntaxhighlight lang="forth">create isprime false , false , true , true , false , |
||
true , false , true , false , false , false , true , |
true , false , true , false , false , false , true , |
||
false , true , false , false , false , true , false , |
false , true , false , false , false , true , false , |
||
Line 905: | Line 1,016: | ||
main |
main |
||
bye</ |
bye</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 921: | Line 1,032: | ||
=={{header|FreeBASIC}}== |
=={{header|FreeBASIC}}== |
||
{{trans|AWK}} |
{{trans|AWK}} |
||
< |
<syntaxhighlight lang="freebasic"> |
||
Function isPrime(valor As Integer) As Boolean |
Function isPrime(valor As Integer) As Boolean |
||
If valor <= 1 Then Return False |
If valor <= 1 Then Return False |
||
Line 946: | Line 1,057: | ||
Print !"\n\n"; k; " n£meros m s extra¤os encontrados." |
Print !"\n\n"; k; " n£meros m s extra¤os encontrados." |
||
Sleep |
Sleep |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 964: | Line 1,075: | ||
=={{header|Fōrmulæ}}== |
=={{header|Fōrmulæ}}== |
||
{{FormulaeEntry|page=https://formulae.org/?script=examples/Strange_plus_numbers}} |
|||
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition. |
|||
'''Solution''' |
|||
Programs in Fōrmulæ are created/edited online in its [https://formulae.org website], However they run on execution servers. By default remote servers are used, but they are limited in memory and processing power, since they are intended for demonstration and casual use. A local server can be downloaded and installed, it has no limitations (it runs in your own computer). Because of that, example programs can be fully visualized and edited, but some of them will not run if they require a moderate or heavy computation/memory resources, and no local server is being used. |
|||
[[File:Fōrmulæ - Strange plus numbers 01.png]] |
|||
In '''[https://formulae.org/?example=Strange_plus_numbers this]''' page you can see the program(s) related to this task and their results. |
|||
[[File:Fōrmulæ - Strange plus numbers 02.png]] |
|||
[[File:Fōrmulæ - Strange plus numbers 03.png]] |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
{{trans|Wren}} |
{{trans|Wren}} |
||
< |
<syntaxhighlight lang="go">package main |
||
import "fmt" |
import "fmt" |
||
Line 1,003: | Line 1,118: | ||
} |
} |
||
fmt.Printf("\n%d strange plus numbers in all.\n", count) |
fmt.Printf("\n%d strange plus numbers in all.\n", count) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,021: | Line 1,136: | ||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
< |
<syntaxhighlight lang="haskell">import Data.List (intercalate) |
||
import Data.List.Split (chunksOf) |
import Data.List.Split (chunksOf) |
||
Line 1,045: | Line 1,160: | ||
unlines |
unlines |
||
(unwords <$> chunksOf 10 (show <$> xs)) |
(unwords <$> chunksOf 10 (show <$> xs)) |
||
]</ |
]</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>"Strange Plus" numbers found in range [100..500] |
<pre>"Strange Plus" numbers found in range [100..500] |
||
Line 1,060: | Line 1,175: | ||
389 411 412 414 416 430 432 434 438 470 |
389 411 412 414 416 430 432 434 438 470 |
||
474 476 492 494 498</pre> |
474 476 492 494 498</pre> |
||
=={{header|J}}== |
|||
Definitions: |
|||
<syntaxhighlight lang="j">digits=: 10&#.inv"0 |
|||
strangeplus=: 100&< * 500&> * (2&{. * ::0:&(1&p:)&(+/) _2&{.)@digits</syntaxhighlight> |
|||
Example: |
|||
<syntaxhighlight lang="j"> I.strangeplus i.500 |
|||
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498</syntaxhighlight> |
|||
=={{header|Java}}== |
=={{header|Java}}== |
||
< |
<syntaxhighlight lang="java">public class Strange { |
||
private static final boolean[] p = { |
private static final boolean[] p = { |
||
false, false, true, true, false, |
false, false, true, true, false, |
||
Line 1,090: | Line 1,216: | ||
} |
} |
||
} |
} |
||
}</ |
}</syntaxhighlight> |
||
<lang>java Strange 101 499</ |
<syntaxhighlight lang="text">java Strange 101 499</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,109: | Line 1,235: | ||
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
See e.g. [[Erd%C5%91s-primes#jq]] for a suitable implementation of `is_prime`. |
||
< |
<syntaxhighlight lang="jq">def nwise($n): |
||
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end; |
||
n; |
n; |
||
Line 1,124: | Line 1,250: | ||
| join(" "); |
| join(" "); |
||
task</ |
task</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,138: | Line 1,264: | ||
=={{header|Julia}}== |
=={{header|Julia}}== |
||
< |
<syntaxhighlight lang="julia">let |
||
smallprimes = [2, 3, 5, 7, 11, 13, 17] # 0 <= all of these primes <= 18 |
smallprimes = [2, 3, 5, 7, 11, 13, 17] # 0 <= all of these primes <= 18 |
||
paired_digit_sums(n) = (d = digits(n); [sum(p) for p in zip(d[1:end-1], d[2:end])]) |
paired_digit_sums(n) = (d = digits(n); [sum(p) for p in zip(d[1:end-1], d[2:end])]) |
||
Line 1,148: | Line 1,274: | ||
end |
end |
||
end |
end |
||
</ |
</syntaxhighlight>{{out}} |
||
<pre> |
<pre> |
||
111 112 114 116 120 121 123 125 129 141 143 147 149 |
111 112 114 116 120 121 123 125 129 141 143 147 149 |
||
Line 1,159: | Line 1,285: | ||
=={{header|Kotlin}}== |
=={{header|Kotlin}}== |
||
{{trans|Java}} |
{{trans|Java}} |
||
< |
<syntaxhighlight lang="scala">val p = arrayOf( |
||
false, false, true, true, false, |
false, false, true, true, false, |
||
true, false, true, false, false, |
true, false, true, false, false, |
||
Line 1,198: | Line 1,324: | ||
fun main() { |
fun main() { |
||
test(101, 499) |
test(101, 499) |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 1,209: | Line 1,335: | ||
=={{header|MAD}}== |
=={{header|MAD}}== |
||
< |
<syntaxhighlight lang="mad"> NORMAL MODE IS INTEGER |
||
INTERNAL FUNCTION(X) |
INTERNAL FUNCTION(X) |
||
Line 1,235: | Line 1,361: | ||
TEST WHENEVER STGPLS.(I), PRINT RESULTS I |
TEST WHENEVER STGPLS.(I), PRINT RESULTS I |
||
END OF PROGRAM</ |
END OF PROGRAM</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre style='height:50ex;'>I = 111 |
<pre style='height:50ex;'>I = 111 |
||
Line 1,303: | Line 1,429: | ||
I = 498</pre> |
I = 498</pre> |
||
=={{header|Maple}}== |
=={{header|Maple}}== |
||
< |
<syntaxhighlight lang="maple">select(n->(u->isprime(add(u[1..2])) and isprime(add(u[2..3])))(convert(n,base,10)),[$101..499]);</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,315: | Line 1,441: | ||
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
=={{header|Mathematica}}/{{header|Wolfram Language}}== |
||
< |
<syntaxhighlight lang="mathematica">Select[Range[101, 499], PrimeQ[Total[IntegerDigits[#][[;; 2]]]] && PrimeQ[Total[IntegerDigits[#][[2 ;;]]]] &] |
||
Length[%]</ |
Length[%]</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>{111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498} |
<pre>{111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498} |
||
Line 1,322: | Line 1,448: | ||
=={{header|Modula-2}}== |
=={{header|Modula-2}}== |
||
< |
<syntaxhighlight lang="modula2">MODULE StrangePlusNumbers; |
||
FROM InOut IMPORT WriteCard, WriteLn; |
FROM InOut IMPORT WriteCard, WriteLn; |
||
Line 1,355: | Line 1,481: | ||
END; |
END; |
||
WriteLn; |
WriteLn; |
||
END StrangePlusNumbers.</ |
END StrangePlusNumbers.</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 111 112 114 116 120 121 123 125 129 141 |
<pre> 111 112 114 116 120 121 123 125 129 141 |
||
Line 1,366: | Line 1,492: | ||
=={{header|Nim}}== |
=={{header|Nim}}== |
||
< |
<syntaxhighlight lang="nim">const Primes = {2, 3, 5, 7, 11, 13, 17} |
||
proc digits(n: 100..999): array[3, int] = |
proc digits(n: 100..999): array[3, int] = |
||
Line 1,376: | Line 1,502: | ||
if d[0] + d[1] in Primes and d[1] + d[2] in Primes: |
if d[0] + d[1] in Primes and d[1] + d[2] in Primes: |
||
inc count |
inc count |
||
stdout.write n, if count mod 13 == 0: '\n' else: ' '</ |
stdout.write n, if count mod 13 == 0: '\n' else: ' '</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,387: | Line 1,513: | ||
=={{header|Perl}}== |
=={{header|Perl}}== |
||
{{libheader|ntheory}} |
{{libheader|ntheory}} |
||
< |
<syntaxhighlight lang="perl">use strict; |
||
use warnings; |
use warnings; |
||
use feature 'say'; |
use feature 'say'; |
||
Line 1,395: | Line 1,521: | ||
my $n = my @SP = grep { my @d = split ''; is_prime $d[0]+$d[1] and is_prime $d[1]+$d[2] } $low+1 .. $high-1; |
my $n = my @SP = grep { my @d = split ''; is_prime $d[0]+$d[1] and is_prime $d[1]+$d[2] } $low+1 .. $high-1; |
||
say "Between $low and $high there are $n strange-plus numbers:\n" . |
say "Between $low and $high there are $n strange-plus numbers:\n" . |
||
(sprintf "@{['%4d' x $n]}", @SP[0..$n-1]) =~ s/(.{80})/$1\n/gr;</ |
(sprintf "@{['%4d' x $n]}", @SP[0..$n-1]) =~ s/(.{80})/$1\n/gr;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>Between 100 and 500 there are 65 strange-plus numbers: |
<pre>Between 100 and 500 there are 65 strange-plus numbers: |
||
Line 1,405: | Line 1,531: | ||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Using the same approach as [[Strange_numbers#Phix]], so this should similarly scale/count easily to the 28-digit range. |
Using the same approach as [[Strange_numbers#Phix]], so this should similarly scale/count easily to the 28-digit range. |
||
<!--< |
<!--<syntaxhighlight lang="phix">(phixonline)--> |
||
<span style="color: #008080;">constant</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_sub</span><span style="color: #0000FF;">,{{</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">7</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: #008080;">constant</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_sub</span><span style="color: #0000FF;">,{{</span><span style="color: #7060A8;">get_primes</span><span style="color: #0000FF;">(-</span><span style="color: #000000;">7</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;">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> |
<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> |
||
Line 1,424: | Line 1,550: | ||
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">strange_plus</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_plus</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_plus 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_plus 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}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,431: | Line 1,557: | ||
=={{header|Picat}}== |
=={{header|Picat}}== |
||
< |
<syntaxhighlight lang="picat">main => |
||
L = [N : N in 100..500, S = N.to_string.map(to_int), |
L = [N : N in 100..500, S = N.to_string.map(to_int), |
||
prime(S[1]+S[2]), |
prime(S[1]+S[2]), |
||
Line 1,440: | Line 1,566: | ||
end, |
end, |
||
nl, |
nl, |
||
println(len=Len)</ |
println(len=Len)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,454: | Line 1,580: | ||
=={{header|PL/I}}== |
=={{header|PL/I}}== |
||
< |
<syntaxhighlight lang="pli">StrangePlusNumbers: procedure options(main); |
||
smallPrime: procedure(n) returns(bit); |
smallPrime: procedure(n) returns(bit); |
||
declare n fixed; |
declare n fixed; |
||
Line 1,479: | Line 1,605: | ||
end; |
end; |
||
end; |
end; |
||
end StrangePlusNumbers;</ |
end StrangePlusNumbers;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> 111 112 114 116 120 121 123 125 129 141 |
<pre> 111 112 114 116 120 121 123 125 129 141 |
||
Line 1,490: | Line 1,616: | ||
=={{header|PL/M}}== |
=={{header|PL/M}}== |
||
< |
<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 1,541: | Line 1,667: | ||
END; |
END; |
||
CALL EXIT; |
CALL EXIT; |
||
EOF</ |
EOF</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 1,554: | Line 1,680: | ||
Using [https://docs.sympy.org/latest/modules/ntheory.html sympy.isprime] |
Using [https://docs.sympy.org/latest/modules/ntheory.html sympy.isprime] |
||
< |
<syntaxhighlight lang="python">Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] on win32 |
||
Type "help", "copyright", "credits" or "license()" for more information. |
Type "help", "copyright", "credits" or "license()" for more information. |
||
>>> from sympy import isprime |
>>> from sympy import isprime |
||
Line 1,562: | Line 1,688: | ||
isprime(sum(int(c) for c in str(x)[1:]))] |
isprime(sum(int(c) for c in str(x)[1:]))] |
||
[111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498] |
[111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498] |
||
>>> </ |
>>> </syntaxhighlight> |
||
Or, as we may not need to wake up '''sympy''' just to check membership of {2, 3, 5, 7, 11, 13, 17}: |
Or, as we may not need to wake up '''sympy''' just to check membership of {2, 3, 5, 7, 11, 13, 17}: |
||
< |
<syntaxhighlight lang="python">'''Strange Plus Numbers''' |
||
Line 1,625: | Line 1,751: | ||
# MAIN --- |
# MAIN --- |
||
if __name__ == '__main__': |
if __name__ == '__main__': |
||
main()</ |
main()</syntaxhighlight> |
||
{{Out}} |
{{Out}} |
||
<pre>"Strange Plus" numbers in range [100..500] |
<pre>"Strange Plus" numbers in range [100..500] |
||
Line 1,638: | Line 1,764: | ||
389 411 412 414 416 430 432 434 438 470 |
389 411 412 414 416 430 432 434 438 470 |
||
474 476 492 494 498</pre> |
474 476 492 494 498</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 + prime not iff |
|||
[ 2drop false ] done |
|||
+ prime ] is strange+ ( n --> b ) |
|||
[] 399 times |
|||
[ i^ 101 + |
|||
dup strange+ iff |
|||
join else drop ] |
|||
dup size echo |
|||
say " strange plus numbers:" |
|||
cr cr |
|||
witheach |
|||
[ echo |
|||
i^ 10 mod 9 = iff cr else sp ]</syntaxhighlight> |
|||
{{out}} |
|||
<pre>65 strange plus numbers: |
|||
111 112 114 116 120 121 123 125 129 141 |
|||
143 147 149 161 165 167 202 203 205 207 |
|||
211 212 214 216 230 232 234 238 250 252 |
|||
256 258 292 294 298 302 303 305 307 320 |
|||
321 323 325 329 341 343 347 349 383 385 |
|||
389 411 412 414 416 430 432 434 438 470 |
|||
474 476 492 494 498 |
|||
</pre> |
|||
=={{header|R}}== |
|||
<syntaxhighlight lang="rsplus"># Primes up to 18 |
|||
pr <- sapply(1:18, \(n) n > 1 && all(n %% seq(2, length.out = n - 2) > 0)) |
|||
is.strange <- function(n) { |
|||
a <- as.integer(strsplit(as.character(n), "")[[1]]) |
|||
pr[a[[1]] + a[[2]] + 1] && pr[a[[2]] + a[[3]] + 1] |
|||
} |
|||
a <- 101:499 |
|||
a[sapply(a, is.strange)]</syntaxhighlight> |
|||
{{out}} |
|||
<pre> [1] 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 |
|||
[24] 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 |
|||
[47] 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |
||
<lang |
<syntaxhighlight lang="raku" line>unit sub MAIN ($start = 100, $end = 500); |
||
put +$_, " matching numbers from $start to $end:\n", $_ given |
put +$_, " matching numbers from $start to $end:\n", $_ given |
||
($start .. $end).hyper(:256batch,:8degree).grep: { all .comb.rotor(2 => -1).map: { .sum.is-prime } };</ |
($start .. $end).hyper(:256batch,:8degree).grep: { all .comb.rotor(2 => -1).map: { .sum.is-prime } };</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>65 matching numbers from 100 to 500: |
<pre>65 matching numbers from 100 to 500: |
||
Line 1,648: | Line 1,828: | ||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
< |
<syntaxhighlight lang="rexx">/*REXX pgm lists strange+ integers (within a range); sum of adjacent dec. digs is prime.*/ |
||
parse arg LO HI . /*obtain optional arguments from the CL*/ |
parse arg LO HI . /*obtain optional arguments from the CL*/ |
||
if LO=='' | LO=="," then LO= 101 /*Not specified? Then use the default.*/ |
if LO=='' | LO=="," then LO= 101 /*Not specified? Then use the default.*/ |
||
Line 1,670: | Line 1,850: | ||
say # ' strange plus numbers found between ' LO " and " HI ' (inclusive)' |
say # ' strange plus numbers found between ' LO " and " HI ' (inclusive)' |
||
say |
say |
||
say strip($)</ |
say strip($)</syntaxhighlight> |
||
{{out|output|text= when using the default inputs:}} |
{{out|output|text= when using the default inputs:}} |
||
<pre> |
<pre> |
||
Line 1,682: | Line 1,862: | ||
=={{header|Ring}}== |
=={{header|Ring}}== |
||
< |
<syntaxhighlight lang="ring"> |
||
load "stdlib.ring" |
load "stdlib.ring" |
||
Line 1,708: | Line 1,888: | ||
ok |
ok |
||
next |
next |
||
</syntaxhighlight> |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,719: | Line 1,899: | ||
389 411 412 414 416 430 432 434 438 470 |
389 411 412 414 416 430 432 434 438 470 |
||
474 476 492 494 498 |
474 476 492 494 498 |
||
</pre> |
|||
=={{header|RPL}}== |
|||
{{works with|HP|49}} |
|||
≪ { } |
|||
<span style="color:red">101 499</span> '''FOR''' n |
|||
n <span style="color:red">10</span> IDIV2 SWAP <span style="color:red">10</span> IDIV2 |
|||
'''IF''' ROT OVER + ISPRIME? UNROT + ISPRIME? AND '''THEN''' n + '''END''' |
|||
'''NEXT''' |
|||
≫ '<span style="color:blue">TASK</span>' STO |
|||
{{out}} |
|||
<pre> |
|||
1: {111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498} |
|||
</pre> |
</pre> |
||
=={{header|Ruby}}== |
=={{header|Ruby}}== |
||
{{trans|Kotlin}} |
{{trans|Kotlin}} |
||
< |
<syntaxhighlight lang="ruby">$p = [ |
||
false, false, true, true, false, |
false, false, true, true, false, |
||
true, false, true, false, false, |
true, false, true, false, false, |
||
Line 1,760: | Line 1,953: | ||
end |
end |
||
test(101, 499)</ |
test(101, 499)</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 1,771: | Line 1,964: | ||
=={{header|Seed7}}== |
=={{header|Seed7}}== |
||
< |
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i"; |
||
const func boolean: prime (in integer: number) is |
const func boolean: prime (in integer: number) is |
||
Line 1,802: | Line 1,995: | ||
end if; |
end if; |
||
end for; |
end for; |
||
end func;</ |
end func;</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,811: | Line 2,004: | ||
412 414 416 430 432 434 438 470 474 476 492 494 498 |
412 414 416 430 432 434 438 470 474 476 492 494 498 |
||
</pre> |
</pre> |
||
=={{header|SETL}}== |
|||
<syntaxhighlight lang="setl">program strange_plus_numbers; |
|||
$ a sum of two digits is never >18 |
|||
primes := {2, 3, 5, 7, 11, 13, 17}; |
|||
strangeplus := { |
|||
n : n in [101..499] |
|||
| lowpairsum(n) in primes and highpairsum(n) in primes |
|||
}; |
|||
loop for n in strangeplus do |
|||
putchar(lpad(str n, 5)); |
|||
if (i +:= 1) mod 10 = 0 then print; end if; |
|||
end loop; |
|||
print; |
|||
proc lowpairsum(n); |
|||
return n mod 100 div 10 + n mod 10; |
|||
end proc; |
|||
proc highpairsum(n); |
|||
return lowpairsum(n div 10); |
|||
end proc; |
|||
end program;</syntaxhighlight> |
|||
{{out}} |
|||
<pre> 111 112 114 116 120 121 123 125 129 141 |
|||
143 147 149 161 165 167 202 203 205 207 |
|||
211 212 214 216 230 232 234 238 250 252 |
|||
256 258 292 294 298 302 303 305 307 320 |
|||
321 323 325 329 341 343 347 349 383 385 |
|||
389 411 412 414 416 430 432 434 438 470 |
|||
474 476 492 494 498</pre> |
|||
=={{header|Sidef}}== |
=={{header|Sidef}}== |
||
< |
<syntaxhighlight lang="ruby">100..500 -> map { .digits }.grep {|d| |
||
is_prime(d[-1]+d[-2]) && is_prime(d[-2]+d[-3]) |
is_prime(d[-1]+d[-2]) && is_prime(d[-2]+d[-3]) |
||
}.map{ .digits2num }.slices(10).each { .join(' ').say }</ |
}.map{ .digits2num }.slices(10).each { .join(' ').say }</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,827: | Line 2,053: | ||
</pre> |
</pre> |
||
=={{header|Vlang}}== |
=={{header|V (Vlang)}}== |
||
{{trans|Go}} |
{{trans|Go}} |
||
<lang vlang>fn is_prime(n int) bool { |
<syntaxhighlight lang="v (vlang)">fn is_prime(n int) bool { |
||
return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 |
return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17 |
||
} |
} |
||
Line 1,856: | Line 2,082: | ||
} |
} |
||
println("\n$count strange plus numbers in all.") |
println("\n$count strange plus numbers in all.") |
||
}</ |
}</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,874: | Line 2,100: | ||
=={{header|VTL-2}}== |
=={{header|VTL-2}}== |
||
< |
<syntaxhighlight lang="vtl2">10 C=0 |
||
20 N=101 |
20 N=101 |
||
30 K=N |
30 K=N |
||
Line 1,893: | Line 2,119: | ||
180 #=C/10*0+0<%*! |
180 #=C/10*0+0<%*! |
||
190 ?="" |
190 ?="" |
||
200 #=!</ |
200 #=!</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
<pre>111 112 114 116 120 121 123 125 129 141 |
<pre>111 112 114 116 120 121 123 125 129 141 |
||
Line 1,905: | Line 2,131: | ||
=={{header|Wren}}== |
=={{header|Wren}}== |
||
Simple brute force is adequate for this. |
Simple brute force is adequate for this. |
||
< |
<syntaxhighlight lang="wren">var primes = [2, 3, 5, 7, 11, 13, 17] |
||
var count = 0 |
var count = 0 |
||
var d = [] |
var d = [] |
||
Line 1,923: | Line 2,149: | ||
} |
} |
||
if (count % 10 != 0) System.print() |
if (count % 10 != 0) System.print() |
||
System.print("\n%(count) strange plus numbers in all.")</ |
System.print("\n%(count) strange plus numbers in all.")</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 1,944: | Line 2,170: | ||
A 16-bit solution for NASM under DOS. Assemble with <code>nasm -fbin strange.asm -o strange.com</code>. The prime sieve up to 18 is hard-coded. |
A 16-bit solution for NASM under DOS. Assemble with <code>nasm -fbin strange.asm -o strange.com</code>. The prime sieve up to 18 is hard-coded. |
||
<lang> org 100h |
<syntaxhighlight lang="text"> org 100h |
||
mov cx, 10 ; cl is used for division, ch to count numbers printed on a line |
mov cx, 10 ; cl is used for division, ch to count numbers printed on a line |
||
Line 1,991: | Line 2,217: | ||
p db 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0 |
p db 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0 |
||
k equ $-p</ |
k equ $-p</syntaxhighlight> |
||
{{out}} |
{{out}} |
||
Line 2,004: | Line 2,230: | ||
=={{header|XPL0}}== |
=={{header|XPL0}}== |
||
< |
<syntaxhighlight lang="xpl0">func StrangePlus(N); |
||
int N, A, B, S; |
int N, A, B, S; |
||
[N:= N/10; |
[N:= N/10; |
||
Line 2,023: | Line 2,249: | ||
IntOut(0, N); |
IntOut(0, N); |
||
if rem(Cnt/20) = 0 then CrLf(0) else ChOut(0, ^ )]; |
if rem(Cnt/20) = 0 then CrLf(0) else ChOut(0, ^ )]; |
||
]</ |
]</syntaxhighlight> |
||
{{out}} |
{{out}} |
Latest revision as of 19:50, 9 June 2024
n is a strange plus number if the sum of the first two digits is prime and the sum of the second two digits is also prime.
Where 100 < n < 500
- Related task
11l
F is_strange_plus(n)
V xs = String(n).map(c -> Int(c))
R all(zip(xs, xs[1..]).map((a, b) -> a + b C (2, 3, 5, 7, 11, 13, 17)))
V xs = (100..500).filter(n -> is_strange_plus(n))
print("\n\"Strange Plus\" numbers in range [100..500]\n")
print(‘(Total: ’String(xs.len)")\n")
L(el) xs
print(el, end' ‘ ’)
I L.index % 10 == 9
print()
- Output:
"Strange Plus" numbers in range [100..500] (Total: 65) 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Action!
BYTE Func IsStrangePlusNumber(INT i)
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0 0 1 0 1 0 0 0 1 0]
BYTE d,prev,first,sum
prev=255
first=1
WHILE i#0
DO
d=i MOD 10
IF prev#255 THEN
sum=d+prev
IF first=1 AND primes(sum)=0 THEN
RETURN (0)
FI
first=0
FI
prev=d
i==/10
OD
IF primes(sum)=0 THEN
RETURN (0)
FI
RETURN (1)
PROC Main()
INT i,count=[0]
FOR i=101 TO 499
DO
IF IsStrangePlusNumber(i) THEN
PrintI(i) Put(32)
count==+1
FI
OD
PrintF("%E%EThere are %I strange plus numbers",count)
RETURN
- Output:
Screenshot from Atari 8-bit computer
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 There are 65 strange plus numbers
ALGOL 68
Does not attempt to generalise beyond 3 digit numbers.
BEGIN # find numbers where the sum of the first 2 digits is prime and also #
# the sum of the second 2 digits is prime #
# considers numbers n where 100 < n < 500 #
PROC small prime = ( INT n )BOOL: n = 2 OR ( ODD n AND n /= 1 AND n /= 9 AND n /= 15 );
INT s count := 0;
FOR n FROM 101 TO 499 DO
INT v := n;
INT d1 = v MOD 10; v OVERAB 10;
INT d2 = v MOD 10; v OVERAB 10;
INT d3 = v;
IF small prime( d1 + d2 ) AND small prime( d2 + d3 ) THEN
print( ( " ", whole( n, -3 ) ) );
IF ( s count +:= 1 ) MOD 10 = 0 THEN print( ( newline ) ) FI
FI
OD
END
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
ALGOL W
begin % find numbers where the sum of the first 2 digits is prime and also %
% the sum of the second 2 digits is prime %
% considers numbers n where 100 < n < 500 %
logical procedure isSmallPrime ( integer value n ); n = 2 or ( odd( n ) and n not = 1 and n not = 9 and n not = 15 );
procedure divideBy ( integer value result n; integer value d ) ; n := n div d;
integer procedure inc ( integer value result n ); begin n := n + 1; n end;
integer sCount;
sCount := 0;
for n := 101 until 499 do begin
integer v, d1, d2, d3;
v := n;
d1 := v rem 10; divideBy( v, 10 );
d2 := v rem 10; divideBy( v, 10 );
d3 := v;
if isSmallPrime( d1 + d2 ) and isSmallPrime( d2 + d3 ) then begin
writeon( i_w := 3, s_w := 0, " ", n );
if inc( sCount ) rem 10 = 0 then write()
end if_small_primes
end for_n
end.
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
APL
(∧⌿ 2 3 5 7 11 13 17 ∊⍨ 2 +⌿ 10 (⊥⍣¯1) X)/X←100+⍳399
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
AppleScript
------------------- STRANGE PLUS NUMBERS -----------------
-- isStrangePlus :: Int -> Bool
on isStrangePlus(n)
set ds to digits(n)
script sumIsSmallPrime
on |λ|(a, b)
{2, 3, 5, 7, 11, 13, 17} contains (a + b)
end |λ|
end script
zipWith(sumIsSmallPrime, ds, rest of ds) does not contain false
end isStrangePlus
--------------------------- TEST -------------------------
on run
set xs to filter(isStrangePlus, enumFromTo(100, 500))
intercalate("\n\n", ¬
{"'Strange Plus' numbers found in range [100..500]", ¬
"Full list:", ¬
("(total " & (length of xs) as string) & ")", ¬
unlines(map(unwords, chunksOf(10, map(str, xs))))})
end run
------------------------- GENERIC ------------------------
-- chunksOf :: Int -> [a] -> [[a]]
on chunksOf(k, xs)
script
on go(ys)
set ab to splitAt(k, ys)
set a to item 1 of ab
if {} ≠ a then
{a} & go(item 2 of ab)
else
a
end if
end go
end script
result's go(xs)
end chunksOf
-- digits :: Int -> [Int]
on digits(n)
script go
on |λ|(x)
x as integer
end |λ|
end script
map(go, characters of (n as string))
end digits
-- enumFromTo :: Int -> Int -> [Int]
on enumFromTo(m, n)
if m ≤ n then
set lst to {}
repeat with i from m to n
set end of lst to i
end repeat
lst
else
{}
end if
end enumFromTo
-- intercalate :: String -> [String] -> String
on intercalate(delim, xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, delim}
set s to xs as text
set my text item delimiters to dlm
s
end intercalate
-- filter :: (a -> Bool) -> [a] -> [a]
on filter(p, xs)
tell mReturn(p)
set lst to {}
set lng to length of xs
repeat with i from 1 to lng
set v to item i of xs
if |λ|(v, i, xs) then set end of lst to v
end repeat
if {text, string} contains class of xs then
lst as text
else
lst
end if
end tell
end filter
-- mReturn :: First-class m => (a -> b) -> m (a -> b)
on mReturn(f)
-- 2nd class handler function lifted into 1st class script wrapper.
if script is class of f then
f
else
script
property |λ| : f
end script
end if
end mReturn
-- map :: (a -> b) -> [a] -> [b]
on map(f, xs)
-- The list obtained by applying f
-- to each element of xs.
tell mReturn(f)
set lng to length of xs
set lst to {}
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, i, xs)
end repeat
return lst
end tell
end map
-- min :: Ord a => a -> a -> a
on min(x, y)
if y < x then
y
else
x
end if
end min
-- splitAt :: Int -> [a] -> ([a], [a])
on splitAt(n, xs)
if n > 0 and n < length of xs then
if class of xs is text then
{items 1 thru n of xs as text, ¬
items (n + 1) thru -1 of xs as text}
else
{items 1 thru n of xs, items (n + 1) thru -1 of xs}
end if
else
if n < 1 then
{{}, xs}
else
{xs, {}}
end if
end if
end splitAt
-- str :: a -> String
on str(x)
x as string
end str
-- unlines :: [String] -> String
on unlines(xs)
-- A single string formed by the intercalation
-- of a list of strings with the newline character.
set {dlm, my text item delimiters} to ¬
{my text item delimiters, linefeed}
set s to xs as text
set my text item delimiters to dlm
s
end unlines
-- unwords :: [String] -> String
on unwords(xs)
set {dlm, my text item delimiters} to ¬
{my text item delimiters, space}
set s to xs as text
set my text item delimiters to dlm
return s
end unwords
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to min(length of xs, length of ys)
set lst to {}
if 1 > lng then
return {}
else
tell mReturn(f)
repeat with i from 1 to lng
set end of lst to |λ|(item i of xs, item i of ys)
end repeat
return lst
end tell
end if
end zipWith
- Output:
'Strange Plus' numbers found in range [100..500] Full list: (total 65) 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Arturo
strangeNums: select 101..499 'x ->
and? -> prime? sum first.n:2 digits x
-> prime? sum last.n:2 digits x
loop split.every: 5 strangeNums 'x ->
print map x 's -> pad to :string s 3
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
AWK
# syntax: GAWK -f STRANGE_PLUS_NUMBERS.AWK
BEGIN {
start = 100
stop = 500
for (i=start; i<=stop; i++) {
c1 = substr(i,1,1)
c2 = substr(i,2,1)
c3 = substr(i,3,1)
if (is_prime(c1 + c2) && is_prime(c2 + c3)) {
printf("%d%1s",i,++count%10?"":"\n")
}
}
printf("\nStrange plus numbers %d-%d: %d\n",start,stop,count)
exit(0)
}
function is_prime(x, i) {
if (x <= 1) {
return(0)
}
for (i=2; i<=int(sqrt(x)); i++) {
if (x % i == 0) {
return(0)
}
}
return(1)
}
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 Strange plus numbers 100-500: 65
BASIC
10 DEFINT A-Z
20 FOR I=100 TO 500
30 N=I
40 R=N MOD 10
50 IF N<10 THEN PRINT I,: GOTO 100
60 L=R
70 N=N\10
80 R=N MOD 10
90 IF INSTR("CDFHLNR",CHR$(L+R+65)) THEN 50
100 NEXT I
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
BCPL
get "libhdr"
let smallprime(n) = n=2 | n=3 | n=5 | n=7 | n=11 | n=13 | n=17
let strangeplus(n) =
n<10 -> true,
~smallprime(n rem 10 + (n/10) rem 10) -> false,
strangeplus(n / 10)
let start() be
$( let col = 0
for i = 100 to 500 if strangeplus(i)
$( writef("%I3 ",i)
col := col + 1
if col rem 10 = 0 then wrch('*N')
$)
wrch('*N')
$)
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
BQN
∘‿13⥊(∧´2‿3‿5‿7‿11‿13‿17∊˜(+˝∘⍉2↕•Fmt-'0'˙))¨⊸/100+↕400
- Output:
┌─ ╵ 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 ┘
C
Generalized solution: a number is strange iff the sum of two consecutive digits is always prime. Numbers < 10 are considered non-strange.
#include <stdio.h>
static int p[19] = {0, 0, 1, 1, 0, 1, 0, 1, 0, 0,
0, 1, 0, 1, 0, 0, 0, 1, 0};
int isstrange(long n) {
if (n < 10) return 0;
for (; n >= 10; n /= 10) {
if (!p[n%10 + (n/10)%10]) return 0;
}
return 1;
}
int main(void) {
long n;
int k = 0;
for (n = 101; n < 500; n++) {
if (isstrange(n)) {
printf("%d%c", n, ++k%10 ? ' ' : '\n');
}
}
return 0;
}
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
C++
#include <iostream>
#include <vector>
const std::vector<bool> p{
false, false, true, true, false,
true, false, true, false, false,
false, true, false, true, false,
false, false, true, false
};
bool isStrange(long n) {
if (n < 10) {
return false;
}
for (; n >= 10; n /= 10) {
if (!p[n % 10 + (n / 10) % 10]) {
return false;
}
}
return true;
}
void test(int nMin, int nMax) {
int k = 0;
for (long n = nMin; n <= nMax;n++) {
if (isStrange(n)) {
std::cout << n;
if (++k % 10 != 0) {
std::cout << ' ';
} else {
std::cout << '\n';
}
}
}
}
int main() {
test(101, 499);
return 0;
}
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
CLU
small_prime = proc (n: int) returns (bool)
small_primes = sequence[int]$[2,3,5,7,11,13,17]
for p: int in sequence[int]$elements(small_primes) do
if n=p then return(true) end
end
return(false)
end small_prime
strange_plus = proc (n: int) returns (bool)
while n >= 10 do
d1: int := n // 10
n := n / 10
d2: int := n // 10
if ~small_prime(d1 + d2) then return(false) end
end
return(true)
end strange_plus
start_up = proc ()
po: stream := stream$primary_input()
col: int := 0
for i: int in int$from_to(100,500) do
if strange_plus(i) then
stream$putright(po, int$unparse(i), 4)
col := col + 1
if col // 10 = 0 then stream$putl(po, "") end
end
end
end start_up
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
COBOL
IDENTIFICATION DIVISION.
PROGRAM-ID. STRANGE-PLUS-NUMBERS.
DATA DIVISION.
WORKING-STORAGE SECTION.
01 VARIABLES.
03 CANDIDATE PIC 999.
03 DIGITS REDEFINES CANDIDATE,
PIC 9 OCCURS 3 TIMES.
03 LEFT-PAIR PIC 99.
88 LEFT-PAIR-PRIME VALUES 2, 3, 5, 7, 11, 13, 17.
03 RIGHT-PAIR PIC 99.
88 RIGHT-PAIR-PRIME VALUES 2, 3, 5, 7, 11, 13, 17.
01 OUT.
03 ROW PIC X(40) VALUE SPACES.
03 PTR PIC 99 VALUE 1.
PROCEDURE DIVISION.
BEGIN.
PERFORM CHECK-STRANGE-NUMBER
VARYING CANDIDATE FROM 100 BY 1
UNTIL CANDIDATE IS GREATER THAN 500.
DISPLAY ROW.
STOP RUN.
CHECK-STRANGE-NUMBER.
ADD DIGITS(1), DIGITS(2) GIVING LEFT-PAIR.
ADD DIGITS(2), DIGITS(3) GIVING RIGHT-PAIR.
IF LEFT-PAIR-PRIME AND RIGHT-PAIR-PRIME,
PERFORM WRITE-STRANGE-NUMBER.
WRITE-STRANGE-NUMBER.
STRING CANDIDATE DELIMITED BY SIZE INTO ROW
WITH POINTER PTR.
ADD 1 TO PTR.
IF PTR IS GREATER THAN 40,
DISPLAY ROW,
MOVE SPACES TO ROW,
MOVE 1 TO PTR.
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Comal
0010 FUNC small'prime#(n#)
0020 RETURN n#=2 OR (n# MOD 2<>0 AND n#<>1 AND n#<>9 AND n#<>15)
0030 ENDFUNC small'prime#
0040 //
0050 FUNC strange'plus#(n#)
0060 dl#:=n# MOD 10
0070 WHILE n#>=10 DO
0080 dr#:=dl#
0090 n#:=n# DIV 10
0100 dl#:=n# MOD 10
0110 IF NOT small'prime#(dl#+dr#) THEN RETURN FALSE
0120 ENDWHILE
0130 RETURN TRUE
0140 ENDFUNC strange'plus#
0150 //
0160 ZONE 4
0170 col#:=0
0180 FOR i#:=100 TO 500 DO
0190 IF strange'plus#(i#) THEN
0200 PRINT i#,
0210 col#:+1
0220 IF col# MOD 10=0 THEN PRINT
0230 ENDIF
0240 ENDFOR i#
0250 PRINT
0260 END
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Cowgol
include "cowgol.coh";
sub small_prime(n: uint8): (p: uint8) is
var primes: uint32 := 0x228AC;
p := ((primes >> n) & 1) as uint8;
end sub;
sub strange_plus(n: uint16): (r: uint8) is
var dl := (n % 10) as uint8;
r := 1;
while n >= 10 and r != 0 loop
var dr := dl;
n := n / 10;
dl := (n % 10) as uint8;
r := r & small_prime(dl + dr);
end loop;
end sub;
var col: uint8 := 0;
var cand: uint16 := 100;
while cand < 500 loop
if strange_plus(cand) != 0 then
print_i16(cand);
col := col + 1;
if col == 10 then
print_nl();
col := 0;
else
print_char(' ');
end if;
end if;
cand := cand + 1;
end loop;
print_nl();
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Delphi
program Strange_plus_numbers;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
function IsPrime(n: Integer): Boolean;
begin
Result := n in [2, 3, 5, 7, 11, 13, 17];
end;
begin
var count := 0;
var d: TArray<Integer>;
writeln('Strange plus numbers in the open interval (100, 500) are:');
for var i := 101 to 499 do
begin
d := [];
var j := i;
while j > 0 do
begin
SetLength(d, Length(d) + 1);
d[High(d)] := j mod 10;
j := j div 10;
end;
if IsPrime(d[0] + d[1]) and IsPrime(d[1] + d[2]) then
begin
write(i, ' ');
inc(count);
if count mod 10 = 0 then
writeln;
end;
end;
if (count mod 10) <> 0 then
writeln;
writeln(#10, count, ' strange plus numbers in all.');
readln;
end.
Alternate solution
program StrangePlusNumbers;
const
m = 18;
var
pr: array [0..m] of Boolean;
n, k, a, b: Integer;
q: Boolean;
begin
// prime sieve
for n := 0 to m do
begin
q := n > 1;
for k := 2 to n - 1 do
if n mod k = 0 then q := False;
pr[n] := q
end;
k := 0;
for n := 101 to 499 do
begin
a := n div 10; // first two digits
b := n mod 100; // last two digits
if pr[a div 10 + a mod 10] and
pr[b div 10 + b mod 10] then
begin
Write(n);
Inc(k);
if k mod 10 = 0 then Writeln else Write(' ')
end
end
end.
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Draco
proc small_prime(word n) bool:
word primes = 0x8A2B;
n>=2 and primes >> (n-2) & 1 = 1
corp
proc strange_plus(word n) bool:
word dl, dr;
bool pair_prime;
dl := n % 10;
while
dr := dl;
n := n / 10;
dl := n % 10;
pair_prime := small_prime(dl + dr);
pair_prime and n >= 10
do od;
pair_prime and n < 10
corp
proc main() void:
byte col;
word cand;
col := 0;
for cand from 101 upto 499 do
if strange_plus(cand) then
write(cand:4);
col := col + 1;
if col = 10 then writeln(); col := 0 fi
fi
od
corp
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
EasyLang
fastfunc isprim num .
if num < 2
return 0
.
i = 2
while i <= sqrt num
if num mod i = 0
return 0
.
i += 1
.
return 1
.
func strange n .
while n >= 1000
n = n div 10
.
d1 = n mod 10
n = n div 10
d2 = n mod 10
n = n div 10
d3 = n mod 10
if isprim (d1 + d2) = 1 and isprim (d2 + d3) = 1
return 1
.
return 0
.
for i = 100 to 499
if strange i = 1
write i & " "
.
.
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
F#
This task uses Extensible Prime Generator (F#).
// Strange numbers. Nigel Galloway: February 25th., 2021
let pD=[0..99]|>List.map(fun n->(n/10,n%10))|>List.filter(fun(n,g)->isPrime(n+g))
pD|>List.filter(fun(n,_)->n>0)|>List.map(fun(n,g)->(n,pD|>List.filter(fun(n,_)->n=g)))
|>List.collect(fun(n,g)->g|>List.map(fun(g,k)->n*100+g*10+k))|>List.filter((>)500)|>List.iter(printf "%d ");printfn ""
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Factor
USING: grouping grouping.extras io kernel math math.primes
math.ranges math.text.utils prettyprint sequences ;
: strange+? ( n -- ? )
dup 10 < [ drop f ]
[ 1 digit-groups [ + ] 2 clump-map [ prime? ] all? ] if ;
"Strange plus numbers in (100, 500):" print nl
100 500 (a,b) [ strange+? ] filter dup
10 group [ [ pprint bl ] each nl ] each nl
length pprint " strange plus numbers found." print
- Output:
Strange plus numbers in (100, 500): 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 strange plus numbers found.
Forth
create isprime false , false , true , true , false ,
true , false , true , false , false , false , true ,
false , true , false , false , false , true , false ,
\ tests whether n is prime for 0 <= n < 19
: prime? ( n -- ? )
cells isprime + @ ;
: strange? ( n -- ? )
dup 10 < if drop false exit then
begin
dup 10 >=
while
dup 10 /mod 10 mod +
prime? invert if drop false exit then
10 /
repeat
drop true ;
: main
0
500 101 do
i strange? if
i .
1+
dup 10 mod 0= if cr then else
then
loop
cr
drop ;
main
bye
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
FreeBASIC
Function isPrime(valor As Integer) As Boolean
If valor <= 1 Then Return False
For i As Integer = 2 To Int(Sqr(valor))
If valor Mod i = 0 Then Return False
Next i
Return True
End Function
Dim As Integer k = 0
Print !"Los n£meros m s extra¤os son:\n"
For m As Integer = 100 To 500
Dim As Integer num1, num2, num3
num1 = Val(Mid(Str(m), 1, 1))
num2 = Val(Mid(Str(m), 2, 1))
num3 = Val(Mid(Str(m), 3, 1))
If isPrime(num1 + num2) And isPrime(num2 + num3) Then
Print Using "####"; m;
k += 1
If k Mod 10 = 0 Then Print
End If
Next m
Print !"\n\n"; k; " n£meros m s extra¤os encontrados."
Sleep
- Output:
Los números más extraños son: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 números más extraños encontrados.
Fōrmulæ
Fōrmulæ programs are not textual, visualization/edition of programs is done showing/manipulating structures but not text. Moreover, there can be multiple visual representations of the same program. Even though it is possible to have textual representation —i.e. XML, JSON— they are intended for storage and transfer purposes more than visualization and edition.
Programs in Fōrmulæ are created/edited online in its website.
In this page you can see and run the program(s) related to this task and their results. You can also change either the programs or the parameters they are called with, for experimentation, but remember that these programs were created with the main purpose of showing a clear solution of the task, and they generally lack any kind of validation.
Solution
Go
package main
import "fmt"
func isPrime(n int) bool {
return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17
}
func main() {
count := 0
var d []int
fmt.Println("Strange plus numbers in the open interval (100, 500) are:\n")
for i := 101; i < 500; i++ {
d = d[:0]
j := i
for j > 0 {
d = append(d, j%10)
j /= 10
}
if isPrime(d[0]+d[1]) && isPrime(d[1]+d[2]) {
fmt.Printf("%d ", i)
count++
if count%10 == 0 {
fmt.Println()
}
}
}
if count%10 != 0 {
fmt.Println()
}
fmt.Printf("\n%d strange plus numbers in all.\n", count)
}
- Output:
Strange plus numbers in the open interval (100, 500) are: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 strange plus numbers in all.
Haskell
import Data.List (intercalate)
import Data.List.Split (chunksOf)
------------------- STRANGE PLUS NUMBERS -----------------
isStrangePlus :: Int -> Bool
isStrangePlus n =
all
(\(a, b) -> (a + b) `elem` [2, 3, 5, 7, 11, 13, 17])
$ (zip <*> tail) (digits n)
digits :: Int -> [Int]
digits = fmap (read . return) . show
--------------------------- TEST -------------------------
main =
let xs = filter isStrangePlus [100 .. 500]
in (putStrLn . intercalate "\n\n")
[ "\"Strange Plus\" numbers found in range [100..500]",
"(total " <> (show . length) xs <> ")",
"Full list:",
unlines
(unwords <$> chunksOf 10 (show <$> xs))
]
- Output:
"Strange Plus" numbers found in range [100..500] (total 65) Full list: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
J
Definitions:
digits=: 10&#.inv"0
strangeplus=: 100&< * 500&> * (2&{. * ::0:&(1&p:)&(+/) _2&{.)@digits
Example:
I.strangeplus i.500
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Java
public class Strange {
private static final boolean[] p = {
false, false, true, true, false,
true, false, true, false, false,
false, true, false, true, false,
false, false, true, false
};
public static boolean isstrange(long n) {
if (n < 10) return false;
for (; n >= 10; n /= 10) {
if (!p[(int)(n%10 + (n/10)%10)]) return false;
}
return true;
}
public static void main(String[] args) {
long nMin = Long.parseLong(args[0]);
long nMax = Long.parseLong(args[1]);
int k = 0;
for (long n = nMin; n <= nMax; n++) {
if (isstrange(n)) {
System.out.print(n + (++k%10 != 0 ? " " : "\n"));
}
}
}
}
java Strange 101 499
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
jq
Works with gojq, the Go implementation of jq
See e.g. Erdős-primes#jq for a suitable implementation of `is_prime`.
def nwise($n):
def n: if length <= $n then . else .[0:$n] , (.[$n:] | n) end;
n;
def is_strange:
def sum($i): (.[$i:$i+1]|tonumber) + (.[$i+1:$i+2]|tonumber);
tostring
| length > 2 and (sum(0) | is_prime) and (sum(1) | is_prime) ;
def task:
[range(101; 500)
| select(is_strange)]
| nwise(10)
| join(" ");
task
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Julia
let
smallprimes = [2, 3, 5, 7, 11, 13, 17] # 0 <= all of these primes <= 18
paired_digit_sums(n) = (d = digits(n); [sum(p) for p in zip(d[1:end-1], d[2:end])])
isstrangeplus(n) = all(x -> x ∈ smallprimes, paired_digit_sums(n))
printed = 0
for n in 100:500
isstrangeplus(n) && print(n, (printed += 1) % 13 == 0 ? "\n" : " ")
end
end
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Kotlin
val p = arrayOf(
false, false, true, true, false,
true, false, true, false, false,
false, true, false, true, false,
false, false, true, false
)
fun isStrange(n: Long): Boolean {
if (n < 10) {
return false
}
var nn = n
while (nn >= 10) {
if (!p[(nn % 10 + (nn / 10) % 10).toInt()]) {
return false
}
nn /= 10
}
return true
}
fun test(nMin: Long, nMax: Long) {
var k = 0
for (n in nMin..nMax) {
if (isStrange(n)) {
print(n)
if (++k % 10 != 0) {
print(' ')
} else {
println()
}
}
}
}
fun main() {
test(101, 499)
}
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
MAD
NORMAL MODE IS INTEGER
INTERNAL FUNCTION(X)
ENTRY TO SMLPRM.
THROUGH TEST, FOR VALUES OF P = 2, 3, 5, 7, 11, 13, 17
TEST WHENEVER P.E.X, FUNCTION RETURN 1B
FUNCTION RETURN 0B
END OF FUNCTION
INTERNAL FUNCTION(NN)
ENTRY TO STGPLS.
N = NN
NX = N / 10
DA = N - NX * 10
STEP DB = DA
N = NX
NX = N / 10
DA = N - NX * 10
WHENEVER .NOT.SMLPRM.(DA + DB), FUNCTION RETURN 0B
WHENEVER N.L.10, FUNCTION RETURN 1B
TRANSFER TO STEP
END OF FUNCTION
THROUGH TEST, FOR I = 100, 1, I.GE.500
TEST WHENEVER STGPLS.(I), PRINT RESULTS I
END OF PROGRAM
- Output:
I = 111 I = 112 I = 114 I = 116 I = 120 I = 121 I = 123 I = 125 I = 129 I = 141 I = 143 I = 147 I = 149 I = 161 I = 165 I = 167 I = 202 I = 203 I = 205 I = 207 I = 211 I = 212 I = 214 I = 216 I = 230 I = 232 I = 234 I = 238 I = 250 I = 252 I = 256 I = 258 I = 292 I = 294 I = 298 I = 302 I = 303 I = 305 I = 307 I = 320 I = 321 I = 323 I = 325 I = 329 I = 341 I = 343 I = 347 I = 349 I = 383 I = 385 I = 389 I = 411 I = 412 I = 414 I = 416 I = 430 I = 432 I = 434 I = 438 I = 470 I = 474 I = 476 I = 492 I = 494 I = 498
Maple
select(n->(u->isprime(add(u[1..2])) and isprime(add(u[2..3])))(convert(n,base,10)),[$101..499]);
- Output:
[111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498]
Mathematica/Wolfram Language
Select[Range[101, 499], PrimeQ[Total[IntegerDigits[#][[;; 2]]]] && PrimeQ[Total[IntegerDigits[#][[2 ;;]]]] &]
Length[%]
- Output:
{111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498} 65
Modula-2
MODULE StrangePlusNumbers;
FROM InOut IMPORT WriteCard, WriteLn;
VAR i, col: CARDINAL;
PROCEDURE SmallPrime(n: CARDINAL): BOOLEAN;
BEGIN
RETURN (n=2) OR (n=3) OR (n=5) OR (n=7) OR (n=11) OR (n=13) OR (n=17)
END SmallPrime;
PROCEDURE StrangePlus(n: CARDINAL): BOOLEAN;
VAR l, r: CARDINAL;
BEGIN
r := n MOD 10;
WHILE n>=10 DO
l := r;
n := n DIV 10;
r := n MOD 10;
IF NOT SmallPrime(l+r) THEN RETURN FALSE END
END;
RETURN TRUE
END StrangePlus;
BEGIN
col := 0;
FOR i := 100 TO 500 DO
IF StrangePlus(i) THEN
WriteCard(i, 4);
INC(col);
IF col MOD 10 = 0 THEN WriteLn END
END
END;
WriteLn;
END StrangePlusNumbers.
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Nim
const Primes = {2, 3, 5, 7, 11, 13, 17}
proc digits(n: 100..999): array[3, int] =
[n div 100, n div 10 mod 10, n mod 10]
var count = 0
for n in 101..<500:
let d = n.digits
if d[0] + d[1] in Primes and d[1] + d[2] in Primes:
inc count
stdout.write n, if count mod 13 == 0: '\n' else: ' '
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Perl
use strict;
use warnings;
use feature 'say';
use ntheory 'is_prime';
my($low, $high) = (100, 500);
my $n = my @SP = grep { my @d = split ''; is_prime $d[0]+$d[1] and is_prime $d[1]+$d[2] } $low+1 .. $high-1;
say "Between $low and $high there are $n strange-plus numbers:\n" .
(sprintf "@{['%4d' x $n]}", @SP[0..$n-1]) =~ s/(.{80})/$1\n/gr;
- Output:
Between 100 and 500 there are 65 strange-plus numbers: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Phix
Using the same approach as Strange_numbers#Phix, so this should similarly scale/count easily to the 28-digit range.
constant poss = apply(true,sq_sub,{{get_primes(-7)},tagset(9,0)}), nxts = apply(true,filter,{poss,{"in"},{{0,9}},{"[]"}}) function strange_plus(integer left, sequence digits, res={}, part="") for i=1 to length(digits) do integer di = digits[i] string pn = part&di+'0' if left=1 then res = append(res,pn) else res = strange_plus(left-1,nxts[di+1],res,pn) end if end for return res end function sequence res = strange_plus(3,tagset(4)) -- (3 digit numbers beginning 1..4) printf(1,"%d strange_plus numbers found: %s\n",{length(res),join(shorten(res,"",5),",")})
- Output:
65 strange_plus numbers found: 111,112,114,116,120,...,474,476,492,494,498
Picat
main =>
L = [N : N in 100..500, S = N.to_string.map(to_int),
prime(S[1]+S[2]),
prime(S[2]+S[3])],
Len = L.len,
foreach({N,I} in zip(L,1..Len))
printf("%3d%s",N,cond(I mod 10 == 0, "\n", " "))
end,
nl,
println(len=Len)
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 len = 65
PL/I
StrangePlusNumbers: procedure options(main);
smallPrime: procedure(n) returns(bit);
declare n fixed;
return(n=2 | n=3 | n=5 | n=7 | n=11 | n=13 | n=17);
end smallPrime;
strangePlus: procedure(nn) returns(bit);
declare (n, nn, d1, d2) fixed;
do n=nn repeat(n/10) while(n>=10);
d1 = mod(n,10);
d2 = mod(n/10,10);
if ^smallPrime(d1+d2) then return('0'b);
end;
return('1'b);
end strangePlus;
declare (i, seen) fixed;
seen = 0;
do i=100 to 500;
if strangePlus(i) then do;
put edit(i) (F(4));
seen = seen + 1;
if mod(seen,10) = 0 then put skip;
end;
end;
end StrangePlusNumbers;
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
PL/M
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;
DECLARE TRUE LITERALLY '0FFH', FALSE LITERALLY '0';
/* PRINT NUMBER */
PRINT$NUM: PROCEDURE(N);
DECLARE (N, P) ADDRESS, C BASED P BYTE;
DECLARE S(6) BYTE INITIAL('.....$');
P = .S(5);
DIGIT:
P = P-1;
C = '0' + N MOD 10;
IF (N := N/10) > 0 THEN GO TO DIGIT;
CALL PRINT(P);
END PRINT$NUM;
/* SEE IF NUMBER IS A SMALL PRIME (<=18) */
SMALL$PRIME: PROCEDURE(N) BYTE;
DECLARE N BYTE;
RETURN N=2 OR N>2 AND SHR(8A2BH, N-2);
END SMALL$PRIME;
/* SEE IF A NUMBER IS A STRANGE PLUS NUMBER */
STRANGE$PLUS: PROCEDURE(N) BYTE;
DECLARE N ADDRESS;
DECLARE (D$LEFT, D$RIGHT) BYTE;
D$RIGHT = N MOD 10;
DO WHILE N >= 10;
D$LEFT = D$RIGHT;
N = N/10;
D$RIGHT = N MOD 10;
IF NOT SMALL$PRIME(D$LEFT + D$RIGHT) THEN RETURN FALSE;
END;
RETURN TRUE;
END STRANGE$PLUS;
/* CHECK NUMBERS IN RANGE 100..500 */
DECLARE CAND ADDRESS, COL BYTE INITIAL(0);
DO CAND = 100 TO 500;
IF STRANGE$PLUS(CAND) THEN DO;
CALL PRINT$NUM(CAND);
IF (COL := COL + 1) = 10 THEN DO;
CALL PRINT(.(13,10,'$'));
COL = 0;
END;
ELSE CALL PRINT(.' $');
END;
END;
CALL EXIT;
EOF
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Python
Using sympy.isprime
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>> from sympy import isprime
>>> [x for x in range(101,500)
if isprime(sum(int(c) for c in str(x)[:2])) and
isprime(sum(int(c) for c in str(x)[1:]))]
[111, 112, 114, 116, 120, 121, 123, 125, 129, 141, 143, 147, 149, 161, 165, 167, 202, 203, 205, 207, 211, 212, 214, 216, 230, 232, 234, 238, 250, 252, 256, 258, 292, 294, 298, 302, 303, 305, 307, 320, 321, 323, 325, 329, 341, 343, 347, 349, 383, 385, 389, 411, 412, 414, 416, 430, 432, 434, 438, 470, 474, 476, 492, 494, 498]
>>>
Or, as we may not need to wake up sympy just to check membership of {2, 3, 5, 7, 11, 13, 17}:
'''Strange Plus Numbers'''
# isStrangePlus :: Int -> Bool
def isStrangePlus(n):
'''True all consecutive decimal digit
pairs in n have prime sums.
'''
def test(a, b):
return a + b in [2, 3, 5, 7, 11, 13, 17]
xs = digits(n)
return all(map(test, xs, xs[1:]))
# ------------------- TEST AND DISPLAY -------------------
# main :: IO ()
def main():
'''List and count of Strange Plus Numbers'''
xs = [
n for n in range(100, 1 + 500)
if isStrangePlus(n)
]
print('\n"Strange Plus" numbers in range [100..500]\n')
print('(Total: ' + str(len(xs)) + ')\n')
print(
'\n'.join(
' '.join(
str(x) for x in row
) for row in chunksOf(10)(xs)
)
)
# ----------------------- GENERIC ------------------------
# chunksOf :: Int -> [a] -> [[a]]
def chunksOf(n):
'''A series of lists of length n, subdividing the
contents of xs. Where the length of xs is not evenly
divible, the final list will be shorter than n.
'''
def go(xs):
return (
xs[i:n + i] for i in range(0, len(xs), n)
) if 0 < n else None
return go
# digits :: Int -> [Int]
def digits(n):
'''Component digits of a decimal number.'''
return [int(c) for c in str(n)]
# MAIN ---
if __name__ == '__main__':
main()
- Output:
"Strange Plus" numbers in range [100..500] (Total: 65) 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Quackery
[ ' [ 2 3 5 7 11 13 17 ]
find 7 < ] is prime ( n --> b )
[ 10 /mod
swap 10 /mod
tuck + prime not iff
[ 2drop false ] done
+ prime ] is strange+ ( n --> b )
[] 399 times
[ i^ 101 +
dup strange+ iff
join else drop ]
dup size echo
say " strange plus numbers:"
cr cr
witheach
[ echo
i^ 10 mod 9 = iff cr else sp ]
- Output:
65 strange plus numbers: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
R
# Primes up to 18
pr <- sapply(1:18, \(n) n > 1 && all(n %% seq(2, length.out = n - 2) > 0))
is.strange <- function(n) {
a <- as.integer(strsplit(as.character(n), "")[[1]])
pr[a[[1]] + a[[2]] + 1] && pr[a[[2]] + a[[3]] + 1]
}
a <- 101:499
a[sapply(a, is.strange)]
- Output:
[1] 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 [24] 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 [47] 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Raku
unit sub MAIN ($start = 100, $end = 500);
put +$_, " matching numbers from $start to $end:\n", $_ given
($start .. $end).hyper(:256batch,:8degree).grep: { all .comb.rotor(2 => -1).map: { .sum.is-prime } };
- Output:
65 matching numbers from 100 to 500: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
REXX
/*REXX pgm lists strange+ integers (within a range); sum of adjacent dec. digs is prime.*/
parse arg LO HI . /*obtain optional arguments from the CL*/
if LO=='' | LO=="," then LO= 101 /*Not specified? Then use the default.*/
if HI=='' | HI=="," then HI= 499 /* " " " " " " */
!.= 0; !.2= 1; !.3= 1; !.5= 1; !.7= 1 /*build array of sums that are prime. */
!.11= 1; !.13= 1; !.17= 1 /* " " " " " " " */
$= /*the list of strange+ numbers (so far)*/
#= 0 /* " number " " " " " */
do j=LO to HI; L= length(j) /*look for strange+ numbers in range. */
if L==1 then iterate /*Number too short? Then skip it. */
do k=1 for L-1 /*examine the difference in the digits.*/
parse var j =(k) y +1 z +1 /*get two adjacent decimal digits: Y Z */
sum= y + z /*sum of two adjacent decimal digits. */
if \!.sum then iterate j /*Sum not prime? Then skip this number*/
end /*k*/
#= # + 1 /*bump the number of "strange+" numbers*/
$= $ j /*append the number to the $ list. */
end /*j*/
/*stick a fork in it, we're all done. */
say # ' strange plus numbers found between ' LO " and " HI ' (inclusive)'
say
say strip($)
- output when using the default inputs:
65 strange plus numbers found between 101 and 499 (inclusive) 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Ring
load "stdlib.ring"
row = 0
see "Strange plus numbers are:" + nl
for n = 100 to 500
flag = 1
str = string(n)
for m = 1 to len(str)-1
num1 = number(str[m])
num2 = number(str[m+1])
pr = num1+num2
if not isprime(pr)
flag = 0
exit
ok
next
if flag = 1
see str + " "
row = row + 1
if row % 10 = 0
see nl
ok
ok
next
- Output:
Strange plus numbers are: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
RPL
≪ { } 101 499 FOR n n 10 IDIV2 SWAP 10 IDIV2 IF ROT OVER + ISPRIME? UNROT + ISPRIME? AND THEN n + END NEXT ≫ 'TASK' STO
- Output:
1: {111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498}
Ruby
$p = [
false, false, true, true, false,
true, false, true, false, false,
false, true, false, true, false,
false, false, true, false
]
def isStrange(n)
if n < 10 then
return false
end
while n >= 10 do
if not $p[n % 10 + (n / 10).floor % 10] then
return false
end
n = (n / 10).floor
end
return true
end
def test(nMin, nMax)
k = 0
for n in nMin .. nMax
if isStrange(n) then
print n
k = k + 1
if k % 10 != 0 then
print ' '
else
print "\n"
end
end
end
end
test(101, 499)
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Seed7
$ include "seed7_05.s7i";
const func boolean: prime (in integer: number) is
return number in {2, 3, 5, 7, 11, 13, 17};
const func boolean: strange (in var integer: number) is func
result
var boolean: strange is TRUE;
begin
while number > 9 and strange do
if not prime(number rem 10 + number div 10 rem 10) then
strange := FALSE;
end if;
number := number div 10;
end while;
end func;
const proc: main is func
local
var integer: n is 0;
var integer: count is 0;
begin
for n range 101 to 499 do
if strange(n) then
write(n <& " ");
incr(count);
if count rem 13 = 0 then
writeln;
end if;
end if;
end for;
end func;
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
SETL
program strange_plus_numbers;
$ a sum of two digits is never >18
primes := {2, 3, 5, 7, 11, 13, 17};
strangeplus := {
n : n in [101..499]
| lowpairsum(n) in primes and highpairsum(n) in primes
};
loop for n in strangeplus do
putchar(lpad(str n, 5));
if (i +:= 1) mod 10 = 0 then print; end if;
end loop;
print;
proc lowpairsum(n);
return n mod 100 div 10 + n mod 10;
end proc;
proc highpairsum(n);
return lowpairsum(n div 10);
end proc;
end program;
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Sidef
100..500 -> map { .digits }.grep {|d|
is_prime(d[-1]+d[-2]) && is_prime(d[-2]+d[-3])
}.map{ .digits2num }.slices(10).each { .join(' ').say }
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
V (Vlang)
fn is_prime(n int) bool {
return n == 2 || n == 3 || n == 5 || n == 7 || n == 11 || n == 13 || n == 17
}
fn main() {
mut count := 0
mut d := []int{}
println("Strange plus numbers in the open interval (100, 500) are:\n")
for i := 101; i < 500; i++ {
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 plus numbers in all.")
}
- Output:
Strange plus numbers in the open interval (100, 500) are: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 strange plus numbers in all.
VTL-2
10 C=0
20 N=101
30 K=N
40 K=K/10
50 L=%
60 K=K/10
70 L=L+%
80 R=%+K
90 L=(L=2)+(L=3)+(L=5)+(L=7)+(L=11)+(L=13)+(L=17
100 R=(R=2)+(R=3)+(R=5)+(R=7)+(R=11)+(R=13)+(R=17
110 #=L*R*150
120 N=N+1
130 #=N<500*30
140 #=999
150 ?=N
160 $=32
170 C=C+1
180 #=C/10*0+0<%*!
190 ?=""
200 #=!
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
Wren
Simple brute force is adequate for this.
var primes = [2, 3, 5, 7, 11, 13, 17]
var count = 0
var d = []
System.print("Strange plus numbers in the open interval (100, 500) are:\n")
for (i in 101..499) {
d.clear()
var j = i
while (j > 0) {
d.add(j % 10)
j = (j/10).floor
}
if (primes.contains(d[0] + d[1]) && primes.contains(d[1] + d[2])) {
System.write("%(i) ")
count = count + 1
if (count % 10 == 0) System.print()
}
}
if (count % 10 != 0) System.print()
System.print("\n%(count) strange plus numbers in all.")
- Output:
Strange plus numbers in the open interval (100, 500) are: 111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498 65 strange plus numbers in all.
x86 Assembly
A 16-bit solution for NASM under DOS. Assemble with nasm -fbin strange.asm -o strange.com
. The prime sieve up to 18 is hard-coded.
org 100h
mov cx, 10 ; cl is used for division, ch to count numbers printed on a line
mov si, 101 ; loop index from 101 to 499
mov di, p ; pointer to prime sieve
; p+k is a pointer to the reversed string repr. of the current index
L1 mov ax, si
xor bx, bx ; bx counts characters in the string
L2 div cl ; div by 10 to get last digit
add ah, 48 ; convert digit to ascii
mov [bx+di+k], ah ; store char
test bl, bl ; if it's the first, don't check for prime sum
jz L3
add ah, [bx+di+k-1] ; sum of digits
sub ah, 96 ; adjust for ascii
xchg ah, bl
cmp byte [bx+di], 1 ; is it prime?
jne L6 ; otherwise continue with next number
mov bl, ah ; restore bl
L3 xor ah, ah ; must be zero for the next division
inc bx ; one more char
test al, al ; are there more digits?
jnz L2
L4 mov ah, 2 ; the number is strange, print it char by char
mov dl, [bx+di+k-1]
int 21h
dec bx
jnz L4
mov ah, 2 ; print a space or a newline
mov dl, 32
inc ch
cmp ch, 10 ; if less than 10 on the current line it's a space
jne L5
mov dl, 10 ; otherwise it's a newline
xor ch, ch ; reset the counter when we reach 10
L5 int 21h
L6 inc si ; next number
cmp si, 500 ; and loop until we reach 500
jb L1
int 20h ; return to DOS
p db 0, 0, 1, 1, 0, 1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0
k equ $-p
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498
XPL0
func StrangePlus(N);
int N, A, B, S;
[N:= N/10;
A:= rem(0);
loop [N:= N/10;
B:= rem(0);
S:= A+B;
if S#2 & S#3 & S#5 & S#7 & S#11 & S#13 & S#17 then return false;
if N = 0 then return true;
A:= B];
];
int Cnt, N;
[Cnt:= 0;
for N:= 100 to 500-1 do
if StrangePlus(N) then
[Cnt:= Cnt+1;
IntOut(0, N);
if rem(Cnt/20) = 0 then CrLf(0) else ChOut(0, ^ )];
]
- Output:
111 112 114 116 120 121 123 125 129 141 143 147 149 161 165 167 202 203 205 207 211 212 214 216 230 232 234 238 250 252 256 258 292 294 298 302 303 305 307 320 321 323 325 329 341 343 347 349 383 385 389 411 412 414 416 430 432 434 438 470 474 476 492 494 498