Pernicious numbers: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 309: | Line 309: | ||
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
||
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
<lang AWK> |
<lang AWK> |
||
Line 413: | Line 414: | ||
return 0; |
return 0; |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre> |
|||
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
|||
888888877 888888878 888888880 888888883 888888885 888888886 |
|||
</pre> |
|||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include <iostream> |
|||
using namespace std; |
|||
int main() { |
|||
int cnt = 0, cnt2, cnt3, tmp, binary[8]; |
|||
for (int i = 3; cnt < 25; i++) { |
|||
tmp = i; |
|||
cnt2 = 0; |
|||
cnt3 = 0; |
|||
for (int j = 7; j > 0; j--) { |
|||
binary[j] = tmp % 2; |
|||
tmp /= 2; |
|||
} |
|||
binary[0] = tmp; |
|||
for (int j = 0; j < 8; j++) { |
|||
if (binary[j] == 1) { |
|||
cnt2++; |
|||
} |
|||
} |
|||
for (int j = 2; j <= (cnt2 / 2); j++) { |
|||
if (cnt2 % j == 0) { |
|||
cnt3++; |
|||
break; |
|||
} |
|||
} |
|||
if (cnt3 == 0 && cnt2 != 1) { |
|||
cout << i << endl; |
|||
cnt++; |
|||
} |
|||
} |
|||
cout << endl; |
|||
int binary2[31]; |
|||
for (int i = 888888877; i <= 888888888; i++) { |
|||
tmp = i; |
|||
cnt2 = 0; |
|||
cnt3 = 0; |
|||
for (int j = 30; j > 0; j--) { |
|||
binary2[j] = tmp % 2; |
|||
tmp /= 2; |
|||
} |
|||
binary2[0] = tmp; |
|||
for (int j = 0; j < 31; j++) { |
|||
if (binary2[j] == 1) { |
|||
cnt2++; |
|||
} |
|||
} |
|||
for (int j = 2; j <= (cnt2 / 2); j++) { |
|||
if (cnt2 % j == 0) { |
|||
cnt3++; |
|||
break; |
|||
} |
|||
} |
|||
if (cnt3 == 0 && cnt2 != 1) { |
|||
cout << i << endl; |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 551: | Line 484: | ||
} |
} |
||
}</lang> |
}</lang> |
||
{{out}} |
|||
<pre> |
|||
3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
|||
888888877 888888878 888888880 888888883 888888885 888888886 |
|||
</pre> |
|||
=={{header|C++}}== |
|||
<lang cpp> |
|||
#include <iostream> |
|||
using namespace std; |
|||
int main() { |
|||
int cnt = 0, cnt2, cnt3, tmp, binary[8]; |
|||
for (int i = 3; cnt < 25; i++) { |
|||
tmp = i; |
|||
cnt2 = 0; |
|||
cnt3 = 0; |
|||
for (int j = 7; j > 0; j--) { |
|||
binary[j] = tmp % 2; |
|||
tmp /= 2; |
|||
} |
|||
binary[0] = tmp; |
|||
for (int j = 0; j < 8; j++) { |
|||
if (binary[j] == 1) { |
|||
cnt2++; |
|||
} |
|||
} |
|||
for (int j = 2; j <= (cnt2 / 2); j++) { |
|||
if (cnt2 % j == 0) { |
|||
cnt3++; |
|||
break; |
|||
} |
|||
} |
|||
if (cnt3 == 0 && cnt2 != 1) { |
|||
cout << i << endl; |
|||
cnt++; |
|||
} |
|||
} |
|||
cout << endl; |
|||
int binary2[31]; |
|||
for (int i = 888888877; i <= 888888888; i++) { |
|||
tmp = i; |
|||
cnt2 = 0; |
|||
cnt3 = 0; |
|||
for (int j = 30; j > 0; j--) { |
|||
binary2[j] = tmp % 2; |
|||
tmp /= 2; |
|||
} |
|||
binary2[0] = tmp; |
|||
for (int j = 0; j < 31; j++) { |
|||
if (binary2[j] == 1) { |
|||
cnt2++; |
|||
} |
|||
} |
|||
for (int j = 2; j <= (cnt2 / 2); j++) { |
|||
if (cnt2 % j == 0) { |
|||
cnt3++; |
|||
break; |
|||
} |
|||
} |
|||
if (cnt3 == 0 && cnt2 != 1) { |
|||
cout << i << endl; |
|||
} |
|||
} |
|||
} |
|||
</lang> |
|||
{{out}} |
{{out}} |
||
<pre> |
<pre> |
||
Line 1,155: | Line 1,156: | ||
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
||
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
||
=={{header|jq}}== |
=={{header|jq}}== |
||
{{works with|jq|1.4}} |
{{works with|jq|1.4}} |
||
Line 1,493: | Line 1,495: | ||
<pre>@[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36] |
<pre>@[3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36] |
||
@[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre> |
@[888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre> |
||
=={{header|PARI/GP}}== |
|||
<lang parigp>pern(n)=isprime(hammingweight(n)) |
|||
select(pern, [1..36]) |
|||
select(pern,[888888877..888888888])</lang> |
|||
{{out}} |
|||
<pre>%1 = [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36] |
|||
%2 = [888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre> |
|||
=={{header|Panda}}== |
=={{header|Panda}}== |
||
Line 1,514: | Line 1,508: | ||
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
||
888888877 888888878 888888880 888888883 888888885 888888886</pre> |
888888877 888888878 888888880 888888883 888888885 888888886</pre> |
||
=={{header|PARI/GP}}== |
|||
<lang parigp>pern(n)=isprime(hammingweight(n)) |
|||
select(pern, [1..36]) |
|||
select(pern,[888888877..888888888])</lang> |
|||
{{out}} |
|||
<pre>%1 = [3, 5, 6, 7, 9, 10, 11, 12, 13, 14, 17, 18, 19, 20, 21, 22, 24, 25, 26, 28, 31, 33, 34, 35, 36] |
|||
%2 = [888888877, 888888878, 888888880, 888888883, 888888885, 888888886]</pre> |
|||
=={{header|Pascal}}== |
=={{header|Pascal}}== |
||
Line 1,651: | Line 1,653: | ||
print "@pern\n"; |
print "@pern\n"; |
||
print join(" ", grep { is_prime(hammingweight($_)) } 888888877 .. 888888888), "\n";</lang> |
print join(" ", grep { is_prime(hammingweight($_)) } 888888877 .. 888888888), "\n";</lang> |
||
=={{header|Perl 6}}== |
|||
Straightforward implementation using Perl 6's ''is-prime'' built-in subroutine. |
|||
<lang perl6>sub is-pernicious(Int $n --> Bool) { |
|||
is-prime [+] $n.base(2).comb; |
|||
} |
|||
say (grep &is-pernicious, 0 .. *)[^25]; |
|||
say grep &is-pernicious, 888_888_877 .. 888_888_888;</lang> |
|||
{{out}} |
|||
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
|||
888888877 888888878 888888880 888888883 888888885 888888886</pre> |
|||
=={{header|Phix}}== |
=={{header|Phix}}== |
||
Line 2,127: | Line 2,117: | ||
display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive). |
display all pernicious numbers between 888,888,877 and 888,888,888 (inclusive). |
||
888888877, 888888878, 888888880, 888888883, 888888885, 888888886</pre> |
888888877, 888888878, 888888880, 888888883, 888888885, 888888886</pre> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
Straightforward implementation using Perl 6's ''is-prime'' built-in subroutine. |
|||
<lang perl6>sub is-pernicious(Int $n --> Bool) { |
|||
is-prime [+] $n.base(2).comb; |
|||
} |
|||
say (grep &is-pernicious, 0 .. *)[^25]; |
|||
say grep &is-pernicious, 888_888_877 .. 888_888_888;</lang> |
|||
{{out}} |
|||
<pre>3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
|||
888888877 888888878 888888880 888888883 888888885 888888886</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |
||
Line 2,510: | Line 2,513: | ||
888888877 888888878 888888880 888888883 888888885 888888886 888888889 |
888888877 888888878 888888880 888888883 888888885 888888886 888888889 |
||
</pre> |
</pre> |
||
=={{header|Tcl}}== |
=={{header|Tcl}}== |
||
Line 2,586: | Line 2,588: | ||
<pre> 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
<pre> 3 5 6 7 9 10 11 12 13 14 17 18 19 20 21 22 24 25 26 28 31 33 34 35 36 |
||
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
888888877 888888878 888888880 888888883 888888885 888888886 </pre> |
||
=={{header|VBScript}}== |
=={{header|VBScript}}== |
||
<lang vb>'check if the number is pernicious |
<lang vb>'check if the number is pernicious |