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
|
|
@ -0,0 +1,17 @@
|
|||
9000 REM ----- function generate
|
||||
9010 REM in ... i ... number
|
||||
9020 REM out ... pf() ... factors
|
||||
9030 REM mod ... ca ... pf candidate
|
||||
9040 pf(0)=0 : ca=2 : REM special case
|
||||
9050 IF i=1 THEN RETURN
|
||||
9060 IF INT(i/ca)*ca=i THEN GOSUB 9200 : GOTO 9050
|
||||
9070 FOR ca=3 TO INT( SQR(i)) STEP 2
|
||||
9080 IF i=1 THEN RETURN
|
||||
9090 IF INT(i/ca)*ca=i THEN GOSUB 9200 : GOTO 9080
|
||||
9100 NEXT
|
||||
9110 IF i>1 THEN ca=i : GOSUB 9200
|
||||
9120 RETURN
|
||||
9200 pf(0)=pf(0)+1
|
||||
9210 pf(pf(0))=ca
|
||||
9220 i=i/ca
|
||||
9230 RETURN
|
||||
49
Task/Prime-decomposition/ERRE/prime-decomposition.erre
Normal file
49
Task/Prime-decomposition/ERRE/prime-decomposition.erre
Normal file
|
|
@ -0,0 +1,49 @@
|
|||
PROGRAM DECOMPOSE
|
||||
|
||||
|
||||
!
|
||||
! for rosettacode.org
|
||||
!
|
||||
|
||||
!VAR NUM,J
|
||||
|
||||
DIM PF[100]
|
||||
|
||||
PROCEDURE STORE_FACTOR
|
||||
PF[0]=PF[0]+1
|
||||
PF[PF[0]]=CA
|
||||
I=I/CA
|
||||
END PROCEDURE
|
||||
|
||||
PROCEDURE DECOMP(I)
|
||||
PF[0]=0 CA=2 ! special case
|
||||
LOOP
|
||||
IF I=1 THEN EXIT PROCEDURE END IF
|
||||
EXIT IF INT(I/CA)*CA<>I
|
||||
STORE_FACTOR
|
||||
END LOOP
|
||||
FOR CA=3 TO INT(SQR(I)) STEP 2 DO
|
||||
LOOP
|
||||
IF I=1 THEN EXIT PROCEDURE END IF
|
||||
EXIT IF INT(I/CA)*CA<>I
|
||||
STORE_FACTOR
|
||||
END LOOP
|
||||
END FOR
|
||||
IF I>1 THEN CA=I STORE_FACTOR END IF
|
||||
END PROCEDURE
|
||||
|
||||
BEGIN
|
||||
! ----- function generate
|
||||
! in ... I ... number
|
||||
! out ... PF[] ... factors
|
||||
! PF[0] ... # of factors
|
||||
! mod ... CA ... pr.fact. candidate
|
||||
PRINT(CHR$(12);) !CLS
|
||||
INPUT("Numero ",NUM)
|
||||
DECOMP(NUM)
|
||||
PRINT(NUM;"=";)
|
||||
FOR J=1 TO PF[0] DO
|
||||
PRINT(PF[J];)
|
||||
END FOR
|
||||
PRINT
|
||||
END PROGRAM
|
||||
|
|
@ -0,0 +1,10 @@
|
|||
(prime-factors 1024)
|
||||
→ (2 2 2 2 2 2 2 2 2 2)
|
||||
|
||||
(lib 'bigint)
|
||||
;; 2^59 - 1
|
||||
(prime-factors (1- (expt 2 59)))
|
||||
→ (179951 3203431780337)
|
||||
|
||||
(prime-factors 100000000000000000037)
|
||||
→ (31 821 66590107 59004541)
|
||||
143
Task/Prime-decomposition/Ezhil/prime-decomposition.ezhil
Normal file
143
Task/Prime-decomposition/Ezhil/prime-decomposition.ezhil
Normal file
|
|
@ -0,0 +1,143 @@
|
|||
## இந்த நிரல் தரப்பட்ட எண்ணின் பகாஎண் கூறுகளைக் கண்டறியும்
|
||||
|
||||
நிரல்பாகம் பகாஎண்ணா(எண்1)
|
||||
|
||||
## இந்த நிரல்பாகம் தரப்பட்ட எண் பகு எண்ணா அல்லது பகா எண்ணா என்று கண்டறிந்து சொல்லும்
|
||||
## பகுஎண் என்றால் 0 திரும்பத் தரப்படும்
|
||||
## பகாஎண் என்றால் 1 திரும்பத் தரப்படும்
|
||||
|
||||
@(எண்1 < 0) ஆனால்
|
||||
|
||||
## எதிர்மறை எண்களை நேராக்குதல்
|
||||
|
||||
எண்1 = எண்1 * (-1)
|
||||
|
||||
முடி
|
||||
|
||||
@(எண்1 < 2) ஆனால்
|
||||
|
||||
## பூஜ்ஜியம், ஒன்று ஆகியவை பகா எண்கள் அல்ல
|
||||
|
||||
பின்கொடு 0
|
||||
|
||||
முடி
|
||||
|
||||
@(எண்1 == 2) ஆனால்
|
||||
|
||||
## இரண்டு என்ற எண் ஒரு பகா எண்
|
||||
|
||||
பின்கொடு 1
|
||||
|
||||
முடி
|
||||
|
||||
மீதம் = எண்1%2
|
||||
|
||||
@(மீதம் == 0) ஆனால்
|
||||
|
||||
## இரட்டைப்படை எண், ஆகவே, இது பகா எண் அல்ல
|
||||
|
||||
பின்கொடு 0
|
||||
|
||||
முடி
|
||||
|
||||
எண்1வர்க்கமூலம் = எண்1^0.5
|
||||
|
||||
@(எண்2 = 3, எண்2 <= எண்1வர்க்கமூலம், எண்2 = எண்2 + 2) ஆக
|
||||
|
||||
மீதம்1 = எண்1%எண்2
|
||||
|
||||
@(மீதம்1 == 0) ஆனால்
|
||||
|
||||
## ஏதேனும் ஓர் எண்ணால் முழுமையாக வகுபட்டுவிட்டது, ஆகவே அது பகா எண் அல்ல
|
||||
|
||||
பின்கொடு 0
|
||||
|
||||
முடி
|
||||
|
||||
முடி
|
||||
|
||||
பின்கொடு 1
|
||||
|
||||
முடி
|
||||
|
||||
நிரல்பாகம் பகுத்தெடு(எண்1)
|
||||
|
||||
## இந்த எண் தரப்பட்ட எண்ணின் பகா எண் கூறுகளைக் கண்டறிந்து பட்டியல் இடும்
|
||||
|
||||
கூறுகள் = பட்டியல்()
|
||||
|
||||
@(எண்1 < 0) ஆனால்
|
||||
|
||||
## எதிர்மறை எண்களை நேராக்குதல்
|
||||
|
||||
எண்1 = எண்1 * (-1)
|
||||
|
||||
முடி
|
||||
|
||||
@(எண்1 <= 1) ஆனால்
|
||||
|
||||
## ஒன்று அல்லது அதற்குக் குறைவான எண்களுக்குப் பகா எண் விகிதம் கண்டறியமுடியாது
|
||||
|
||||
பின்கொடு கூறுகள்
|
||||
|
||||
முடி
|
||||
|
||||
@(பகாஎண்ணா(எண்1) == 1) ஆனால்
|
||||
|
||||
## தரப்பட்ட எண்ணே பகா எண்ணாக அமைந்துவிட்டால், அதற்கு அதுவே பகாஎண் கூறு ஆகும்
|
||||
|
||||
பின்இணை(கூறுகள், எண்1)
|
||||
பின்கொடு கூறுகள்
|
||||
|
||||
முடி
|
||||
|
||||
தாற்காலிகஎண் = எண்1
|
||||
|
||||
எண்2 = 2
|
||||
|
||||
@(எண்2 <= தாற்காலிகஎண்) வரை
|
||||
|
||||
விடை1 = பகாஎண்ணா(எண்2)
|
||||
மீண்டும்தொடங்கு = 0
|
||||
|
||||
@(விடை1 == 1) ஆனால்
|
||||
|
||||
விடை2 = தாற்காலிகஎண்%எண்2
|
||||
|
||||
@(விடை2 == 0) ஆனால்
|
||||
|
||||
## பகா எண்ணால் முழுமையாக வகுபட்டுள்ளது, அதனைப் பட்டியலில் இணைக்கிறோம்
|
||||
|
||||
பின்இணை(கூறுகள், எண்2)
|
||||
தாற்காலிகஎண் = தாற்காலிகஎண்/எண்2
|
||||
|
||||
## மீண்டும் இரண்டில் தொடங்கி இதே கணக்கிடுதலைத் தொடரவேண்டும்
|
||||
|
||||
எண்2 = 2
|
||||
மீண்டும்தொடங்கு = 1
|
||||
|
||||
முடி
|
||||
|
||||
முடி
|
||||
|
||||
@(மீண்டும்தொடங்கு == 0) ஆனால்
|
||||
|
||||
## அடுத்த எண்ணைத் தேர்ந்தெடுத்துக் கணக்கிடுதலைத் தொடரவேண்டும்
|
||||
|
||||
எண்2 = எண்2 + 1
|
||||
|
||||
முடி
|
||||
|
||||
முடி
|
||||
|
||||
பின்கொடு கூறுகள்
|
||||
|
||||
முடி
|
||||
|
||||
அ = int(உள்ளீடு("உங்களுக்குப் பிடித்த ஓர் எண்ணைத் தாருங்கள்: "))
|
||||
|
||||
பகாஎண்கூறுகள் = பட்டியல்()
|
||||
|
||||
பகாஎண்கூறுகள் = பகுத்தெடு(அ)
|
||||
|
||||
பதிப்பி "நீங்கள் தந்த எண்ணின் பகா எண் கூறுகள் இவை: ", பகாஎண்கூறுகள்
|
||||
|
|
@ -0,0 +1,53 @@
|
|||
' FB 1.05.0 Win64
|
||||
|
||||
Function isPrime(n As Integer) As Boolean
|
||||
If n Mod 2 = 0 Then Return n = 2
|
||||
If n Mod 3 = 0 Then Return n = 3
|
||||
Dim d As Integer = 5
|
||||
While d * d <= n
|
||||
If n Mod d = 0 Then Return False
|
||||
d += 2
|
||||
If n Mod d = 0 Then Return False
|
||||
d += 4
|
||||
Wend
|
||||
Return True
|
||||
End Function
|
||||
|
||||
Sub getPrimeFactors(factors() As UInteger, n As UInteger)
|
||||
If n < 2 Then Return
|
||||
If isPrime(n) Then
|
||||
Redim factors(0 To 0)
|
||||
factors(0) = n
|
||||
Return
|
||||
End If
|
||||
Dim factor As UInteger = 2
|
||||
Do
|
||||
If n Mod factor = 0 Then
|
||||
Redim Preserve factors(0 To UBound(factors) + 1)
|
||||
factors(UBound(factors)) = factor
|
||||
n \= factor
|
||||
If n = 1 Then Return
|
||||
If isPrime(n) Then factor = n
|
||||
Else
|
||||
factor += 1
|
||||
End If
|
||||
Loop
|
||||
End Sub
|
||||
|
||||
Dim factors() As UInteger
|
||||
Dim primes(1 To 17) As UInteger = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59}
|
||||
Dim n As UInteger
|
||||
For i As UInteger = 1 To 17
|
||||
Erase factors
|
||||
n = 1 Shl primes(i) - 1
|
||||
getPrimeFactors factors(), n
|
||||
Print "2^";Str(primes(i)); Tab(5); " - 1 = "; Str(n); Tab(30);" => ";
|
||||
For j As UInteger = LBound(factors) To UBound(factors)
|
||||
Print factors(j);
|
||||
If j < UBound(factors) Then Print " x ";
|
||||
Next j
|
||||
Print
|
||||
Next i
|
||||
Print
|
||||
Print "Press any key to quit"
|
||||
Sleep
|
||||
10
Task/Prime-decomposition/LFE/prime-decomposition.lfe
Normal file
10
Task/Prime-decomposition/LFE/prime-decomposition.lfe
Normal file
|
|
@ -0,0 +1,10 @@
|
|||
(defun factors (n)
|
||||
(factors n 2 '()))
|
||||
|
||||
(defun factors
|
||||
((1 _ acc)
|
||||
acc)
|
||||
((n k acc) (when (== 0 (rem n k)))
|
||||
(factors (div n k) k (cons k acc)))
|
||||
((n k acc)
|
||||
(factors n (+ k 1) acc)))
|
||||
23
Task/Prime-decomposition/Lingo/prime-decomposition-1.lingo
Normal file
23
Task/Prime-decomposition/Lingo/prime-decomposition-1.lingo
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
-- Returns list of prime factors for given number.
|
||||
-- To overcome the limits of integers (signed 32-bit in Lingo),
|
||||
-- the number can be specified as float (which works up to 2^53).
|
||||
-- For the same reason, values in returned list are floats, not integers.
|
||||
on getPrimeFactors (n)
|
||||
f = []
|
||||
f.sort()
|
||||
c = sqrt(n)
|
||||
i = 1.0
|
||||
repeat while TRUE
|
||||
i=i+1
|
||||
if i>c then exit repeat
|
||||
check = n/i
|
||||
if bitOr(check,0)=check then
|
||||
f.add(i)
|
||||
n = check
|
||||
c = sqrt(n)
|
||||
i = 1.0
|
||||
end if
|
||||
end repeat
|
||||
f.add(n)
|
||||
return f
|
||||
end
|
||||
11
Task/Prime-decomposition/Lingo/prime-decomposition-2.lingo
Normal file
11
Task/Prime-decomposition/Lingo/prime-decomposition-2.lingo
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
put getPrimeFactors(12)
|
||||
-- [2.0000, 2.0000, 3.0000]
|
||||
|
||||
-- print floats without fractional digits
|
||||
the floatPrecision=0
|
||||
|
||||
put getPrimeFactors(12)
|
||||
-- [2, 2, 3]
|
||||
|
||||
put getPrimeFactors(1125899906842623.0)
|
||||
-- [3, 251, 601, 4051, 614141]
|
||||
47
Task/Prime-decomposition/Nim/prime-decomposition.nim
Normal file
47
Task/Prime-decomposition/Nim/prime-decomposition.nim
Normal file
|
|
@ -0,0 +1,47 @@
|
|||
import strutils, math, sequtils, times
|
||||
|
||||
proc getStep(n: int64) : int64 {.inline.} =
|
||||
result = 1 + n*4 - int64(n /% 2)*2
|
||||
|
||||
proc primeFac(n: int64): seq[int64] =
|
||||
var res: seq[int64] = @[]
|
||||
var maxq = int64(floor(sqrt(float(n))))
|
||||
var d = 1
|
||||
var q: int64 = (n %% 2) and 2 or 3 # either 2 or 3, alternating
|
||||
while (q <= maxq) and ((n %% q) != 0):
|
||||
q = getStep(d)
|
||||
d += 1
|
||||
if q <= maxq:
|
||||
var q1: seq[int64] = primeFac(n /% q)
|
||||
var q2: seq[int64] = primeFac(q)
|
||||
res = concat(q2, q1, res)
|
||||
else:
|
||||
res.add(n)
|
||||
result = res
|
||||
|
||||
var is_prime: seq[Bool] = @[]
|
||||
is_prime.add(False)
|
||||
is_prime.add(False)
|
||||
|
||||
iterator primes(limit: int): int =
|
||||
for n in high(is_prime) .. limit+2: is_prime.add(True)
|
||||
for n in 2 .. limit + 1:
|
||||
if is_prime[n]:
|
||||
yield n
|
||||
for i in countup((n *% n), limit+1, n): # start at ``n`` squared
|
||||
try:
|
||||
is_prime[i] = False
|
||||
except EInvalidIndex: break
|
||||
|
||||
# Example: calculate factors of Mersenne numbers to M59 #
|
||||
|
||||
for m in primes(59):
|
||||
var p = int64(pow(2.0,float(m)) - 1)
|
||||
write(stdout,"2**$1-1 = $2, with factors: " % [$m, $p] )
|
||||
var start = cpuTime()
|
||||
var f = primeFac(p)
|
||||
for factor in f:
|
||||
write(stdout, factor)
|
||||
write(stdout, ", ")
|
||||
FlushFile(stdout)
|
||||
writeln(stdout, "=> $#ms" % $int(1000*(cpuTime()-start)) )
|
||||
14
Task/Prime-decomposition/Oforth/prime-decomposition.oforth
Normal file
14
Task/Prime-decomposition/Oforth/prime-decomposition.oforth
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
: factors(n) // ( aInteger -- aList )
|
||||
| k p |
|
||||
ListBuffer new
|
||||
2 ->k
|
||||
n nsqrt ->p
|
||||
while( k p <= ) [
|
||||
n k /mod swap ifZero: [
|
||||
dup ->n nsqrt ->p
|
||||
k over add continue
|
||||
]
|
||||
drop k 1+ ->k
|
||||
]
|
||||
n 1 > ifTrue: [ n over add ]
|
||||
dup freeze ;
|
||||
20
Task/Prime-decomposition/Ring/prime-decomposition.ring
Normal file
20
Task/Prime-decomposition/Ring/prime-decomposition.ring
Normal file
|
|
@ -0,0 +1,20 @@
|
|||
prime = 18705
|
||||
decomp(prime)
|
||||
|
||||
func decomp nr
|
||||
x = ""
|
||||
for i = 1 to nr
|
||||
if isPrime(i) and nr % i = 0
|
||||
x = x + string(i) + " * " ok
|
||||
if i = nr
|
||||
x2 = substr(x,1,(len(x)-2))
|
||||
see string(nr) + " = " + x2 + nl ok
|
||||
next
|
||||
|
||||
func isPrime num
|
||||
if (num <= 1) return 0 ok
|
||||
if (num % 2 = 0) return 0 ok
|
||||
for i = 3 to floor(num / 2) -1 step 2
|
||||
if (num % i = 0) return 0 ok
|
||||
next
|
||||
return 1
|
||||
9
Task/Prime-decomposition/SPAD/prime-decomposition.spad
Normal file
9
Task/Prime-decomposition/SPAD/prime-decomposition.spad
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
(1) -> factor 102400
|
||||
|
||||
12 2
|
||||
(1) 2 5
|
||||
Type: Factored(Integer)
|
||||
(2) -> factor 23193931893819371
|
||||
|
||||
(2) 83 3469 71341 1129153
|
||||
Type: Factored(Integer)
|
||||
|
|
@ -0,0 +1,11 @@
|
|||
isPrime(n) := n = 2 or (n > 1 and none(n mod ([2]++((1...floor(sqrt(n)/2))*2+1)) = 0));
|
||||
|
||||
primeFactorization(num) := primeFactorizationHelp(num, []);
|
||||
|
||||
primeFactorizationHelp(num, current(1)) :=
|
||||
let
|
||||
primeFactors[i] := i when num mod i = 0 and isPrime(i) foreach i within 2 ... num;
|
||||
in
|
||||
current when size(primeFactors) = 0
|
||||
else
|
||||
primeFactorizationHelp(num / product(primeFactors), current ++ primeFactors);
|
||||
|
|
@ -0,0 +1,8 @@
|
|||
primeFactorization(num) := primeFactorizationHelp(num, 2, []);
|
||||
|
||||
primeFactorizationHelp(num, divisor, factors(1)) :=
|
||||
factors when num <= 1
|
||||
else
|
||||
primeFactorizationHelp(num, divisor + 1, factors) when num mod divisor /= 0
|
||||
else
|
||||
primeFactorizationHelp(num / divisor, divisor, factors ++ [divisor]);
|
||||
|
|
@ -0,0 +1,4 @@
|
|||
require('ntheory')
|
||||
func prime_factors(n) {
|
||||
[%S<ntheory>.factor(n.to_s)]
|
||||
}
|
||||
18
Task/Prime-decomposition/Sidef/prime-decomposition-2.sidef
Normal file
18
Task/Prime-decomposition/Sidef/prime-decomposition-2.sidef
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
func prime_factors(n) {
|
||||
var p = 3;
|
||||
var out = [];
|
||||
return out if (n < 1);
|
||||
while (!(n & 1)) {
|
||||
n >>= 1;
|
||||
out << 2;
|
||||
}
|
||||
while ((n > 1) && (p*p <= n)) {
|
||||
while (n %% p) {
|
||||
n /= p;
|
||||
out << p;
|
||||
}
|
||||
p += 2;
|
||||
}
|
||||
out << n if (n > 1);
|
||||
return out;
|
||||
}
|
||||
|
|
@ -0,0 +1 @@
|
|||
say prime_factors(536870911);
|
||||
14
Task/Prime-decomposition/jq/prime-decomposition-1.jq
Normal file
14
Task/Prime-decomposition/jq/prime-decomposition-1.jq
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
def factors:
|
||||
. as $in
|
||||
| [2, $in, false]
|
||||
| recurse( .[0] as $p |.[1] as $q | .[2] as $valid | .[3] as $s
|
||||
| if $q == 1 then empty
|
||||
elif $q % $p == 0 then [$p, $q/$p, true]
|
||||
elif $p == 2 then [3, $q, false, $s]
|
||||
else
|
||||
($s // ($q | sqrt)) as $s
|
||||
| if $p + 2 <= $s then [$p + 2, $q, false, $s]
|
||||
else [$q, 1, true]
|
||||
end
|
||||
end )
|
||||
| if .[2] then .[0] else empty end ;
|
||||
7
Task/Prime-decomposition/jq/prime-decomposition-2.jq
Normal file
7
Task/Prime-decomposition/jq/prime-decomposition-2.jq
Normal file
|
|
@ -0,0 +1,7 @@
|
|||
[9007199254740992 | factors] | length
|
||||
#=> 53
|
||||
|
||||
# 2**29-1 = 536870911
|
||||
[ 536870911 | factors ]
|
||||
|
||||
#=> [233,1103,2089]
|
||||
Loading…
Add table
Add a link
Reference in a new issue