Speedily calculate base 3 value of real huge integer number with Python 3

后端 未结 1 611
闹比i
闹比i 2021-01-13 04:54

We have a large number like (10**1500000)+1, and want to convert it to base 3. Below is running code with the fastest way we found with normal Python (without using numpy o

相关标签:
1条回答
  • 2021-01-13 05:26

    Here's a method that expands on your convsteps solution by recursing with the base squaring with each call. Some extra work is required to remove leading zeros.

    def number_to_base(n, b):
        if n < b:
            return [n]
        else:
            digits = [d for x in number_to_base(n, b*b) for d in divmod(x, b)]
            return digits if digits[0] else digits[1:]
    

    My quick timing test shows that it's the same as your step2 within the margin of error. But it's simpler and probably has fewer bugs.

    0 讨论(0)
提交回复
热议问题