September Morn Update

This commit is contained in:
Ingy döt Net 2019-09-12 10:33:56 -07:00
parent 4e2d22a71d
commit aac6731f2c
6856 changed files with 141342 additions and 21127 deletions

View file

@ -0,0 +1 @@
--- {}

View file

@ -0,0 +1,30 @@
LCM CSECT
USING LCM,R15 use calling register
L R6,A a
L R7,B b
LR R8,R6 c=a
LOOPW LR R4,R8 c
SRDA R4,32 shift to next reg
DR R4,R7 c/b
LTR R4,R4 while c mod b<>0
BZ ELOOPW leave while
AR R8,R6 c+=a
B LOOPW end while
ELOOPW LPR R9,R6 c=abs(u)
L R1,A a
XDECO R1,XDEC edit a
MVC PG+4(5),XDEC+7 move a to buffer
L R1,B b
XDECO R1,XDEC edit b
MVC PG+10(5),XDEC+7 move b to buffer
XDECO R8,XDEC edit c
MVC PG+17(10),XDEC+2 move c to buffer
XPRNT PG,80 print buffer
XR R15,R15 return code =0
BR R14 return to caller
A DC F'1764' a
B DC F'3920' b
PG DC CL80'lcm(00000,00000)=0000000000' buffer
XDEC DS CL12 temp for edit
YREGS
END LCM

View file

@ -0,0 +1,8 @@
100 DEF LCM(A,B)=(A*B)/GCD(A,B)
110 DEF GCD(A,B)
120 DO WHILE B>0
130 LET T=B:LET B=MOD(A,B):LET A=T
140 LOOP
150 LET GCD=A
160 END DEF
170 PRINT LCM(12,18)

View file

@ -1,11 +1,11 @@
import extensions.
import system'math.
import extensions;
import system'math;
gcd = (:m:n)((n == 0)iif(m absolute, $(gcd(n,n mod:m)))).
gcd = (m,n => (n == 0) ? (m.Absolute) : (gcd(n,n.mod:m)));
lcm = (:m:n)((m * n) absolute / gcd(m,n)).
lcm = (m,n => (m * n).Absolute / gcd(m,n));
program =
[
console printLine("lcm(12,18)=",lcm(12,18)).
].
public program()
{
console.printLine("lcm(12,18)=",lcm(12,18))
}

View file

@ -1,15 +1,15 @@
print "Least Common Multiple of 12 and 18 is ";LCM(12,18)
print "Least Common Multiple of 12 and 18 is "; LCM(12, 18)
end
function LCM(m,n)
LCM=abs(m*n)/GCD(m,n)
end function
function LCM(m, n)
LCM = abs(m * n) / GCD(m, n)
end function
function GCD(a,b)
function GCD(a, b)
while b
c = a
a = b
b = c mod b
wend
GCD = abs(a)
end function
end function

View file

