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,27 @@
(exact->inexact 67/74)
→ 0.9054054054054054
(inexact->exact 0.9054054054054054)
→ 67/74
(rationalize 0.7978723404255319)
→ 75/94
;; finding rational approximations of PI
(for ((ε (in-range -1 -15 -1)))
(writeln ( format "precision:10^%d %t PI = %d" ε
(rationalize PI (expt 10 e)))))
"precision:10^-1 PI = 16/5"
"precision:10^-2 PI = 22/7" ;;🎩
"precision:10^-3 PI = 201/64"
"precision:10^-4 PI = 333/106"
"precision:10^-5 PI = 355/113" ;; 🎩 🎩
"precision:10^-6 PI = 355/113"
"precision:10^-7 PI = 75948/24175"
"precision:10^-8 PI = 100798/32085"
"precision:10^-9 PI = 103993/33102"
"precision:10^-10 PI = 312689/99532"
"precision:10^-11 PI = 833719/265381"
"precision:10^-12 PI = 4272943/1360120"
"precision:10^-13 PI = 5419351/1725033"
"precision:10^-14 PI = 58466453/18610450"

View file

@ -0,0 +1,62 @@
'' Written in FreeBASIC
'' (no error checking, limited to 64-bit signed math)
type number as longint
#define str2num vallng
#define pow10(n) clngint(10 ^ (n))
function gcd(a as number, b as number) as number
if a = 0 then return b
return gcd(b mod a, a)
end function
function parserational(n as const string) as string
dim as string whole, dec, num, denom
dim as number iwhole, idec, inum, idenom, igcd
'' find positions of '.', '(' and ')' in code
dim as integer dpos, r1pos, r2pos
dpos = instr(n & ".", ".")
r1pos = instr(n & "(", "(")
r2pos = instr(n & ")", ")")
'' extract sections of number (whole, decimal, repeated numerator), generate '999' denominator
whole = left(n, dpos - 1)
dec = mid(n, dpos + 1, r1pos - dpos - 1)
num = mid(n, r1pos + 1, r2pos - r1pos - 1)
denom = string(len(num), "9"): if denom = "" then denom = "1"
'' parse sections to integer
iwhole = str2num(whole)
idec = str2num(dec)
inum = str2num(num)
idenom = str2num(denom)
'' if whole was negative, decimal and repeated sections need to be negative too
if left(ltrim(whole), 1) = "-" then idec = -idec: inum = -inum
'' add decimal part to repeated fraction, and scale down
inum += idec * idenom
idenom *= pow10(len(dec))
'' add integer part to fraction
inum += iwhole * idenom
'' simplify fraction
igcd = abs(gcd(inum, idenom))
if igcd <> 0 then
inum \= igcd
idenom \= igcd
end if
return inum & " / " & idenom & " = " & (inum / idenom)
end function
data "0.9(054)", "0.(518)", "-.12(345)", ""
do
dim as string n
read n
if n = "" then exit do
print n & ":", parserational(n)
loop

View file

@ -0,0 +1,16 @@
function decrat(string s)
integer nom = 0
integer denom = 1
if s[1..2]!="0." then ?9/0 end if
for i=3 to length(s) do
integer ch = s[i]
if ch<'0' or ch>'9' then ?9/0 end if
nom = nom*10 + ch-'0'
denom *= 10
end for
return sq_div({nom,denom},gcd(nom,denom))
end function
?decrat("0.9054054")
?decrat("0.518518")
?decrat("0.75")

View file

@ -0,0 +1,3 @@
'0.9054054 0.518518 0.75'.split.each { |d|
say d.num.as_rat;
}

View file

@ -0,0 +1,3 @@
say 0.9054054.as_rat;
say 0.518518.as_rat;
say 0.75.as_rat;