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
113
Task/Balanced-ternary/Glagol/balanced-ternary.glagol
Normal file
113
Task/Balanced-ternary/Glagol/balanced-ternary.glagol
Normal file
|
|
@ -0,0 +1,113 @@
|
|||
PROGRAM Setun+;
|
||||
USES
|
||||
Parameter IS "...\Departments\Exchange\"
|
||||
Text IS "...\Departments\Numbers\"
|
||||
Output IS "...\Departments\Exchange\";
|
||||
|
||||
VAR
|
||||
AF: RANGE 10 IS SIGN;
|
||||
mfpos: INT;
|
||||
number: INT;
|
||||
memory ACCESS TO STRUCT
|
||||
cell: RANGE 20 IS INT;
|
||||
size: UZKEL;
|
||||
negative: BOOL
|
||||
END;
|
||||
|
||||
PROC Create.Memory;
|
||||
BEGIN
|
||||
CREATE(memory);
|
||||
memory.size := 0;
|
||||
memory.negative := FALSE
|
||||
END Create.Memory;
|
||||
|
||||
PROC Add.Memory(that: INT)
|
||||
BEGIN
|
||||
memory.cells[memory.size] := that;
|
||||
ZOOM(memory.size)
|
||||
END Add.Memory;
|
||||
|
||||
PROC Invert.Memory;
|
||||
VAR
|
||||
zchsl: INT;
|
||||
account: INT;
|
||||
BEGIN
|
||||
FOR cq := 0 TO memory.size DIVIDE 2 - 1 DO
|
||||
zchsl := memory.cells[cq];
|
||||
memory.cells[cq] := memory.cells[memory.size-size-1];
|
||||
memory.cells[memory.size-MF-1] := zchsl
|
||||
END
|
||||
END Invert.Memory;
|
||||
|
||||
PROC Withdraw.Memory;
|
||||
VAR
|
||||
account: INT;
|
||||
BEGIN
|
||||
FOR cq := 0 TO memory.size-1 DO
|
||||
IF memory.cells[cq] < 0 THEN
|
||||
Output.Append("-")
|
||||
ANDIF memory.cells[cq] > 0 THEN
|
||||
Output.Append("+")
|
||||
ELSE Output.Append("0") END
|
||||
END
|
||||
END Withdraw.Memory;
|
||||
|
||||
PROC Remove.Memory;
|
||||
BEGIN
|
||||
memory := Empty
|
||||
END Remove.Memory;
|
||||
|
||||
PROC Translate(number: INT)
|
||||
VAR
|
||||
about: INT;
|
||||
s: BOOL;
|
||||
PROC B.Memory(that: INT)
|
||||
BEGIN
|
||||
IF memory.negative THEN
|
||||
IF that < 0 THEN Add.Memory(1)
|
||||
ANDIF that > 0 THEN Add.Memory(1)
|
||||
ELSE Add.Memory(0) END
|
||||
ELSE
|
||||
Add.Memory(that)
|
||||
END
|
||||
END B.Memory;
|
||||
BEGIN
|
||||
IF number < 0 THEN memory.negative := TRUE END;
|
||||
number := UNIT(number)
|
||||
s := FALSE;
|
||||
WHILE number > 0 DO
|
||||
about := number BALANCE 3;
|
||||
number := number DIVIDE 3;
|
||||
IF s THEN
|
||||
IF about = 2 THEN B.Memory(0) ANDIF about = 1 THEN B.Memory(1) ELSE B.Memory(1) s := FALSE END
|
||||
ELSE
|
||||
IF about = 2 THEN B.Memory(-1) s := TRUE ELSE B.Memory(a) END
|
||||
END
|
||||
END;
|
||||
IF s THEN B.Memory(1) END;
|
||||
Invert.Memory;
|
||||
Withdraw.Memory(TRUE)
|
||||
END Translate;
|
||||
|
||||
PROC InNumber(): INT;
|
||||
VAR
|
||||
MF, MN: INT;
|
||||
result: INT;
|
||||
BEGIN
|
||||
result := 0
|
||||
pl := 1;
|
||||
FOR cq := 0 TO memory.size-1 DO
|
||||
ZOOM(result, memory.Cells[memory.size-cq-1] * mn);
|
||||
pl := pl * 3
|
||||
END;
|
||||
RETURN result;
|
||||
END InNumber;
|
||||
|
||||
BEGIN
|
||||
Parameter.Text(1, AF); mfpos := 0;
|
||||
number := Text.Whole(AF, mfpos);
|
||||
Create.Memory;
|
||||
Translate(number);
|
||||
Output.ChTarget(" = %d.", InNumber(), 0, 0, 0);
|
||||
Remove.Memory
|
||||
END Setun.
|
||||
91
Task/Balanced-ternary/Phix/balanced-ternary-1.phix
Normal file
91
Task/Balanced-ternary/Phix/balanced-ternary-1.phix
Normal file
|
|
@ -0,0 +1,91 @@
|
|||
function bt2dec(string bt)
|
||||
integer res = 0
|
||||
for i=1 to length(bt) do
|
||||
res = 3*res+(bt[i]='+')-(bt[i]='-')
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
function negate(string bt)
|
||||
for i=1 to length(bt) do
|
||||
if bt[i]!='0' then
|
||||
bt[i] = '+'+'-'-bt[i]
|
||||
end if
|
||||
end for
|
||||
return bt
|
||||
end function
|
||||
|
||||
function dec2bt(integer n)
|
||||
string res = "0"
|
||||
integer neg, r
|
||||
if n!=0 then
|
||||
neg = n<0
|
||||
if neg then n = -n end if
|
||||
res = ""
|
||||
while n!=0 do
|
||||
r = mod(n,3)
|
||||
res = "0+-"[r+1]&res
|
||||
n = floor((n+(r=2))/3)
|
||||
end while
|
||||
if neg then res = negate(res) end if
|
||||
end if
|
||||
return res
|
||||
end function
|
||||
|
||||
-- res,carry for a+b+carry lookup tables (not the fastest way to do it, I'm sure):
|
||||
constant {tadd,addres} = columnize({{"---","0-"},{"--0","+-"},{"--+","-0"},
|
||||
{"-0-","+-"},{"-00","-0"},{"-0+","00"},
|
||||
{"-+-","-0"},{"-+0","00"},{"-++","+0"},
|
||||
{"0--","+-"},{"0-0","-0"},{"0-+","00"},
|
||||
{"00-","-0"},{"000","00"},{"00+","+0"},
|
||||
{"0+-","00"},{"0+0","+0"},{"0++","-+"},
|
||||
{"+--","-0"},{"+-0","00"},{"+-+","+0"},
|
||||
{"+0-","00"},{"+00","+0"},{"+0+","-+"},
|
||||
{"++-","+0"},{"++0","-+"},{"+++","0+"}})
|
||||
|
||||
|
||||
function bt_add(string a, string b)
|
||||
integer pad = length(a)-length(b)
|
||||
integer carry = '0'
|
||||
if pad!=0 then
|
||||
if pad<0 then
|
||||
a = repeat('0',-pad)&a
|
||||
else
|
||||
b = repeat('0',pad)&b
|
||||
end if
|
||||
end if
|
||||
for i=length(a) to 1 by -1 do
|
||||
{a[i],carry} = addres[find(a[i]&b[i]&carry,tadd)]
|
||||
end for
|
||||
if carry!='0' then
|
||||
a = carry&a
|
||||
else
|
||||
while length(a)>1 and a[1]='0' do
|
||||
a = a[2..$]
|
||||
end while
|
||||
end if
|
||||
return a
|
||||
end function
|
||||
|
||||
function bt_mul(string a, string b)
|
||||
string pos = a, neg = negate(a), res = "0"
|
||||
integer ch
|
||||
for i=length(b) to 1 by -1 do
|
||||
ch = b[i]
|
||||
if ch='+' then
|
||||
res = bt_add(res,pos)
|
||||
elsif ch='-' then
|
||||
res = bt_add(res,neg)
|
||||
end if
|
||||
pos = pos&'0'
|
||||
neg = neg&'0'
|
||||
end for
|
||||
return res
|
||||
end function
|
||||
|
||||
string a = "+-0++0+", b = dec2bt(-436), c = "+-++-"
|
||||
|
||||
?{bt2dec(a),bt2dec(b),bt2dec(c)}
|
||||
|
||||
string res = bt_mul(a,bt_add(b,negate(c)))
|
||||
?{res,bt2dec(res)}
|
||||
16
Task/Balanced-ternary/Phix/balanced-ternary-2.phix
Normal file
16
Task/Balanced-ternary/Phix/balanced-ternary-2.phix
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
atom t0 = time()
|
||||
string f999 = dec2bt(1)
|
||||
for i=2 to 999 do
|
||||
f999 = bt_mul(f999,dec2bt(i))
|
||||
end for
|
||||
string f1000 = bt_mul(f999,dec2bt(1000))
|
||||
|
||||
printf(1,"In balanced ternary, f999 has %d digits and f1000 has %d digits\n",{length(f999),length(f1000)})
|
||||
|
||||
integer count = 0
|
||||
f999 = negate(f999)
|
||||
while f1000!="0" do
|
||||
f1000 = bt_add(f1000,f999)
|
||||
count += 1
|
||||
end while
|
||||
printf(1,"It took %d subtractions to reach 0. (%3.2fs)\n",{count,time()-t0})
|
||||
Loading…
Add table
Add a link
Reference in a new issue