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,20 @@
MX = 524_000_000
N = Math.sqrt(MX).to_u32
x = Array(Int32).new(MX+1, 1)
(2..N).each { |i|
p = i*i
x[p] += i
k = i+i+1
(p+i..MX).step(i) { |j|
x[j] += k
k += 1
}
}
(4..MX).each { |m|
n = x[m]
if n < m && n != 0 && m == x[n]
puts "#{n} #{m}"
end
}

View file

@ -0,0 +1,28 @@
PROGRAM AMICABLE
CONST LIMIT=20000
PROCEDURE SUMPROP(NUM->M)
IF NUM<2 THEN M=0 EXIT PROCEDURE
SUM=1
ROOT=SQR(NUM)
FOR I=2 TO ROOT-1 DO
IF (NUM=I*INT(NUM/I)) THEN
SUM=SUM+I+NUM/I
END IF
IF (NUM=ROOT*INT(NUM/ROOT)) THEN
SUM=SUM+ROOT
END IF
END FOR
M=SUM
END PROCEDURE
BEGIN
PRINT(CHR$(12);) ! CLS
PRINT("Amicable pairs < ";LIMIT)
FOR N=1 TO LIMIT DO
SUMPROP(N->M1)
SUMPROP(M1->M2)
IF (N=M2 AND N<M1) THEN PRINT(N,M1)
END FOR
END PROGRAM

View file

