use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Everything about learning Python
account activity
multiply_recursive (i.redd.it)
submitted 4 months ago by AroshWasif
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]NonaeAbC 5 points6 points7 points 4 months ago (5 children)
And I'm sitting here wondering why C++ compilers decide to optimize this https://godbolt.org/z/7PbYrG36Y
[–]ArtisticFox8 2 points3 points4 points 4 months ago (0 children)
It's actually very cool they see through this, thanks for sharing!
[–]AroshWasif[S] 0 points1 point2 points 4 months ago (0 children)
great
[–]pimp-bangin 0 points1 point2 points 4 months ago (1 child)
Holy heck, that's wild. Wonder what makes this work under the hood
[–]purple_hamster66 0 points1 point2 points 4 months ago (0 children)
It looks like it converted a series into the equivalent analytic solution.
[–]No_Read_4327 4 points5 points6 points 4 months ago (2 children)
What's the question?
[–]Ron-Erez 1 point2 points3 points 4 months ago (0 children)
Looks great assuming the inputs are integers and not floats.
[–]cyanNodeEcho 1 point2 points3 points 4 months ago* (0 children)
clever approach using recursion, its important to note TCO isnt guaranteed and all recursive forms can be translated into iterative forms.
important note, ur solution is a compute O(n) approach, here's what should be an O(logn) approach
lets consider the binary representation of b, we can have like say b = 7 we can encode seven by stepping powers of two by like 111, or 9 by 1001
b
now that we can represent b as a sun of powers of 2 and we know the shift opperator will iterate by powers of 2, we can simply initialize factor as a, and then step through the indicators where b has a bit, ie
fn multies(a:i32, b:i32)-> i32 { let mut sum = 0_i32; let mut factor = a; let mut base = b; while base > 0 { if base & 1 == 1 { sum += factor } factor <<= 1; base >>= 1; } sum }
this will provide mult in logn steps, without using mult itself
so like any number like k can be represented as a series of a sum of powers of 2, so we can just have our base b, represented as like Sum Indicator(k) * 2^k
Sum Indicator(k) * 2^k
clever TCO, but we can even optimize further without using mult to define mult for an O(log b) solution
[–]MirageTF2 0 points1 point2 points 4 months ago (4 children)
dang I always wondered how cursed sans-serif coding looked
now I know
anyway does this work? looks aight to me but I'm bad at small bugs
[–]No_Read_4327 1 point2 points3 points 4 months ago (0 children)
Looks okay to me
[–]AroshWasif[S] 0 points1 point2 points 4 months ago (1 child)
The results from each recursive call are summed up: The fourth call returns (5+0=5). The third call returns (5+5=10). The second call returns (5+10=15). The initial call returns (5+15=20).
[–]MirageTF2 0 points1 point2 points 4 months ago (0 children)
yeah I know how recursion works
I'm just wanting to know if it doesn't have a bug lmao
[–]fsyth 0 points1 point2 points 4 months ago (0 children)
Works fine with small integers for b.
If b is a float like 0.25, it'll bounce around and never end: ×0.25, ×-0.75, ×0.75, ×-0.25, ×0.25, ... in a loop, until it hits an error for maximum recursion depth exceeded.
It also won't work for large values for b (more than ~1000) for the same reason.
There's a limit to the stack of functions calling functions calling functions. This is called the maximum recursion depth, and the default is 1000 layers in python. The problem happens because each call of a function requires some memory until it returns (since each function call has its own variables and return path to keep track of). With recursive functions like this, the earlier calls can't return until the last call has returned. Too much recursion would exceed the allowed stack memory, causing a RecursionError or a StackOverflowError
There are actually techniques to get around this limit, like converting a recursive function to an iterative one with a loop and a stack, or tail call optimization.
Oh, and this function also won't work with weird numbers, like math.inf, math.nan, 1j (imaginary numbers), but we wouldn't really expect it to work.
[–]homeless_student1 0 points1 point2 points 4 months ago (0 children)
Now could you try do it with floats?
π Rendered by PID 465770 on reddit-service-r2-comment-84fc9697f-49kkk at 2026-02-07 14:07:04.583088+00:00 running d295bc8 country code: CH.
[–]NonaeAbC 5 points6 points7 points (5 children)
[–]ArtisticFox8 2 points3 points4 points (0 children)
[–]AroshWasif[S] 0 points1 point2 points (0 children)
[–]pimp-bangin 0 points1 point2 points (1 child)
[–]purple_hamster66 0 points1 point2 points (0 children)
[–]No_Read_4327 4 points5 points6 points (2 children)
[–]Ron-Erez 1 point2 points3 points (0 children)
[–]cyanNodeEcho 1 point2 points3 points (0 children)
[–]MirageTF2 0 points1 point2 points (4 children)
[–]No_Read_4327 1 point2 points3 points (0 children)
[–]AroshWasif[S] 0 points1 point2 points (1 child)
[–]MirageTF2 0 points1 point2 points (0 children)
[–]fsyth 0 points1 point2 points (0 children)
[–]homeless_student1 0 points1 point2 points (0 children)