Smallest multiple: Difference between revisions
Content added Content deleted
(Realize in F#) |
(→{{header|Python}}: Added a variant with output which diverges from the other Python version) |
||
Line 498: | Line 498: | ||
200: 406223405488266528941807089475561526729920491439317610846106007364194068990773980214853993805940114288328203598436687354558152299530325929626578124378588043546734967334192274905429717169202850358367337285700993918963748498703256809194612087695134987729083306000942716524953600 |
200: 406223405488266528941807089475561526729920491439317610846106007364194068990773980214853993805940114288328203598436687354558152299530325929626578124378588043546734967334192274905429717169202850358367337285700993918963748498703256809194612087695134987729083306000942716524953600 |
||
</pre> |
</pre> |
||
Or, defining '''lcm''' a little differently, and obtaining results that seem to diverge from those above for N=200 (but match those in Raku etc for N=200 and N=2000): |
|||
<lang python>'''Smallest multiple''' |
|||
from math import gcd |
|||
from functools import reduce |
|||
# smallestEvenlyDivisibleByOneToN :: Integer -> Integer |
|||
def smallestEvenlyDivisibleByOneToN(n): |
|||
'''The smallest positive integer this is evenly |
|||
divisible by all of the integers in [1..N] |
|||
''' |
|||
return reduce( |
|||
uncurry(lcm), |
|||
range(1, 1 + n) |
|||
) |
|||
# ------------------------- TEST ------------------------- |
|||
# main :: IO () |
|||
def main(): |
|||
'''Tests for n drawn from {10, 20, 200}''' |
|||
print( |
|||
'\n'.join([ |
|||
f'{n} -> {smallestEvenlyDivisibleByOneToN(n)}' |
|||
for n in [10, 20, 200, 2000] |
|||
]) |
|||
) |
|||
# ----------------------- GENERIC ------------------------ |
|||
# lcm :: Int -> Int -> Int |
|||
def lcm(x): |
|||
'''The smallest positive integer divisible |
|||
without remainder by both x and y. |
|||
''' |
|||
def go(y): |
|||
return 0 if (0 == x or 0 == y) else abs( |
|||
y * (x // gcd(x, y)) |
|||
) |
|||
return go |
|||
# uncurry :: (a -> b -> c) -> ((a, b) -> c) |
|||
def uncurry(f): |
|||
'''A function over a tuple, |
|||
derived from a curried function. |
|||
''' |
|||
def go(*ab): |
|||
return f(ab[0][0])(ab[0][1]) if ( |
|||
1 == len(ab) |
|||
) else f(ab[0])(ab[1]) |
|||
return go |
|||
# MAIN --- |
|||
if __name__ == '__main__': |
|||
main()</lang> |
|||
{{Out}} |
|||
<pre>10 -> 2520 |
|||
20 -> 232792560 |
|||
200 -> 337293588832926264639465766794841407432394382785157234228847021917234018060677390066992000 |
|||
2000 -> 151117794877444315307536308337572822173736308853579339903227904473000476322347234655122160866668946941993951014270933512030194957221371956828843521568082173786251242333157830450435623211664308500316844478617809101158220672108895053508829266120497031742749376045929890296052805527212315382805219353316270742572401962035464878235703759464796806075131056520079836955770415021318508272982103736658633390411347759000563271226062182345964184167346918225243856348794013355418404695826256911622054015423611375261945905974225257659010379414787547681984112941581325198396634685659217861208771400322507388161967513719166366839894214040787733471287845629833993885413462225294548785581641804620417256563685280586511301918399010451347815776570842790738545306707750937624267501103840324470083425714138183905657667736579430274197734179172691637931540695631396056193786415805463680000</pre> |
|||
=={{header|Raku}}== |
=={{header|Raku}}== |