@ -0,0 +1,17 @@
;; using (sum-divisors) from math.lib
(lib 'math)
(define (amicable N)
(define n 0)
(for/list ((m (in-range 2 N)))
(set! n (sum-divisors m))
#:continue (>= n (* 1.5 m)) ;; assume n/m < 1.5
#:continue (<= n m) ;; prevent perfect numbers
#:continue (!= (sum-divisors n) m)
(cons m n)))
(amicable 20000)
→ ((220 . 284) (1184 . 1210) (2620 . 2924) (5020 . 5564) (6232 . 6368) (10744 . 10856) (12285 . 14595) (17296 . 18416))
(amicable 1_000_000) ;; 42 pairs
→ (... (802725 . 863835) (879712 . 901424) (898216 . 980984) (947835 . 1125765) (998104 . 1043096))

View file

@ -0,0 +1,35 @@
' FreeBASIC v1.05.0 win64
Function SumProperDivisors(number As Integer) As Integer
If number < 2 Then Return 0
Dim sum As Integer = 0
For i As Integer = 1 To number \ 2
If number Mod i = 0 Then sum += i
Next
Return sum
End Function
Dim As Integer n, f
Dim As Integer sum(19999)
For n = 1 To 19999
sum(n) = SumProperDivisors(n)
Next
Print "The pairs of amicable numbers below 20,000 are :"
Print
For n = 1 To 19998
' f = SumProperDivisors(n)
f = sum(n)
If f <= n OrElse f < 1 OrElse f > 19999 Then Continue For
If f = sum(n) AndAlso n = sum(f) Then
Print Using "#####"; n;
Print " and "; Using "#####"; sum(n)
End If
Next
Print
Print "Press any key to exit the program"
Sleep
End

View file

@ -0,0 +1,38 @@
' version 04-10-2016
' compile with: fbc -s console
' replaced the function with 2 FOR NEXT loops
#Define max 20000 ' test for pairs below max
#Define max_1 max -1
Dim As String u_str = String(Len(Str(max))+1,"#")
Dim As UInteger n, f
Dim Shared As UInteger sum(max_1)
For n = 2 To max_1
sum(n) = 1
Next
For n = 2 To max_1 \ 2
For f = n * 2 To max_1 Step n
sum(f) += n
Next
Next
Print
Print Using " The pairs of amicable numbers below" & u_str & ", are :"; max
Print
For n = 1 To max_1 -1
f = Sum(n)
If f <= n OrElse f > max Then Continue For
If f = sum(n) AndAlso n = sum(f) Then
Print Using u_str & " and" & u_str ; n; f
End If
Next
' empty keyboard buffer
While Inkey <> "" : Wend
Print : Print : Print " Hit any key to end program"
Sleep
End

View file

@ -0,0 +1,16 @@
fun divisors(n: int): []int =
filter (fn x => n%x == 0) (map (1+) (iota (n/2)))
fun amicable((n: int, nd: int), (m: int, md: int)): bool =
n < m && nd == m && md == n
fun getPair (divs: [upper](int, int)) (flat_i: int): ((int,int), (int,int)) =
let i = flat_i / upper
let j = flat_i % upper
in unsafe (divs[i], divs[j])
fun main(upper: int): [][2]int =
let range = map (1+) (iota upper)
let divs = zip range (map (fn n => reduce (+) 0 (divisors n)) range)
let amicable = filter amicable (map (getPair divs) (iota (upper*upper)))
in map (fn (np,mp) => [np.0, mp.0]) amicable

View file

@ -0,0 +1,41 @@
OPENW 1
CLEARW 1
'
DIM f%(20001) ! sum of proper factors for each n
FOR i%=1 TO 20000
f%(i%)=@sum_proper_divisors(i%)
NEXT i%
' look for pairs
FOR i%=1 TO 20000
FOR j%=i%+1 TO 20000
IF f%(i%)=j% AND i%=f%(j%)
PRINT "Amicable pair ";i%;" ";j%
ENDIF
NEXT j%
NEXT i%
'
PRINT
PRINT "-- found all amicable pairs"
~INP(2)
CLOSEW 1
'
' Compute the sum of proper divisors of given number
'
FUNCTION sum_proper_divisors(n%)
LOCAL i%,sum%,root%
'
IF n%>1 ! n% must be 2 or larger
sum%=1 ! start with 1
root%=SQR(n%) ! note that root% is an integer
' check possible factors, up to sqrt
FOR i%=2 TO root%
IF n% MOD i%=0
sum%=sum%+i% ! i% is a factor
IF i%*i%<>n% ! check i% is not actual square root of n%
sum%=sum%+n%/i% ! so n%/i% will also be a factor
ENDIF
ENDIF
NEXT i%
ENDIF
RETURN sum%
ENDFUNC

View file

@ -0,0 +1,18 @@
from math import sqrt
const N = 524_000_000.int32
proc sumProperDivisors(someNum: int32, chk4less: bool): int32 =
result = 1
let maxPD = sqrt(someNum.float).int32
let offset = someNum mod 2
for divNum in countup(2 + offset, maxPD, 1 + offset):
if someNum mod divNum == 0:
result += divNum + someNum div divNum
if chk4less and result >= someNum:
return 0
for n in countdown(N, 2):
let m = sumProperDivisors(n, true)
if m != 0 and n == sumProperDivisors(m, false):
echo $n, " ", $m

View file

@ -0,0 +1,17 @@
from math import sqrt
const N = 524_000_000.int32
var x = newSeq[int32](N+1)
for i in 2..sqrt(N.float).int32:
var p = i*i
x[p] += i
var j = i + i
while (p += i; p <= N):
j.inc
x[p] += j
for m in 4..N:
let n = x[m] + 1
if n < m and n != 0 and m == x[n] + 1:
echo n, " ", m

View file

@ -0,0 +1,10 @@
Integer method: properDivs self 2 / seq filter(#[ self swap mod 0 == ]) ;
: amicables
| i j |
ListBuffer new
20000 loop: i [
i properDivs sum dup ->j i <= ifTrue: [ continue ]
j properDivs sum i <> ifTrue: [ continue ]
[ i, j ] over add
] ;

View file

@ -0,0 +1,5 @@
integer n
for m=1 to 20000 do
n = sum(factors(m,-1))
if m<n and m=sum(factors(n,-1)) then ?{m,n} end if
end for

View file

@ -0,0 +1,16 @@
size = 18500
for n = 1 to size
m = amicable(n)
if m>n and amicable(m)=n
see "" + n + " and " + m + nl ok
next
see "OK" + nl
func amicable nr
sum = 1
for d = 2 to sqrt(nr)
if nr % d = 0
sum = sum + d
sum = sum + nr / d ok
next
return sum

View file

@ -0,0 +1,15 @@
func propdivsum(x) {
gather {
2.to(x.isqrt).each { |d|
if (x %% d) {
take(d)
take(x/d) if !x.is_sqr
}
}
}.sum(x > 0 ? 1 : 0)
}
1.to(20000).each { |i|
var j = propdivsum(i)
say "#{i} #{j}" if (j>i && i==propdivsum(j))
}

View file

@ -0,0 +1,54 @@
import func Darwin.sqrt
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
func properDivs(n: Int) -> [Int] {
if n == 1 { return [] }
var result = [Int]()
for div in filter (1...sqrt(n), { n % $0 == 0 }) {
result.append(div)
if n/div != div && n/div != n { result.append(n/div) }
}
return sorted(result)
}
func sumDivs(n:Int) -> Int {
struct Cache { static var sum = [Int:Int]() }
if let sum = Cache.sum[n] { return sum }
let sum = properDivs(n).reduce(0) { $0 + $1 }
Cache.sum[n] = sum
return sum
}
func amicable(n:Int, m:Int) -> Bool {
if n == m { return false }
if sumDivs(n) != m || sumDivs(m) != n { return false }
return true
}
var pairs = [(Int, Int)]()
for n in 1 ..< 20_000 {
for m in n+1 ... 20_000 {
if amicable(n, m) {
pairs.append(n, m)
println("\(n, m)")
}
}
}

View file

@ -0,0 +1,44 @@
import func Darwin.sqrt
func sqrt(x:Int) -> Int { return Int(sqrt(Double(x))) }
func sigma(n: Int) -> Int {
if n == 1 { return 0 } // definition of aliquot sum
var result = 1
let root = sqrt(n)
for var div = 2; div <= root; ++div {
if n % div == 0 {
result += div + n/div
}
}
if root*root == n { result -= root }
return (result)
}
func amicables (upTo: Int) -> () {
var aliquot = Array(count: upTo+1, repeatedValue: 0)
for i in 1 ... upTo { // fill lookup array
aliquot[i] = sigma(i)
}
for i in 1 ... upTo {
let a = aliquot[i]
if a > upTo {continue} //second part of pair out-of-bounds
if a == i {continue} //skip perfect numbers
if i == aliquot[a] {
print("\(i, a)")
aliquot[a] = upTo+1 //prevent second display of pair
}
}
}
amicables(20_000)

View file

@ -0,0 +1,26 @@
# unordered
def proper_divisors:
. as $n
| if $n > 1 then 1,
(sqrt|floor as $s
| range(2; $s+1) as $i
| if ($n % $i) == 0 then $i,
(if $i * $i == $n then empty else ($n / $i) end)
else empty
end)
else empty
end;
def addup(stream): reduce stream as $i (0; . + $i);
def task(n):
(reduce range(0; n+1) as $n
( []; . + [$n | addup(proper_divisors)] )) as $listing
| range(1;n+1) as $j
| range(1;$j) as $k
| if $listing[$j] == $k and $listing[$k] == $j
then "\($k) and \($j) are amicable"
else empty
end ;
task(20000)

View file

@ -0,0 +1,9 @@
$ jq -c -n -f amicable_pairs.jq
220 and 284 are amicable
1184 and 1210 are amicable
2620 and 2924 are amicable
5020 and 5564 are amicable
6232 and 6368 are amicable
10744 and 10856 are amicable
12285 and 14595 are amicable
17296 and 18416 are amicable