Add tasks for all the new languages
This commit is contained in:
parent
9dc3c2bb62
commit
bba7bfd280
13208 changed files with 134745 additions and 0 deletions
10
Task/Pernicious-numbers/EchoLisp/pernicious-numbers.echolisp
Normal file
10
Task/Pernicious-numbers/EchoLisp/pernicious-numbers.echolisp
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(lib 'sequences)
|
||||
|
||||
(define (pernicious? n) (prime? (bit-count n)))
|
||||
|
||||
(define pernicious (filter pernicious? [1 .. ]))
|
||||
(take pernicious 25)
|
||||
→ (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)
|
||||
|
||||
(take (filter pernicious? [888888877 .. 888888889]) #:all)
|
||||
→ (888888877 888888878 888888880 888888883 888888885 888888886)
|
||||
|
|
@ -0,0 +1,57 @@
|
|||
' FreeBASIC v1.05.0 win64
|
||||
|
||||
Function SumBinaryDigits(number As Integer) As Integer
|
||||
If number < 0 Then number = -number ' convert negative numbers to positive
|
||||
Var sum = 0
|
||||
While number > 0
|
||||
sum += number Mod 2
|
||||
number \= 2
|
||||
Wend
|
||||
Return sum
|
||||
End Function
|
||||
|
||||
Function IsPrime(number As Integer) As Boolean
|
||||
If number <= 1 Then
|
||||
Return false
|
||||
ElseIf number <= 3 Then
|
||||
Return true
|
||||
ElseIf number Mod 2 = 0 OrElse number Mod 3 = 0 Then
|
||||
Return false
|
||||
End If
|
||||
Var i = 5
|
||||
While i * i <= number
|
||||
If number Mod i = 0 OrElse number Mod (i + 2) = 0 Then
|
||||
Return false
|
||||
End If
|
||||
i += 6
|
||||
Wend
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Function IsPernicious(number As Integer) As Boolean
|
||||
Dim popCount As Integer = SumBinaryDigits(number)
|
||||
Return IsPrime(popCount)
|
||||
End Function
|
||||
|
||||
Dim As Integer n = 1, count = 0
|
||||
Print "The following are the first 25 pernicious numbers :"
|
||||
Print
|
||||
|
||||
Do
|
||||
If IsPernicious(n) Then
|
||||
Print Using "###"; n;
|
||||
count += 1
|
||||
End If
|
||||
n += 1
|
||||
Loop Until count = 25
|
||||
|
||||
Print : Print
|
||||
Print "The pernicious numbers between 888,888,877 and 888,888,888 inclusive are :"
|
||||
Print
|
||||
For n = 888888877 To 888888888
|
||||
If IsPernicious(n) Then Print Using "##########"; n;
|
||||
Next
|
||||
Print : Print
|
||||
Print "Press any key to exit the program"
|
||||
Sleep
|
||||
End
|
||||
30
Task/Pernicious-numbers/Nim/pernicious-numbers.nim
Normal file
30
Task/Pernicious-numbers/Nim/pernicious-numbers.nim
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
import strutils
|
||||
|
||||
proc count(s: string, sub: char): int =
|
||||
var i = 0
|
||||
while true:
|
||||
i = s.find(sub, i)
|
||||
if i < 0:
|
||||
break
|
||||
inc i
|
||||
inc result
|
||||
|
||||
proc popcount(n): int = n.toBin(64).count('1')
|
||||
|
||||
const primes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61}
|
||||
|
||||
var p = newSeq[int]()
|
||||
var i = 0
|
||||
while p.len < 25:
|
||||
if popcount(i) in primes: p.add i
|
||||
inc i
|
||||
|
||||
echo p
|
||||
|
||||
p = @[]
|
||||
i = 888_888_877
|
||||
while i <= 888_888_888:
|
||||
if popcount(i) in primes: p.add i
|
||||
inc i
|
||||
|
||||
echo p
|
||||
7
Task/Pernicious-numbers/Panda/pernicious-numbers.panda
Normal file
7
Task/Pernicious-numbers/Panda/pernicious-numbers.panda
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
fun prime(a) type integer->integer
|
||||
a where count{{a.factor}}==2
|
||||
fun pernisc(a) type integer->integer
|
||||
a where sum{{a.radix:2 .char.integer}}.integer.prime
|
||||
|
||||
1..36.pernisc
|
||||
888888877..888888888.pernisc
|
||||
21
Task/Pernicious-numbers/Sidef/pernicious-numbers.sidef
Normal file
21
Task/Pernicious-numbers/Sidef/pernicious-numbers.sidef
Normal file
|
|
@ -0,0 +1,21 @@
|
|||
func is_pernicious(n) {
|
||||
var c = 2693408940; # primes < 32 as set bits
|
||||
while (n > 0) { c >>= 1; n &= (n - 1) }
|
||||
c & 1;
|
||||
}
|
||||
|
||||
var (i, *p) = 0;
|
||||
while (p.len < 25) {
|
||||
p << i if is_pernicious(i);
|
||||
++i;
|
||||
}
|
||||
|
||||
say p.join(' ');
|
||||
|
||||
var (i, *p) = 888888877;
|
||||
while (i < 888888888) {
|
||||
p << i if is_pernicious(i);
|
||||
++i;
|
||||
}
|
||||
|
||||
say p.join(' ');
|
||||
74
Task/Pernicious-numbers/Symsyn/pernicious-numbers.symsyn
Normal file
74
Task/Pernicious-numbers/Symsyn/pernicious-numbers.symsyn
Normal file
|
|
@ -0,0 +1,74 @@
|
|||
primes : 0b0010100000100000100010100010000010100000100010100010100010101100
|
||||
|
||||
| the first 25 pernicious numbers
|
||||
|
||||
|
||||
$T | clear string
|
||||
num_pn | set to zero
|
||||
2 n | start at 2
|
||||
5 hi_bit
|
||||
if num_pn LT 25
|
||||
call popcount | count ones
|
||||
if primes bit pop_cnt | if pop_cnt bit of bit vector primes is one
|
||||
+ num_pn | inc number of pernicious numbers
|
||||
~ n $S | convert to decimal string
|
||||
+ ' ' $S | pad a space
|
||||
+ $S $T | add to string $T
|
||||
endif
|
||||
+ pop_cnt | next number (odd) has one more bit than previous (even)
|
||||
+ n | next number
|
||||
if primes bit pop_cnt
|
||||
+ num_pn
|
||||
~ n $S
|
||||
+ ' ' $S
|
||||
+ $S $T
|
||||
endif
|
||||
+ n
|
||||
goif | go back to if
|
||||
endif
|
||||
$T [] | display numbers
|
||||
|
||||
|
||||
|
||||
| pernicious numbers in range 888888877 .. 888888888
|
||||
|
||||
$T | clear string
|
||||
num_pn | set to zero
|
||||
888888876 n | start at 888888876
|
||||
29 hi_bit
|
||||
if n LE 888888888
|
||||
call popcount | count ones
|
||||
if primes bit pop_cnt | if pop_cnt bit of bit vector primes is one
|
||||
+ num_pn | inc number of pernicious numbers
|
||||
~ n $S | convert to decimal string
|
||||
+ ' ' $S | pad a space
|
||||
+ $S $T | add to string $T
|
||||
endif
|
||||
+ pop_cnt | next number (odd) has one more bit than previous (even)
|
||||
+ n | next number
|
||||
if primes bit pop_cnt
|
||||
+ num_pn
|
||||
~ n $S
|
||||
+ ' ' $S
|
||||
+ $S $T
|
||||
endif
|
||||
+ n
|
||||
goif | go back to if
|
||||
endif
|
||||
$T [] | display numbers
|
||||
|
||||
stop
|
||||
|
||||
|
||||
|
||||
popcount | count ones in bit field
|
||||
pop_cnt | pop_cnt to zero
|
||||
1 bit_num | only count even numbers so skip bit 0
|
||||
if bit_num LE hi_bit
|
||||
if n bit bit_num
|
||||
+ pop_cnt
|
||||
endif
|
||||
+ bit_num
|
||||
goif
|
||||
endif
|
||||
return
|
||||
|
|
@ -0,0 +1 @@
|
|||
:ispernum ^(@isPrime \@count \=1 @arr &\`![.toString 2])
|
||||
|
|
@ -0,0 +1,2 @@
|
|||
!-ispernum 1..36 ; returns [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]
|
||||
!-ispernum 888888877..888888888 ; returns [888888877 888888878 888888880 888888883 888888885 888888886]
|
||||
37
Task/Pernicious-numbers/jq/pernicious-numbers.jq
Normal file
37
Task/Pernicious-numbers/jq/pernicious-numbers.jq
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
# is_prime is designed to work with jq 1.4
|
||||
def is_prime:
|
||||
if . == 2 then true
|
||||
else 2 < . and . % 2 == 1 and
|
||||
. as $in
|
||||
| (($in + 1) | sqrt) as $m
|
||||
| (((($m - 1) / 2) | floor) + 1) as $max
|
||||
| reduce range(1; $max) as $i
|
||||
(true; if . then ($in % ((2 * $i) + 1)) > 0 else false end)
|
||||
end;
|
||||
|
||||
def popcount:
|
||||
def bin: recurse( if . == 0 then empty else ./2 | floor end ) % 2;
|
||||
[bin] | add;
|
||||
|
||||
def is_pernicious: popcount | is_prime;
|
||||
|
||||
# Emit a stream of "count" pernicious numbers greater than
|
||||
# or equal to m:
|
||||
def pernicious(m; count):
|
||||
if count > 0 then
|
||||
if m | is_pernicious then m, pernicious(m+1; count -1)
|
||||
else pernicious(m+1; count)
|
||||
end
|
||||
else empty
|
||||
end;
|
||||
|
||||
def task:
|
||||
# display the first 25 pernicious numbers:
|
||||
[ pernicious(1;25) ],
|
||||
|
||||
# display all pernicious numbers between
|
||||
# 888,888,877 and 888,888,888 (inclusive).
|
||||
[ range(888888877; 888888889) | select( is_pernicious ) ]
|
||||
;
|
||||
|
||||
task
|
||||
Loading…
Add table
Add a link
Reference in a new issue