June 2018 Update
This commit is contained in:
parent
ba8067c3b7
commit
22f33d4004
5278 changed files with 84726 additions and 14379 deletions
|
|
@ -1,4 +1,6 @@
|
|||
A [[wp:Pernicious number|pernicious number]] is a positive integer whose [[population count]] is a prime. The population count is the number of ones in the binary representation of a non-negative integer.
|
||||
A [[wp:Pernicious number|pernicious number]] is a positive integer whose [[population count]] is a prime.
|
||||
|
||||
The population count is the number of ''ones'' in the binary representation of a non-negative integer.
|
||||
|
||||
|
||||
;Example
|
||||
|
|
@ -6,7 +8,7 @@ A [[wp:Pernicious number|pernicious number]] is a positive integer
|
|||
|
||||
|
||||
;Task
|
||||
* display the first '''25''' pernicious numbers.
|
||||
* display the first '''25''' pernicious numbers (in decimal).
|
||||
* display all pernicious numbers between '''888,888,877''' and '''888,888,888''' (inclusive).
|
||||
* display each list of integers on one line (which may or may not include a title).
|
||||
|
||||
|
|
|
|||
54
Task/Pernicious-numbers/AWK/pernicious-numbers.awk
Normal file
54
Task/Pernicious-numbers/AWK/pernicious-numbers.awk
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
# syntax: GAWK -f PERNICIOUS_NUMBERS.AWK
|
||||
BEGIN {
|
||||
pernicious(25)
|
||||
pernicious(888888877,888888888)
|
||||
exit(0)
|
||||
}
|
||||
function pernicious(x,y, count,n) {
|
||||
if (y == "") { # print first X pernicious numbers
|
||||
while (count < x) {
|
||||
if (is_prime(pop_count(++n)) == 1) {
|
||||
printf("%d ",n)
|
||||
count++
|
||||
}
|
||||
}
|
||||
}
|
||||
else { # print pernicious numbers in X-Y range
|
||||
for (n=x; n<=y; n++) {
|
||||
if (is_prime(pop_count(n)) == 1) {
|
||||
printf("%d ",n)
|
||||
}
|
||||
}
|
||||
}
|
||||
print("")
|
||||
}
|
||||
function dec2bin(n, str) {
|
||||
while (n) {
|
||||
if (n%2 == 0) {
|
||||
str = "0" str
|
||||
}
|
||||
else {
|
||||
str = "1" str
|
||||
}
|
||||
n = int(n/2)
|
||||
}
|
||||
if (str == "") {
|
||||
str = "0"
|
||||
}
|
||||
return(str)
|
||||
}
|
||||
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)
|
||||
}
|
||||
function pop_count(n) {
|
||||
n = dec2bin(n)
|
||||
return gsub(/1/,"&",n)
|
||||
}
|
||||
6
Task/Pernicious-numbers/Factor/pernicious-numbers.factor
Normal file
6
Task/Pernicious-numbers/Factor/pernicious-numbers.factor
Normal file
|
|
@ -0,0 +1,6 @@
|
|||
USING: lists lists.lazy math.bits math.primes math.ranges ;
|
||||
|
||||
: pernicious? ( n -- ? ) make-bits [ t = ] count prime? ;
|
||||
|
||||
0 lfrom [ pernicious? ] lfilter 25 swap ltake list>array . ! print first 25 pernicious numbers
|
||||
888,888,877 888,888,888 [a,b] [ pernicious? ] filter . ! print pernicious numbers in range
|
||||
26
Task/Pernicious-numbers/Haskell/pernicious-numbers-2.hs
Normal file
26
Task/Pernicious-numbers/Haskell/pernicious-numbers-2.hs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
import Data.List (unfoldr)
|
||||
import Data.Tuple (swap)
|
||||
|
||||
isPernicious :: Int -> Bool
|
||||
isPernicious = isPrime . popCount
|
||||
|
||||
popCount :: Int -> Int
|
||||
popCount =
|
||||
sum . unfoldr ((flip if_ Nothing . (0 ==)) <*> (Just . swap . flip quotRem 2))
|
||||
|
||||
isPrime :: Int -> Bool
|
||||
isPrime =
|
||||
(==) <$> (\n -> [1 .. n] >>= flip ((if_ . (0 ==) . mod n) <*> return) []) <*>
|
||||
((1 :) . return)
|
||||
|
||||
if_ :: Bool -> a -> a -> a
|
||||
if_ True x _ = x
|
||||
if_ False _ y = y
|
||||
|
||||
main :: IO ()
|
||||
main =
|
||||
mapM_
|
||||
print
|
||||
[ take 25 $ filter isPernicious [1 ..]
|
||||
, filter isPernicious [888888877 .. 888888888]
|
||||
]
|
||||
|
|
@ -1,19 +1,16 @@
|
|||
ispernicious(n::Int) = isprime(count_ones(n))
|
||||
using Primes
|
||||
|
||||
pcnt = 0
|
||||
i = 0
|
||||
print(" ")
|
||||
while pcnt < 25
|
||||
i += 1
|
||||
ispernicious(i) || continue
|
||||
pcnt += 1
|
||||
print(i, " ")
|
||||
ispernicious(n::Integer) = isprime(count_ones(n))
|
||||
nextpernicious(n::Integer) = begin n += 1; while !ispernicious(n) n += 1 end; return n end
|
||||
function perniciouses(n::Int)
|
||||
rst = Vector{Int}(n)
|
||||
rst[1] = 3
|
||||
for i in 2:n
|
||||
rst[i] = nextpernicious(rst[i-1])
|
||||
end
|
||||
return rst
|
||||
end
|
||||
println()
|
||||
perniciouses(a::Integer, b::Integer) = filter(ispernicious, a:b)
|
||||
|
||||
print(" ")
|
||||
for i in 888888877:888888888
|
||||
ispernicious(i) || continue
|
||||
print(i, " ")
|
||||
end
|
||||
println()
|
||||
println("First 25 pernicious numbers: ", join(perniciouses(25), ", "))
|
||||
println("Perniciouses in [888888877, 888888888]: ", join(perniciouses(888888877, 888888888), ", "))
|
||||
|
|
|
|||
50
Task/Pernicious-numbers/Modula-2/pernicious-numbers.mod2
Normal file
50
Task/Pernicious-numbers/Modula-2/pernicious-numbers.mod2
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
MODULE Pernicious;
|
||||
FROM FormatString IMPORT FormatString;
|
||||
FROM Terminal IMPORT WriteString,WriteLn,ReadChar;
|
||||
|
||||
PROCEDURE IsPrime(x : LONGINT) : BOOLEAN;
|
||||
VAR i : LONGINT;
|
||||
BEGIN
|
||||
IF x<2 THEN RETURN FALSE END;
|
||||
FOR i:=2 TO x-1 DO
|
||||
IF x MOD i = 0 THEN RETURN FALSE END
|
||||
END;
|
||||
RETURN TRUE
|
||||
END IsPrime;
|
||||
|
||||
PROCEDURE BitCount(x : LONGINT) : LONGINT;
|
||||
VAR count : LONGINT;
|
||||
BEGIN
|
||||
count := 0;
|
||||
WHILE x>0 DO
|
||||
x := x BAND (x-1);
|
||||
INC(count)
|
||||
END;
|
||||
RETURN count
|
||||
END BitCount;
|
||||
|
||||
VAR
|
||||
buf : ARRAY[0..63] OF CHAR;
|
||||
i,n : LONGINT;
|
||||
BEGIN
|
||||
i := 1;
|
||||
n := 0;
|
||||
WHILE n<25 DO
|
||||
IF IsPrime(BitCount(i)) THEN
|
||||
FormatString("%l ", buf, i);
|
||||
WriteString(buf);
|
||||
INC(n)
|
||||
END;
|
||||
INC(i)
|
||||
END;
|
||||
WriteLn;
|
||||
|
||||
FOR i:=888888877 TO 888888888 DO
|
||||
IF IsPrime(BitCount(i)) THEN
|
||||
FormatString("%l ", buf, i);
|
||||
WriteString(buf)
|
||||
END;
|
||||
END;
|
||||
|
||||
ReadChar
|
||||
END Pernicious.
|
||||
42
Task/Pernicious-numbers/Ring/pernicious-numbers.ring
Normal file
42
Task/Pernicious-numbers/Ring/pernicious-numbers.ring
Normal file
|
|
@ -0,0 +1,42 @@
|
|||
# Project : Pernicious numbers
|
||||
# Date : 2017/10/01
|
||||
# Author : Gal Zsolt (~ CalmoSoft ~)
|
||||
# Email : <calmosoft@gmail.com>
|
||||
|
||||
see "The first 25 pernicious numbers:" + nl
|
||||
nr = 0
|
||||
for n=1 to 50
|
||||
sum = 0
|
||||
str = decimaltobase(n, 2)
|
||||
for m=1 to len(str)
|
||||
if str[m] = "1"
|
||||
sum = sum + 1
|
||||
ok
|
||||
next
|
||||
if isprime(sum)
|
||||
nr = nr + 1
|
||||
see "" + n + " "
|
||||
ok
|
||||
if nr = 25
|
||||
exit
|
||||
ok
|
||||
next
|
||||
|
||||
func decimaltobase(nr, base)
|
||||
binary = 0
|
||||
i = 1
|
||||
while(nr != 0)
|
||||
remainder = nr % base
|
||||
nr = floor(nr/base)
|
||||
binary= binary + (remainder*i)
|
||||
i = i*10
|
||||
end
|
||||
return string(binary)
|
||||
|
||||
func isprime num
|
||||
if (num <= 1) return 0 ok
|
||||
if (num % 2 = 0 and num != 2) return 0 ok
|
||||
for i = 3 to floor(num / 2) -1 step 2
|
||||
if (num % i = 0) return 0 ok
|
||||
next
|
||||
return 1
|
||||
Loading…
Add table
Add a link
Reference in a new issue