Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8272509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T07:08:36+00:00 2026-06-08T07:08:36+00:00

Based on this answer , I’m implementing a simple algorithm in Python 3.x to

  • 0

Based on this answer, I’m implementing a simple algorithm in Python 3.x to determine whether or not a integer n is a power of another integer base. However, the algorithm doesn’t return the correct result. The code in the linked answer is:

while (n % 3 == 0) {
    n /= 3;
}
return n == 1;

(A comment indicates that checking n == 0 is also necessary, logically). This is my code:

def is_power(n: int, base: int) -> bool:
    if n == 0:
        return false
    while (n % base == 0):
        n /= base
    return n == 1

I wrote simple testing code that tests a certain range of bases and exponents, but the results it returns are incorrect. Testing code:

for base in range(3, 10):
    print("Base: {0}".format(base))
    for exp in range(30, 40):
        b = is_power(pow(base, exp), base)
        if not(b):
            print("{: <3}: {: <5}".format(exp, str(b)))

I tested this with a much larger range, but for the sake of output, I limited it in this example. This outputs:

Base: 3
35 : False
36 : False
37 : False
38 : False
39 : False
Base: 4
Base: 5
30 : False
31 : False
32 : False
33 : False
34 : False
35 : False
36 : False
37 : False
38 : False
39 : False
Base: 6
35 : False
36 : False
37 : False
38 : False
39 : False
Base: 7
30 : False
31 : False
32 : False
33 : False
34 : False
35 : False
36 : False
37 : False
38 : False
39 : False
Base: 8
Base: 9
30 : False
31 : False
32 : False
33 : False
34 : False
35 : False
36 : False
37 : False
38 : False
39 : False

which is clearly incorrect. I’ve tried debugging a small example, where n = pow(3, 35) and base = 3 yields these values of n in the loop:

50031545098999707
1.6677181699666568e+16

and the loop ends, because 50031545098999707 / 3 == 1.667718169966656 9 e+16 (note that the last digit is different). Is this the problem? Are Python’s calculations failing? If not, what is the problem with this algorithm?

The algorithm also fails even more if I use math.pow instead, but I’m not necessarily surprised by that, since checking one example demonstrates that pow and math.pow don’t always return the same value.

import math
pow(3, 35) == math.pow(3, 35) # 50031545098999707 != 5.0031545098999704e+16
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-08T07:08:37+00:00Added an answer on June 8, 2026 at 7:08 am

    Since you are using Python 3, you should use

    def is_power(n: int, base: int) -> bool:
        if n == 0:
            return false
        while (n % base == 0):
            n //= base
        #     ^^ note two slashes
        return n == 1
    

    In Python 3.x, the / operation always perform “real division” and return a floating point number, but in the algorithm you’ve linked to, it expects the / to do integer division, which is done with the // (“floor division”) operator instead.

    And the test fails really because of the lack of precision in floating-point arithmetic, as you have expected. The algorithm is still correct when / returns an infinite-precision real number. Take 3.034 as an example,

    3.0 ** 34 == 16677181699666569
              == 0b111011001111111100111011110011000100000011001010001001
    

    The default floating-point format only supports 53 bits of precision, but the above number requires 54 bits to represent, so we have to round off the last bit (the 1), resulting in

                 0b111011001111111100111011110011000100000011001010001000
                                                                        ^
              == 16677181699666568
    

    which the modulus to 3 will return 2, which will break the loop and return false.

    (I haven’t checked why it rounds down instead of up, but even if it rounds up the modulus is still not zero, so it will still return false.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to print out rotating fan based on this answer with Python. import
I am trying to create a simple rating controller based on this answer. Rails
This seems like it should be simple, but I'm not finding an answer anywhere.
This is a further question based on this answer: How can I implement a
Based on the answer for this question What's the difference between CompositionBatch and catalogs?
Based the accepted answer to this question I've setup a NetBeans/tomcat environment. In testing
This question is based on my previous question which I got a working answer
UPDATE: Solved. Thanks BusyMark! EDIT: This is revised based on the answer below from
Based on this answer Regular Expressions: Is there an AND operator? I tried the
I'm experimenting with XSLT2, using a stylesheet based on this answer: <xsl:stylesheet version=2.0 xmlns:xsl=http://www.w3.org/1999/XSL/Transform>

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.