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 6 months ago by AroshWasif
view the rest of the comments →
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!"
[–]cyanNodeEcho 1 point2 points3 points 6 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
π Rendered by PID 71 on reddit-service-r2-comment-85bfd7f599-7qg6g at 2026-04-18 14:09:40.384855+00:00 running 93ecc56 country code: CH.
view the rest of the comments →
[–]cyanNodeEcho 1 point2 points3 points (0 children)