Add tasks for all the new languages

This commit is contained in:
Tina Müller 2016-12-05 23:44:36 +01:00
parent 9dc3c2bb62
commit bba7bfd280
13208 changed files with 134745 additions and 0 deletions

View file

@ -0,0 +1,8 @@
shared void run() {
{Integer*} getFactors(Integer n) =>
(1..n).filter((Integer element) => element.divides(n));
for(Integer i in 1..100) {
print("the factors of ``i`` are ``getFactors(i)``");
}
}

View file

@ -0,0 +1,43 @@
PROGRAM FACTORS
!$DOUBLE
PROCEDURE FACTORLIST(N->L$)
LOCAL C%,I,FLIPS%,I%
LOCAL DIM L[32]
FOR I=1 TO SQR(N) DO
IF N=I*INT(N/I) THEN
L[C%]=I
C%=C%+1
IF N<>I*I THEN
L[C%]=INT(N/I)
C%=C%+1
END IF
END IF
END FOR
! BUBBLE SORT ARRAY L[]
FLIPS%=1
WHILE FLIPS%>0 DO
FLIPS%=0
FOR I%=0 TO C%-2 DO
IF L[I%]>L[I%+1] THEN SWAP(L[I%],L[I%+1]) FLIPS%=1
END FOR
END WHILE
L$=""
FOR I%=0 TO C%-1 DO
L$=L$+STR$(L[I%])+","
END FOR
L$=LEFT$(L$,LEN(L$)-1)
END PROCEDURE
BEGIN
PRINT(CHR$(12);) ! CLS
FACTORLIST(45->L$)
PRINT("The factors of 45 are ";L$)
FACTORLIST(12345->L$)
PRINT("The factors of 12345 are ";L$)
END PROGRAM

View file

