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
|
|
@ -0,0 +1,39 @@
|
|||
(lib 'math) ;; for poly multiplication
|
||||
|
||||
;; convert string of decimal digits to polynomial
|
||||
;; "1234" → x^3 +2x^2 +3x +4
|
||||
;; least-significant digit first
|
||||
(define (string->long N)
|
||||
(reverse (map string->number (string->list N))))
|
||||
|
||||
;; convert polynomial to string
|
||||
(define (long->string N)
|
||||
(if (pair? N)
|
||||
(string-append (number->string (first N)) (long->string (rest N))) ""))
|
||||
|
||||
;; convert poly coefficients to base 10
|
||||
(define (poly->10 P (carry 0))
|
||||
(append
|
||||
(for/list ((coeff P))
|
||||
(set! coeff (+ carry coeff ))
|
||||
(set! carry (quotient coeff 10)) ;; new carry
|
||||
(modulo coeff 10))
|
||||
(if(zero? carry) null (list carry)))) ;; remove leading 0 if any
|
||||
|
||||
;; long multiplication
|
||||
;; convert input - strings of decimal digits - to polynomials
|
||||
;; perform poly multiplication in base 10
|
||||
;; convert result to string of decimal digits
|
||||
|
||||
(define (long-mul A B )
|
||||
(long->string (reverse (poly->10 (poly-mul (string->long A) (string->long B))))))
|
||||
|
||||
(define two-64 "18446744073709551616")
|
||||
(long-mul two-64 two-64)
|
||||
→ "340282366920938463463374607431768211456"
|
||||
|
||||
;; check it
|
||||
(lib 'bigint)
|
||||
Lib: bigint.lib loaded.
|
||||
(expt 2 128)
|
||||
→ 340282366920938463463374607431768211456
|
||||
41
Task/Long-multiplication/Nim/long-multiplication.nim
Normal file
41
Task/Long-multiplication/Nim/long-multiplication.nim
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
import strutils
|
||||
|
||||
proc ti(a): int = ord(a) - ord('0')
|
||||
|
||||
proc longmulti(a, b: string): string =
|
||||
var
|
||||
i, j, n, carry, la, lb = 0
|
||||
k = false
|
||||
|
||||
# either is zero, return "0"
|
||||
if a == "0" or b == "0":
|
||||
return "0"
|
||||
|
||||
# see if either a or b is negative
|
||||
if a[0] == '-':
|
||||
i = 1; k = not k
|
||||
if b[0] == '-':
|
||||
j = 1; k = not k
|
||||
|
||||
# if yes, prepend minus sign if needed and skip the sign
|
||||
if i > 0 or j > 0:
|
||||
result = if k: "-" else: ""
|
||||
result.add longmulti(a[i..a.high], b[j..b.high])
|
||||
return
|
||||
|
||||
result = repeatChar(a.len + b.len, '0')
|
||||
|
||||
for i in countdown(a.high, 0):
|
||||
var carry = 0
|
||||
var k = i + b.len
|
||||
for j in countdown(b.high, 0):
|
||||
let n = ti(a[i]) * ti(b[j]) + ti(result[k]) + carry
|
||||
carry = n div 10
|
||||
result[k] = chr(n mod 10 + ord('0'))
|
||||
dec k
|
||||
result[k] = chr(ord(result[k]) + carry)
|
||||
|
||||
if result[0] == '0':
|
||||
result[0..result.high-1] = result[1..result.high]
|
||||
|
||||
echo longmulti("-18446744073709551616", "-18446744073709551616")
|
||||
28
Task/Long-multiplication/Oforth/long-multiplication.oforth
Normal file
28
Task/Long-multiplication/Oforth/long-multiplication.oforth
Normal file
|
|
@ -0,0 +1,28 @@
|
|||
Number Class new: Natural(v)
|
||||
|
||||
Natural method: initialize := v ;
|
||||
Natural method: _v @v ;
|
||||
|
||||
Natural classMethod: newValues super new ;
|
||||
Natural classMethod: newFrom asList self newValues ;
|
||||
|
||||
Natural method: *(n)
|
||||
| v i j l x k |
|
||||
n _v ->v
|
||||
ListBuffer initValue(@v size v size + 1+, 0) ->l
|
||||
|
||||
v size loop: i [
|
||||
i v at dup ->x 0 ifEq: [ continue ]
|
||||
0 @v size loop: j [
|
||||
i j + 1- ->k
|
||||
j @v at x * + l at(k) + 1000000000 /mod k rot l put
|
||||
]
|
||||
k 1+ swap l put
|
||||
]
|
||||
while(l last 0 == l size 0 <> and) [ l removeLast drop ]
|
||||
l dup freeze Natural newValues ;
|
||||
|
||||
Natural method: <<
|
||||
| i |
|
||||
@v last <<
|
||||
@v size 1 - loop: i [ @v at(@v size i -) <<wjp(0, JUSTIFY_RIGHT, 8) ] ;
|
||||
54
Task/Long-multiplication/Phix/long-multiplication.phix
Normal file
54
Task/Long-multiplication/Phix/long-multiplication.phix
Normal file
|
|
@ -0,0 +1,54 @@
|
|||
constant base = 1_000_000_000
|
||||
|
||||
function bcd9_mult(sequence a, sequence b)
|
||||
sequence c
|
||||
integer j
|
||||
atom ci
|
||||
c = repeat(0,length(a)+length(b))
|
||||
for i=1 to length(a) do
|
||||
j = i+length(b)-1
|
||||
c[i..j] = sq_add(c[i..j],sq_mul(a[i],b))
|
||||
end for
|
||||
|
||||
for i=1 to length(c) do
|
||||
ci = c[i]
|
||||
if ci>base then
|
||||
c[i+1] += floor(ci/base) -- carry
|
||||
c[i] = remainder(ci,base)
|
||||
end if
|
||||
end for
|
||||
|
||||
if c[$]=0 then
|
||||
c = c[1..$-1]
|
||||
end if
|
||||
return c
|
||||
end function
|
||||
|
||||
function atom_to_bcd9(atom a)
|
||||
sequence s = {}
|
||||
while a>0 do
|
||||
s = append(s,remainder(a,base))
|
||||
a = floor(a/base)
|
||||
end while
|
||||
return s
|
||||
end function
|
||||
|
||||
function bcd9_to_str(sequence a)
|
||||
string s = sprintf("%d",a[$])
|
||||
for i=length(a)-1 to 1 by -1 do
|
||||
s &= sprintf("%09d",a[i])
|
||||
end for
|
||||
-- (might want to trim leading 0s here)
|
||||
return s
|
||||
end function
|
||||
|
||||
sequence a, b, c
|
||||
|
||||
a = atom_to_bcd9(power(2,32))
|
||||
printf(1,"a is %s\n",{bcd9_to_str(a)})
|
||||
|
||||
b = bcd9_mult(a,a)
|
||||
printf(1,"a*a is %s\n",{bcd9_to_str(b)})
|
||||
|
||||
c = bcd9_mult(b,b)
|
||||
printf(1,"a*a*a*a is %s\n",{bcd9_to_str(c)})
|
||||
|
|
@ -0,0 +1 @@
|
|||
say (2**64 * 2**64);
|
||||
37
Task/Long-multiplication/Sidef/long-multiplication-2.sidef
Normal file
37
Task/Long-multiplication/Sidef/long-multiplication-2.sidef
Normal file
|
|
@ -0,0 +1,37 @@
|
|||
func add_with_carry(result, addend, addendpos) {
|
||||
loop {
|
||||
while (result.len < addendpos+1) {
|
||||
result.append(0);
|
||||
};
|
||||
var addend_digits = (addend.to_i + result[addendpos].to_i -> to_chars);
|
||||
result[addendpos] = addend_digits.pop;
|
||||
addend_digits.len > 0 || break;
|
||||
addend = addend_digits.pop;
|
||||
addendpos++;
|
||||
}
|
||||
}
|
||||
|
||||
func longhand_multiplication(multiplicand, multiplier) {
|
||||
|
||||
var result = [];
|
||||
var multiplicand_offset = 0;
|
||||
|
||||
multiplicand.reverse.each { |multiplicand_digit|
|
||||
var multiplier_offset = multiplicand_offset;
|
||||
multiplier.reverse.each { |multiplier_digit|
|
||||
var multiplication_result = (multiplicand_digit.to_i * multiplier_digit.to_i -> to_s);
|
||||
|
||||
var addend_offset = multiplier_offset;
|
||||
multiplication_result.reverse.each { |result_digit_addend|
|
||||
add_with_carry(result, result_digit_addend, addend_offset);
|
||||
addend_offset++;
|
||||
};
|
||||
multiplier_offset++;
|
||||
};
|
||||
multiplicand_offset++;
|
||||
};
|
||||
|
||||
return result.join.reverse;
|
||||
}
|
||||
|
||||
say longhand_multiplication('18446744073709551616', '18446744073709551616');
|
||||
50
Task/Long-multiplication/Sidef/long-multiplication-3.sidef
Normal file
50
Task/Long-multiplication/Sidef/long-multiplication-3.sidef
Normal file
|
|
@ -0,0 +1,50 @@
|
|||
func long_multiplication(String a, String b) -> String {
|
||||
|
||||
if (a.len < b.len) {
|
||||
(a, b) = (b, a)
|
||||
}
|
||||
|
||||
'0' ~~ [a, b] && return '0'
|
||||
|
||||
var x = a.reverse.chars.map{.to_n}
|
||||
var y = b.reverse.chars.map{.to_n}
|
||||
|
||||
var xlen = x.end
|
||||
var ylen = y.end
|
||||
|
||||
var mem = 0
|
||||
var map = y.len.of { [] }
|
||||
|
||||
for j in ^y {
|
||||
for i in ^x {
|
||||
var n = (x[i]*y[j] + mem)
|
||||
var(d, m) = n.divmod(10)
|
||||
if (i == xlen) {
|
||||
map[j] << (m, d)
|
||||
mem = 0;
|
||||
}
|
||||
else {
|
||||
map[j] << m
|
||||
mem = d
|
||||
}
|
||||
}
|
||||
|
||||
var n = (ylen - j)
|
||||
n > 0 && map[j].append(n.of(0)...)
|
||||
var m = (ylen - n)
|
||||
m > 0 && map[j].prepend(m.of(0)...)
|
||||
}
|
||||
|
||||
var result = []
|
||||
var mrange = ^map
|
||||
var end = (xlen + ylen + 2)
|
||||
|
||||
for i in ^end {
|
||||
var n = (mrange.map {|j| map[j][i] }.sum + mem)
|
||||
(mem, result[result.end+1]) = n.divmod(10)
|
||||
}
|
||||
|
||||
result.join.reverse -= /^0+/
|
||||
}
|
||||
|
||||
say long_multiplication('18446744073709551616', '18446744073709551616')
|
||||
46
Task/Long-multiplication/jq/long-multiplication-1.jq
Normal file
46
Task/Long-multiplication/jq/long-multiplication-1.jq
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
# multiply two decimal strings, which may be signed (+ or -)
|
||||
def long_multiply(num1; num2):
|
||||
|
||||
def stripsign:
|
||||
.[0:1] as $a
|
||||
| if $a == "-" then [ -1, .[1:]]
|
||||
elif $a == "+" then [ 1, .[1:]]
|
||||
else [1, .]
|
||||
end;
|
||||
|
||||
def adjustsign(sign):
|
||||
if sign == 1 then . else "-" + . end;
|
||||
|
||||
# mult/2 assumes neither argument has a sign
|
||||
def mult(num1;num2):
|
||||
(num1 | explode | map(.-48) | reverse) as $a1
|
||||
| (num2 | explode | map(.-48) | reverse) as $a2
|
||||
| reduce range(0; num1|length) as $i1
|
||||
([]; # result
|
||||
reduce range(0; num2|length) as $i2 (.;
|
||||
($i1 + $i2) as $ix
|
||||
| ( $a1[$i1] * $a2[$i2] +
|
||||
(if $ix >= length then 0
|
||||
else .[$ix]
|
||||
end) ) as $r
|
||||
| if $r > 9 # carrying
|
||||
then
|
||||
.[$ix + 1] = ($r / 10 | floor) +
|
||||
(if $ix + 1 >= length then 0
|
||||
else .[$ix + 1]
|
||||
end)
|
||||
| .[$ix] = $r - ( $r / 10 | floor ) * 10
|
||||
else
|
||||
.[$ix] = $r
|
||||
end
|
||||
)
|
||||
)
|
||||
| reverse | map(.+48) | implode;
|
||||
|
||||
(num1|stripsign) as $a1
|
||||
| (num2|stripsign) as $a2
|
||||
| if $a1[1] == "0" or $a2[1] == "0" then "0"
|
||||
elif $a1[1] == "1" then $a2[1]|adjustsign( $a1[0] * $a2[0] )
|
||||
elif $a2[1] == "1" then $a1[1]|adjustsign( $a1[0] * $a2[0] )
|
||||
else mult($a1[1]; $a2[1]) | adjustsign( $a1[0] * $a2[0] )
|
||||
end;
|
||||
18
Task/Long-multiplication/jq/long-multiplication-2.jq
Normal file
18
Task/Long-multiplication/jq/long-multiplication-2.jq
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
# Emit (input)^i where input and i are non-negative decimal integers,
|
||||
# represented as numbers and/or strings.
|
||||
def long_power(i):
|
||||
def power(i):
|
||||
tostring as $self
|
||||
| (i|tostring) as $i
|
||||
| if $i == "0" then "1"
|
||||
elif $i == "1" then $self
|
||||
elif $self == "0" then "0"
|
||||
else reduce range(1;i) as $_ ( $self; long_multiply(.; $self) )
|
||||
end;
|
||||
|
||||
(i|tonumber) as $i
|
||||
| if $i < 4 then power($i)
|
||||
else ($i|sqrt|floor) as $j
|
||||
| ($i - $j*$j) as $k
|
||||
| long_multiply( power($j) | power($j) ; power($k) )
|
||||
end ;
|
||||
1
Task/Long-multiplication/jq/long-multiplication-3.jq
Normal file
1
Task/Long-multiplication/jq/long-multiplication-3.jq
Normal file
|
|
@ -0,0 +1 @@
|
|||
2 | long_power(64) | long_multiply(.;.)
|
||||
Loading…
Add table
Add a link
Reference in a new issue