Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
46
Task/Arithmetic-Rational/Lingo/arithmetic-rational-1.lingo
Normal file
46
Task/Arithmetic-Rational/Lingo/arithmetic-rational-1.lingo
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
-- parent script "Frac"
|
||||
property num
|
||||
property denom
|
||||
|
||||
----------------------------------------
|
||||
-- @constructor
|
||||
-- @param {integer} numerator
|
||||
-- @param {integer} [denominator=1]
|
||||
----------------------------------------
|
||||
on new (me, numerator, denominator)
|
||||
if voidP(denominator) then denominator = 1
|
||||
if denominator=0 then return VOID -- rule out division by zero
|
||||
g = me._gcd(numerator, denominator)
|
||||
if g<>0 then
|
||||
numerator = numerator/g
|
||||
denominator = denominator/g
|
||||
else
|
||||
numerator = 0
|
||||
denominator = 1
|
||||
end if
|
||||
if denominator<0 then
|
||||
numerator = -numerator
|
||||
denominator = -denominator
|
||||
end if
|
||||
me.num = numerator
|
||||
me.denom = denominator
|
||||
return me
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- Returns string representation "<num>/<denom>"
|
||||
-- @return {string}
|
||||
----------------------------------------
|
||||
on toString (me)
|
||||
return me.num&"/"&me.denom
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
--
|
||||
----------------------------------------
|
||||
on _gcd (me, a, b)
|
||||
if a = 0 then return b
|
||||
if b = 0 then return a
|
||||
if a > b then return me._gcd(b, a mod b)
|
||||
return me._gcd(a, b mod a)
|
||||
end
|
||||
83
Task/Arithmetic-Rational/Lingo/arithmetic-rational-2.lingo
Normal file
83
Task/Arithmetic-Rational/Lingo/arithmetic-rational-2.lingo
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
-- Frac library (movie script)
|
||||
|
||||
----------------------------------------
|
||||
-- Shortcut for creating 'frac' values
|
||||
-- @param {integer} numerator
|
||||
-- @param {integer} denominator
|
||||
-- @return {instance}
|
||||
----------------------------------------
|
||||
on frac (numerator, denominator)
|
||||
return script("Frac").new(numerator, denominator)
|
||||
end
|
||||
|
||||
----------------------------------------
|
||||
-- All functions below this comment only support 'fracs', i.e. instances
|
||||
-- of the Frac Class, as arguments. An integer n is casted to frac via frac(n).
|
||||
----------------------------------------
|
||||
|
||||
-- Optionally supports more than 2 arguments
|
||||
on fAdd (a, b) -- ...
|
||||
res = a
|
||||
repeat with i = 2 to the paramCount
|
||||
p = param(i)
|
||||
num = res.num * p.denom + res.denom * p.num
|
||||
denom = res.denom * p.denom
|
||||
res = frac(num, denom)
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
||||
on fSub (a, b)
|
||||
return frac(a.num * b.den - a.den * b.num, a.den * b.den)
|
||||
end
|
||||
|
||||
-- Optionally supports more than 2 arguments
|
||||
on fMul (a, b) -- ...
|
||||
res = a
|
||||
repeat with i = 2 to the paramCount
|
||||
p = param(i)
|
||||
res = frac(res.num * p.num, res.denom * p.denom)
|
||||
end repeat
|
||||
return res
|
||||
end
|
||||
|
||||
on fDiv (a, b)
|
||||
return frac(a.num * b.denom, a.denom * b.num)
|
||||
end
|
||||
|
||||
on fAbs (f)
|
||||
return frac(abs(f.num), f.denom)
|
||||
end
|
||||
|
||||
on fNeg (f)
|
||||
return frac(-f.num, f.denom)
|
||||
end
|
||||
|
||||
on fEQ (a, b)
|
||||
diff = fSub(a, b)
|
||||
return diff.num=0
|
||||
end
|
||||
|
||||
on fNE (a, b)
|
||||
return not fEQ (a, b)
|
||||
end
|
||||
|
||||
on fGT (a, b)
|
||||
diff = fSub(a, b)
|
||||
return diff.num>0
|
||||
end
|
||||
|
||||
on fLT (a, b)
|
||||
diff = fSub(a, b)
|
||||
return diff.num<0
|
||||
end
|
||||
|
||||
on fGE (a, b)
|
||||
diff = fSub(a, b)
|
||||
return diff.num>=0
|
||||
end
|
||||
|
||||
on fLE (a, b)
|
||||
diff = fSub(a, b)
|
||||
return diff.num<=0
|
||||
end
|
||||
13
Task/Arithmetic-Rational/Lingo/arithmetic-rational-3.lingo
Normal file
13
Task/Arithmetic-Rational/Lingo/arithmetic-rational-3.lingo
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
f = frac(2,3)
|
||||
put f.toString()
|
||||
-- "2/3"
|
||||
|
||||
-- fractions are normalized on the fly
|
||||
f = frac(4,6)
|
||||
put f.toString()
|
||||
-- "2/3"
|
||||
|
||||
-- casting integer to frac
|
||||
f = frac(23)
|
||||
put f.toString()
|
||||
-- "23/1"
|
||||
15
Task/Arithmetic-Rational/Lingo/arithmetic-rational-4.lingo
Normal file
15
Task/Arithmetic-Rational/Lingo/arithmetic-rational-4.lingo
Normal file
|
|
@ -0,0 +1,15 @@
|
|||
-- in some movie script
|
||||
----------------------------------------
|
||||
-- Prints all perfect numbers up to n
|
||||
-- @param {integer|float} n
|
||||
----------------------------------------
|
||||
on findPerfects (n)
|
||||
repeat with i = 2 to n
|
||||
sum = frac(1, i)
|
||||
cnt = sqrt(i)
|
||||
repeat with fac = 2 to cnt
|
||||
if i mod fac = 0 then sum = fAdd(sum, frac(1, fac), frac(fac, i))
|
||||
end repeat
|
||||
if sum.denom = sum.num then put i
|
||||
end repeat
|
||||
end
|
||||
|
|
@ -0,0 +1,5 @@
|
|||
findPerfects(power(2, 19))
|
||||
-- 6
|
||||
-- 28
|
||||
-- 496
|
||||
-- 8128
|
||||
Loading…
Add table
Add a link
Reference in a new issue