1

I am quite new to learning python and was playing around with the math functions. I tried to create a function that would allow you to find certain powers e.g. squares, cubes. Why is it when I run the code below it lists most of the powers required but manages to miss some.

def more_powers():
    print "For which power do you wish to find: "
    power = int(raw_input("> "))

    print "Choose the upperbound: "
    n = int(raw_input("> "))

    for num in range(2, n):
        for base in range(2, num):
            if log(num, base) / power == 1:
                print "%d is a power of %d." % (num, base)
            else:
                 base += 1


For which power do you wish to find:
> 3
Choose the upperbound:
> 5000
8 is a power of 2.
27 is a power of 3.
64 is a power of 4.
343 is a power of 7.
512 is a power of 8.
729 is a power of 9.
1331 is a power of 11.
1728 is a power of 12.
2197 is a power of 13.
2744 is a power of 14.
3375 is a power of 15.
4096 is a power of 16.

As you can see it misses out the equivalent power for 5, 6, 10 and 17.

1 Answers1

2

Hint:

>>> log(125, 5) / 3 == 1
False
>>> log(125, 5)
3.0000000000000004

>>> log(216, 6) / 3 == 1
False
>>> log(216, 6)
3.0000000000000004
Community
  • 1
  • 1
Idos
  • 15,053
  • 14
  • 60
  • 75