@ -0,0 +1,18 @@
;; ppows
;; input : a list g of grouped prime factors ( 3 3 3 ..)
;; returns (1 3 9 27 ...)
(define (ppows g (mult 1))
(for/fold (ppows '(1)) ((a g))
(set! mult (* mult a))
(cons mult ppows)))
;; factors
;; decomp n into ((2 2 ..) ( 3 3 ..) ) prime factors groups
;; combines (1 2 4 8 ..) (1 3 9 ..) lists
(define (factors n)
(list-sort <
(if (<= n 1) '(1)
(for/fold (divs'(1)) ((g (map ppows (group (prime-factors n)))))
(for*/list ((a divs) (b g)) (* a b))))))

View file

@ -0,0 +1,10 @@
(lib 'bigint)
(factors 666)
→ (1 2 3 6 9 18 37 74 111 222 333 666)
(length (factors 108233175859200))
→ 666 ;; 💀
(define huge 1200034005600070000008900000000000000000)
(time ( length (factors huge)))
→ (394ms 7776)

View file

@ -0,0 +1,20 @@
' FB 1.05.0 Win64
Sub printFactors(n As Integer)
If n < 1 Then Return
Print n; " =>";
For i As Integer = 1 To n / 2
If n Mod i = 0 Then Print i; " ";
Next i
Print n
End Sub
printFactors(11)
printFactors(21)
printFactors(32)
printFactors(45)
printFactors(67)
printFactors(96)
Print
Print "Press any key to quit"
Sleep

View file

@ -0,0 +1 @@
def factors( n ) = {d | d <- 1..n if d|n}

View file

@ -0,0 +1,2 @@
for x <- [103, 316, 519, 639, 760]
println( 'The set of factors of ' + x + ' is ' + factors(x) )

View file

@ -0,0 +1,46 @@
include "ConsoleWindow"
clear local mode
local fn IntegerFactors( f as long ) as Str255
dim as long i, s, l(100), c : c = 0
dim as Str255 factorStr
for i = 1 to sqr(f)
if ( f mod i == 0 )
l(c) = i
c++
if ( f <> i ^ 2 )
l(c) = ( f / i )
c++
end if
end if
next i
s = 1
while ( s = 1 )
s = 0
for i = 0 to c-1
if l(i) > l(i+1) and l(i+1) <> 0
swap l(i), l(i+1)
s = 1
end if
next i
wend
for i = 0 to c-1
if ( i < c -1 )
factorStr = factorStr + str$(l(i)) + ","
else
factorStr = factorStr + str$(l(i))
end if
next
end fn = factorStr
print "Factors of 25 are:"; fn IntegerFactors( 25 )
print "Factors of 45 are:"; fn IntegerFactors( 45 )
print "Factors of 103 are:"; fn IntegerFactors( 103 )
print "Factors of 760 are:"; fn IntegerFactors( 760 )
print "Factors of 12345 are:"; fn IntegerFactors( 12345 )
print "Factors of 32766 are:"; fn IntegerFactors( 32766 )
print "Factors of 32767 are:"; fn IntegerFactors( 32767 )
print "Factors of 57097 are:"; fn IntegerFactors( 57097 )
print "Factors of 12345678 are:"; fn IntegerFactors( 12345678 )
print "Factors of 32434243 are:"; fn IntegerFactors( 32434243 )

View file

@ -0,0 +1,4 @@
(defun factors (n)
(list-comp
((<- i (when (== 0 (rem n i))) (lists:seq 1 (trunc (/ n 2)))))
i))

View file

@ -0,0 +1,12 @@
(defun factors (n)
"Tail-recursive prime factors function."
(factors n 2 '()))
(defun factors
((1 _ acc) (++ acc '(1)))
((n _ acc) (when (=< n 0))
#(error undefined))
((n k acc) (when (== 0 (rem n k)))
(factors (div n k) k (cons k acc)))
((n k acc)
(factors n (+ k 1) acc)))

View file

@ -0,0 +1,8 @@
on factors(n)
res = [1]
repeat with i = 2 to n/2
if n mod i = 0 then res.add(i)
end repeat
res.add(n)
return res
end

View file

@ -0,0 +1,6 @@
put factors(45)
-- [1, 3, 5, 9, 15, 45]
put factors(53)
-- [1, 53]
put factors(64)
-- [1, 2, 4, 8, 16, 32, 64]

View file

@ -0,0 +1,15 @@
import intsets, math, algorithm
proc factors(n): seq[int] =
var fs = initIntSet()
for x in 1 .. int(sqrt(float(n))):
if n mod x == 0:
fs.incl(x)
fs.incl(n div x)
result = @[]
for x in fs:
result.add(x)
sort(result, system.cmp[int])
echo factors(45)

View file

@ -0,0 +1,3 @@
Integer method: factors self seq filter(#[ self isMultiple ]) ;
120 factors println

View file

@ -0,0 +1,4 @@
fun factor(n) type integer->integer
f where n.mod(1..n=>f)==0
45.factor

View file

@ -0,0 +1,11 @@
nArray = list(100)
n = 45
j = 0
for i = 1 to n
if n % i = 0 j = j + 1 nArray[j] = i ok
next
see "Factors of " + n + " = "
for i = 1 to j
see "" + nArray[i] + " "
next

View file

@ -0,0 +1 @@
Factors(num(0))[i] := i when num mod i = 0 foreach i within 1 ... num;

View file

@ -0,0 +1,9 @@
Factors(num(0)) :=
let
factorPairs[i] :=
[i] when i = sqrt(num)
else
[i, num/i] when num mod i = 0
foreach i within 1 ... floor(sqrt(num));
in
join(factorPairs);

View file

@ -0,0 +1,11 @@
func factors(n) {
var divs = []
range(1, n.sqrt.int).each { |d|
divs << d if n%%d
}
divs + [divs[-1]**2 == n ? divs.pop : ()] + divs.reverse.map{|d| n/d }
}
[53, 64, 32766].each { |n|
say "factors(#{n}): #{factors(n)}"
}

View file

@ -0,0 +1,4 @@
func factors(n: Int) -> [Int] {
return filter(1...n) { n % $0 == 0 }
}

View file

@ -0,0 +1,18 @@
import func Darwin.sqrt
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
func factors(n: Int) -> [Int] {
var result = [Int]()
for factor in filter (1...sqrt(n), { n % $0 == 0 }) {
result.append(factor)
if n/factor != factor { result.append(n/factor) }
}
return sorted(result)
}

View file

@ -0,0 +1,6 @@
println(factors(4))
println(factors(1))
println(factors(25))
println(factors(63))
println(factors(19))
println(factors(768))

View file

@ -0,0 +1,10 @@
decl int n
set n (int args<1>)
decl int i
for (set i 1) (< i (+ (/ n 2) 1)) (inc i)
if (= (mod n i) 0)
out i " " console
end if
end for
out n endl console

View file

@ -0,0 +1,9 @@
@let {
factors1 &n !-\%%n @to n
factors_tacit @(\\%% !- @to)
[[
!factors1 10
!factors_tacit 100
!factors1 720
]]
}

View file

@ -0,0 +1,16 @@
# This implementation uses "sort" for tidiness
def factors:
. as $num
| reduce range(1; 1 + sqrt|floor) as $i
([];
if ($num % $i) == 0 then
($num / $i) as $r
| if $i == $r then . + [$i] else . + [$i, $r] end
else .
end )
| sort;
def task:
(45, 53, 64) | "\(.): \(factors)" ;
task