Strange numbers: Difference between revisions
m
syntax highlighting fixup automation
(J) |
Thundergnat (talk | contribs) m (syntax highlighting fixup automation) |
||
Line 16:
{{trans|Python}}
<
V xs = String(n).map(c -> Int(c))
R all(zip(xs, xs[1..]).map((a, b) -> abs(a - b) C (2, 3, 5, 7)))
Line 26:
print(el, end' ‘ ’)
I L.index % 10 == 9
print()</
{{out}}
Line 47:
=={{header|Action!}}==
<
BYTE ARRAY primes=[0 0 1 1 0 1 0 1 0 0]
BYTE d,diff,prev
Line 81:
OD
PrintF("%E%EThere are %I strange numbers",count)
RETURN</
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Strange_numbers.png Screenshot from Atari 8-bit computer]
Line 94:
=={{header|Ada}}==
<
procedure Strange_Numbers is
Line 134:
end loop;
Put_Line ("Strange numbers in range 1_000_000_000 .. 1_999_999_999: " & Count'Image);
end Strange_Numbers;</
{{out}}
<pre>
Line 152:
=={{header|ALGOL W}}==
<
% by a prime %
% returns true if n is strange, false otherwise %
Line 190:
end for_n
end
end.</
{{out}}
<pre>
Line 201:
=={{header|APL}}==
<
{{out}}
<pre>111 112 114 116 120 121 123 125 141 143 161 202 203 205 207 211 212 214 216 230 232 234 250 252 302 303 305 307 320 321
Line 207:
=={{header|AppleScript}}==
<
-- isStrange :: Int -> Bool
Line 418:
end tell
end if
end zipWith</
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 437:
=={{header|AWK}}==
<syntaxhighlight lang="awk">
# syntax: GAWK -f STRANGE_NUMBERS.AWK
BEGIN {
Line 469:
}
function abs(x) { if (x >= 0) { return x } else { return -x } }
</syntaxhighlight>
{{out}}
<pre>
Line 485:
=={{header|BASIC}}==
<
20 FOR I=100 TO 500
30 S=STR$(I)
Line 493:
70 NEXT J
80 PRINT I,
90 NEXT I</
{{out}}
<pre> 130 131 135 136 138
Line 514:
496 497</pre>
=={{header|BCPL}}==
<
let dgtprime(x) = x=2 | x=3 | x=5 | x=7
Line 530:
$)
wrch('*N')
$)</
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
Line 543:
=={{header|BQN}}==
<
{{out}}
The unused positions in the table are filled with <code>0</code>s, because that's the behaviour
Line 562:
=={{header|C}}==
{{trans|Go}}
<
#include <stdio.h>
Line 600:
return 0;
}</
{{out}}
<pre>Strange numbers in the open interval (100, 500) are:
Line 616:
===Extended task===
<
const int next_digit[] = {
Line 655:
return 0;
}</
{{out}}
<pre>% ./a.out | fmt
Line 669:
=={{header|C++}}==
{{trans|Java}}
<
#include <iostream>
#include <vector>
Line 720:
return 0;
}</
{{out}}
<pre>Strange numbers in range [100..500]
Line 736:
=={{header|CLU}}==
<
while n>0 do
yield(n // 10)
Line 771:
end
end
end start_up</
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
Line 784:
=={{header|COBOL}}==
<
PROGRAM-ID. STRANGE-NUMBERS.
Line 819:
MOVE NUM TO N-OUT.
DISPLAY N-OUT.
DONE. EXIT.</
{{out}}
<pre style='height:50ex;'>130
Line 910:
=={{header|Cowgol}}==
<
sub abs(n: int8): (r: uint8) is
Line 945:
n := n + 1;
end loop;
print_nl();</
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
Line 958:
=={{header|Draco}}==
<
bool is_strange;
short da, db, diff;
Line 986:
fi
od
corp</
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
Line 999:
=={{header|F_Sharp|F#}}==
<
// Strange numbers. Nigel Galloway: February 25th., 2021
let N=Array.init 10(fun n->[2;3;5;7]|>List.collect(fun g->[n+g;n-g])|>List.filter((<) -1)|>List.filter((>)10))
[1..4]|>List.collect(fun g->N.[g]|>List.map(fun n->g*10+n%10))|>List.collect(fun n->N.[n%10]|>List.map(fun g->n*10+g%10))|>List.iter(printf "%d "); printfn ""
</syntaxhighlight>
{{out}}
<pre>
Line 1,012:
===Identifying and filtering===
{{works with|Factor|0.99 2020-08-14}}
<
math.text.utils math.vectors prettyprint sequences ;
Line 1,022:
100 500 (a,b) [ strange? ] filter dup
10 group [ [ pprint bl ] each nl ] each nl
length pprint " strange numbers found." print</
{{out}}
<pre>
Line 1,043:
Since we know the next possible digits based on the current last digit, we can construct strange numbers with a backtracking algorithm.
{{works with|Factor|0.99 2020-08-14}}
<
formatting io kernel sequences sequences.generalizations
tools.memory.private tools.time ;
Line 1,065:
dup length commas write
" 10-digit strange numbers beginning with 1:" print
[ first2 ] [ last2 ] bi "%u\n%u\n...\n%u\n%u\n" printf</
{{out}}
<pre>
Line 1,079:
=={{header|FreeBASIC}}==
<
dim as uinteger d, ld, a
if n<10 then return true 'arbitrary, but makes the recursion easier
Line 1,098:
if is_strange(n) then c+=1
next n
print using "There are ####### 10-digit strange numbers beginning with 1.";c</
{{out}}<pre>
Strange numbers between 100 and 500
Line 1,106:
=={{header|Forth}}==
<
: prime? ( n -- ? )
1 swap lshift 0xac and 0<> ;
Line 1,136:
main
bye</
{{out}}
Line 1,154:
===Basic task===
{{trans|Wren}}
<
import "fmt"
Line 1,188:
}
fmt.Printf("\n%d strange numbers in all.\n", count)
}</
{{out}}
Line 1,209:
===Stretch goal===
{{trans|Julia}}
<
import "fmt"
Line 1,239:
}
fmt.Println("Found", len(strangeOnes), places, "\b-digit strange numbers beginning with", start)
}</
{{out}}
Line 1,247:
=={{header|Haskell}}==
<
import Data.List.Split (chunksOf)
Line 1,270:
unlines
(unwords <$> chunksOf 10 (show <$> xs))
]</
{{Out}}
<pre>Strange numbers found in range [100..500]
Line 1,290:
=={{header|J}}==
<
strangePair=: (1 p: ::0:"0 i.10),. 0 1}.isStrange (+ 10&*)"0/~ i.10</
To test if a number is strange, we check if the absolute value of the differences of adjacent pairs of digits are 2, 3, 5 or 7.
Line 1,300:
Task examples:
<
130 131 135 136 138 141 142 146 147 149 161 163 164 168 169 181 183 185 186 202 203 205 207 241 242 246 247 249 250 252 253 257 258 270 272 274 275 279 292 294 296 297 302 303 305 307 313 314 316 318 350 352 353 357 358 361 363 364 368 369 381 383 385 386 ...
#{{ ;<@{{x,"1 0 I. ({:x){y }}&strangePair"1 y}}^:9 ,:1
853423</
=={{header|Java}}==
{{trans|Python}}
<
import java.util.List;
import java.util.function.BiPredicate;
Line 1,359:
}
}
}</
{{out}}
<pre>Strange numbers in range [100..500]
Line 1,379:
===Filter method===
<syntaxhighlight lang="jq">
def is_strange:
def digits: tostring | explode | map([.] | implode | tonumber);
Line 1,401:
task(100; 500; is_strange)
</syntaxhighlight>
{{out}}
<pre>
Line 1,420:
===Generator method===
`lpad` and and `nwise` are as above and so their definitions are not repeated.
<syntaxhighlight lang="jq">
# input: width
# $start: maximum first digit allowed e.g. 9 for unrestricted
Line 1,442:
[3 | generate(4) | map(tostring) | join("") | tonumber]
| nwise(10) | map(lpad(3)) | join(" ")
</syntaxhighlight>
{{out}}
As above, except for the header.
===Stretch goal===
Using generate/1 as already defined:
<syntaxhighlight lang="jq">
def count(s): reduce s as $x (null; .+1);
count(10 | generate(1))
</
{{out}}
Line 1,459:
=={{header|Julia}}==
===Filter method===
<
function filter_open_interval(start, stop, f, doprint=true, rowlength=92)
Line 1,475:
filter_open_interval(100, 500, isstrange)
</
<pre>
Finding numbers matching isstrange for open interval (100, 500):
Line 1,488:
===Generator method===
HT: Factor for the table.
<
possibles = [
[2, 3, 5, 7],
Line 1,514:
countstrange(10)
@time countstrange(10)
</
<pre>
Found 853423 10-digit strange numbers with the most significant digit 1.
Line 1,524:
=={{header|Kotlin}}==
{{trans|Java}}
<
fun digits(n: Int): List<Int> {
Line 1,568:
}
println()
}</
{{out}}
<pre>Strange numbers in range [100..500]
Line 1,584:
=={{header|MAD}}==
<
INTERNAL FUNCTION(D)
Line 1,617:
VECTOR VALUES F = $I4*$
END OF PROGRAM </
{{out}}
<pre style='height:50ex;'>STRANGE NUMBERS
Line 1,709:
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<syntaxhighlight lang="mathematica">
Select[Range[101, 499],
AllTrue[Partition[IntegerDigits[#], 2, 1], Differences /* First /* PrimeQ] &
]</
{{out}}
<pre>{130,131,135,136,138,141,142,146,147,149,161,163,164,168,169,181,183,185,186,202,203,205,207,241,242,246,247,249,250,252,253,257,258,270,272,274,275,279,292,294,296,297,302,303,305,307,313,314,316,318,350,352,353,357,358,361,363,364,368,369,381,383,385,386,413,414,416,418,420,424,425,427,429,461,463,464,468,469,470,472,474,475,479,492,494,496,497}</pre>
=={{header|Modula-2}}==
<
FROM InOut IMPORT WriteCard, WriteLn;
Line 1,750:
END;
WriteLn
END StrangeNumbers.</
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
Line 1,765:
Our program finds the strange numbers with a given number of digits. Filtering is done afterwards. It consumes a lot of memory but is able to find the result in less than a second on our small laptop.
<
const PrimeDigits = [2, 3, 5, 7]
Line 1,802:
result = findStrangeNumbers(10).filterIt(it div 1_000_000_000 == 1)
echo "\nFound ", result.len, " strange numbers with 10 digits and starting with 1."</
{{out}}
Line 1,819:
Not dynamically memorizing of all numbers saves much time 200ms -> 4ms.<BR>
Count is about 4.6 to the power of Digitscount -> first digit 1 followed by 9 digit -> 4.6^9 = 922190
<
const
Line 1,905:
Writeln;
Writeln('Count : ',cnt);
end.</
{{out}}
<pre>
Line 1,923:
=={{header|Perl}}==
<
use warnings;
use feature 'say';
Line 1,937:
my $cnt = my @strange = grep { is_strange($_) } $low+1 .. $high-1;
say "Between $low and $high there are $cnt strange numbers:\n" .
(sprintf "@{['%5d' x $cnt]}", @strange[0..$cnt-1]) =~ s/(.{80})/$1\n/gr;</
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
Line 1,948:
=={{header|Phix}}==
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">poss</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">}}),</span>
Line 1,970:
<span style="color: #004080;">sequence</span> <span style="color: #000000;">res</span> <span style="color: #0000FF;">=</span> <span style="color: #000000;">strange</span><span style="color: #0000FF;">(</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">4</span><span style="color: #0000FF;">))</span> <span style="color: #000080;font-style:italic;">-- (3 digit numbers beginning 1..4)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"%d strange numbers found: %s\n"</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">length</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">),</span><span style="color: #7060A8;">join</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">shorten</span><span style="color: #0000FF;">(</span><span style="color: #000000;">res</span><span style="color: #0000FF;">,</span><span style="color: #008000;">""</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">),</span><span style="color: #008000;">","</span><span style="color: #0000FF;">)})</span>
<!--</
{{out}}
<pre>
Line 1,977:
=== stretch goal ===
Dunno when to quit, me. As there does not appear to be much interest in which 1-digits are strange, I've shown three ways to set ones (output of 0, 10, or 4).
<!--<
<span style="color: #008080;">constant</span> <span style="color: #000000;">diff</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{-</span><span style="color: #000000;">7</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,-</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">2</span><span style="color: #0000FF;">,</span><span style="color: #000000;">3</span><span style="color: #0000FF;">,</span><span style="color: #000000;">5</span><span style="color: #0000FF;">,</span><span style="color: #000000;">7</span><span style="color: #0000FF;">},</span>
<span style="color: #000000;">poss</span> <span style="color: #0000FF;">=</span> <span style="color: #7060A8;">apply</span><span style="color: #0000FF;">(</span><span style="color: #004600;">true</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">sq_add</span><span style="color: #0000FF;">,{</span><span style="color: #7060A8;">tagset</span><span style="color: #0000FF;">(</span><span style="color: #000000;">9</span><span style="color: #0000FF;">,</span><span style="color: #000000;">0</span><span style="color: #0000FF;">),{</span><span style="color: #000000;">diff</span><span style="color: #0000FF;">}}),</span>
Line 2,011:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"(%s)\n"</span><span style="color: #0000FF;">,</span><span style="color: #7060A8;">elapsed</span><span style="color: #0000FF;">(</span><span style="color: #7060A8;">time</span><span style="color: #0000FF;">()-</span><span style="color: #000000;">t0</span><span style="color: #0000FF;">))</span>
<!--</
{{out}}
<pre>
Line 2,048:
=={{header|PL/I}}==
<
strange: procedure(nn) returns(bit);
declare (n, nn) fixed;
Line 2,076:
end;
end;
end strangeNumbers;</
{{out}}
<pre> 130 131 135 136 138 141 142 146 147 149
Line 2,089:
=={{header|PL/M}}==
<
BDOS: PROCEDURE (F,A); DECLARE F BYTE, A ADDRESS; GO TO 5; END BDOS;
EXIT: PROCEDURE; GO TO 0; END EXIT;
Line 2,134:
END;
CALL EXIT;
EOF</
{{out}}
<pre>130 131 135 136 138 141 142 146 147 149
Line 2,147:
=={{header|Python}}==
<
Line 2,205:
# MAIN ---
if __name__ == '__main__':
main()</
{{Out}}
<pre>Strange numbers in range [100..500]
Line 2,222:
=={{header|Raku}}==
<syntaxhighlight lang="raku"
my ($low,$high) = 100, 500;
Line 2,228:
say "Between $low and $high there are {+@strange} strange numbers:\n";
print @strange.batch(20)».fmt("%4d").join: "\n";</
{{out}}
<pre>Between 100 and 500 there are 87 strange numbers:
Line 2,241:
Some optimization was added to bypass calculating the absolute value of adjacent decimal digit differences,
<br>as well as parsing of a number to determine the differences.
<
numeric digits 20 /*allow processing of larger numbers. */
parse arg LO HI bd . /*obtain optional arguments from the CL*/
Line 2,274:
exit 0
/*──────────────────────────────────────────────────────────────────────────────────────*/
commas: parse arg _; do jc=length(_)-3 to 1 by -3; _=insert(',', _, jc); end; return _</
{{out|output|text= when using the default inputs:}}
<pre>
Line 2,290:
=={{header|Ring}}==
<
load "stdlib.ring"
Line 2,316:
ok
next
</syntaxhighlight>
{{out}}
<pre>
Line 2,333:
=={{header|Ruby}}==
{{trans|Java}}
<
result = []
while n > 0
Line 2,370:
xs.each_slice(10) do |s|
print s, "\n"
end</
{{out}}
<pre>Strange numbers in range [100 .. 500]
Line 2,386:
=={{header|Sidef}}==
<
var seq = [p]
Line 2,413:
strange_numbers(500).grep { _ > 100 }.slices(10).each{
.join(' ').say
}</
{{out}}
<pre>
Line 2,430:
===Basic task===
{{trans|Go}}
<
mut n := nn
if n < 0 {
Line 2,461:
}
println("\n$count strange numbers in all.")
}</
{{out}}
Line 2,482:
=={{header|Wren}}==
===Basic task===
<
var count = 0
var d = []
Line 2,500:
}
if (count % 10 != 0) System.print()
System.print("\n%(count) strange numbers in all.")</
{{out}}
Line 2,523:
... though I've generated the 'possibles' table rather than hard-code it.
Runtime about 0.085 seconds which is quick for Wren.
<
var possibles = List.filled(10, null)
for (i in 0..9) {
Line 2,544:
}
System.print("Found %(strangeOnes.count) %(places)-digit strange numbers beginning with %(start).")</
{{out}}
Line 2,553:
=={{header|XPL0}}==
Takes 49 seconds on Raspberry Pi 4.
<
int N, A, B, D;
[N:= N/10;
Line 2,580:
Cnt:= Cnt+1;
IntOut(0, Cnt);
]</
{{out}}
|