@ -1,22 +1,98 @@
from prime_decomposition import decompose
try:
reduce
except NameError:
from functools import reduce
'''Least common multiple'''
def lcm(a, b):
mul = int.__mul__
if a and b:
da = list(decompose(abs(a)))
db = list(decompose(abs(b)))
merge= da
for d in da:
if d in db: db.remove(d)
merge += db
return reduce(mul, merge, 1)
return 0
from inspect import signature
# lcm :: Int -> Int -> Int
def lcm(x):
'''The smallest positive integer divisible
without remainder by both x and y.
'''
return lambda y: 0 if 0 in (x, y) else abs(
y * (x // gcd_(x)(y))
)
# gcd_ :: Int -> Int -> Int
def gcd_(x):
'''The greatest common divisor in terms of
the divisibility preordering.
'''
def go(a, b):
return go(b, a % b) if 0 != b else a
return lambda y: go(abs(x), abs(y))
# TEST ----------------------------------------------------
# main :: IO ()
def main():
'''Tests'''
print(
fTable(
__doc__ + 's of 60 and [12..20]:'
)(repr)(repr)(
lcm(60)
)(enumFromTo(12)(20))
)
pairs = [(0, 2), (2, 0), (-6, 14), (12, 18)]
print(
fTable(
'\n\n' + __doc__ + 's of ' + repr(pairs) + ':'
)(repr)(repr)(
uncurry(lcm)
)(pairs)
)
# GENERIC -------------------------------------------------
# enumFromTo :: (Int, Int) -> [Int]
def enumFromTo(m):
'''Integer enumeration from m to n.'''
return lambda n: list(range(m, 1 + n))
# uncurry :: (a -> b -> c) -> ((a, b) -> c)
def uncurry(f):
'''A function over a tuple, derived from
a vanilla or curried function.
'''
if 1 < len(signature(f).parameters):
return lambda xy: f(*xy)
else:
return lambda xy: f(xy[0])(xy[1])
# unlines :: [String] -> String
def unlines(xs):
'''A single string derived by the intercalation
of a list of strings with the newline character.
'''
return '\n'.join(xs)
# FORMATTING ----------------------------------------------
# fTable :: String -> (a -> String) ->
# (b -> String) -> (a -> b) -> [a] -> String
def fTable(s):
'''Heading -> x display function -> fx display function ->
f -> xs -> tabular string.
'''
def go(xShow, fxShow, f, xs):
ys = [xShow(x) for x in xs]
w = max(map(len, ys))
return s + '\n' + '\n'.join(map(
lambda x, y: y.rjust(w, ' ') + ' -> ' + fxShow(f(x)),
xs, ys
))
return lambda xShow: lambda fxShow: lambda f: lambda xs: go(
xShow, fxShow, f, xs
)
# MAIN ---
if __name__ == '__main__':
print( lcm(12, 18) ) # 36
print( lcm(-6, 14) ) # 42
assert lcm(0, 2) == lcm(2, 0) == 0
main()

View file

@ -1,19 +1,22 @@
>>> def lcm(*values):
values = set([abs(int(v)) for v in values])
if values and 0 not in values:
n = n0 = max(values)
values.remove(n)
while any( n % m for m in values ):
n += n0
return n
return 0
from prime_decomposition import decompose
try:
reduce
except NameError:
from functools import reduce
>>> lcm(-6, 14)
42
>>> lcm(2, 0)
0
>>> lcm(12, 18)
36
>>> lcm(12, 18, 22)
396
>>>
def lcm(a, b):
mul = int.__mul__
if a and b:
da = list(decompose(abs(a)))
db = list(decompose(abs(b)))
merge= da
for d in da:
if d in db: db.remove(d)
merge += db
return reduce(mul, merge, 1)
return 0
if __name__ == '__main__':
print( lcm(12, 18) ) # 36
print( lcm(-6, 14) ) # 42
assert lcm(0, 2) == lcm(2, 0) == 0

View file

@ -1,18 +1,19 @@
>>> def lcm(p,q):
p, q = abs(p), abs(q)
m = p * q
if not m: return 0
while True:
p %= q
if not p: return m // q
q %= p
if not q: return m // p
>>> def lcm(*values):
values = set([abs(int(v)) for v in values])
if values and 0 not in values:
n = n0 = max(values)
values.remove(n)
while any( n % m for m in values ):
n += n0
return n
return 0
>>> lcm(-6, 14)
42
>>> lcm(12, 18)
36
>>> lcm(2, 0)
0
>>> lcm(12, 18)
36
>>> lcm(12, 18, 22)
396
>>>

View file

@ -0,0 +1,18 @@
>>> def lcm(p,q):
p, q = abs(p), abs(q)
m = p * q
if not m: return 0
while True:
p %= q
if not p: return m // q
q %= p
if not q: return m // p
>>> lcm(-6, 14)
42
>>> lcm(12, 18)
36
>>> lcm(2, 0)
0
>>>

View file

@ -0,0 +1,12 @@
Function gcd(u As Long, v As Long) As Long
Dim t As Long
Do While v
t = u
u = v
v = t Mod v
Loop
gcd = u
End Function
Function lcm(m As Long, n As Long) As Long
lcm = Abs(m * n) / gcd(m, n)
End Function