Re:How not to teach recursion (2021)
by edflsafoiewq on Saturday June 18, 2022 @03:12PM
(#31792816)
Even the basic schoolbook algorithms are recursive
a * (10*b + c)
= 10*(a*b) + (a*c)
In fact the usual method is even non-tail recursive, you build up a (literal!) stack of intermediate results which you sum back up at the end.The usual method is iterative:
for digit in a, for digit in b, multiply digits and offset by sum of indices of digits. The sun results.
Re:How not to teach recursion (2021)
by contravariant on Saturday June 18, 2022 @07:43PM
(#31794819)
I think most schoolbook algorithms are tail recursive.
They're a bit hard to write down otherwise.
A simpler way multiplication is recursive is the way it is defined for natural numbers in mathematics:
a × b = 0 if b = 0
a × b = a + a × (b - 1) if b ≠ 0
Re:How not to teach recursion (2021)
by nonsapreiche on Saturday June 18, 2022 @09:12PM
(#31795272)
a x b = a if b = 1
Re:How not to teach recursion (2021)
by ethanwillis on Sunday June 19, 2022 @12:25AM
(#31796158)
What is a + a x 0? :)
Re:How not to teach recursion (2021)
by dreamcompiler on Saturday June 18, 2022 @02:50PM
(#31792648)
Exercise: Based on the above discussion, why should squaring an n-digit number be faster than multiplying 2 different n-digit numbers?
The reason the algorithm is recursive is that we're really talking about polynomial multiplication, which is convolution. (Other kinds of multiplication (e.g. dot products) are not convolution and this discussion doesn't apply to them.)
The above is interesting and curious, but is it useful? Yes. Karatsuba multiplication [0] exploits this recursion and adds some clever algebra to reduce the total number of multiplication instructions needed and thus speed up bignum multiplication.
For even bigger numbers, the Schönhage–Strassen algorithm [1] goes even further and uses the Fourier transform to convert the overarching convolution operation to element-by-element (dot-product style) multiplication.
[0] https://en.wikipedia.org/wiki/Karatsuba_algorithm#Recursive_...
[1] https://en.wikipedia.org/wiki/Sch%C3%B6nhage%E2%80%93Strasse...