Qbasicnews.com

Full Version: Algorithm to determine if a number A is a power of B.
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Just to clear things up...

Quote:al·go·rithm Audio pronunciation of "algorithm" ( P ) Pronunciation Key (lg-rthm)
n.

A step-by-step problem-solving procedure, especially an established, recursive computational procedure for solving a problem in a finite number of steps.

Tongue
Mabey you didn't understand me. I ment I was out, as in out of this compition, as in I'm done, as in I'm not going to work on it, as in good job neo. I can't belive I let that LOG thing get by me....heh

So good bye, me no more reply unless there is another issue with algo.
Is this what you were asking?

Code:
input "a = ", a
input "b = ", b

n = a
while ((n / b) = (n \ b))
    n = n / b
    if n = 1 then
        print a; " is a power of"; b
        sleep
        end
    end if
wend

print a; " is NOT a power of"; b
sleep

...only works with integers >= 1...
Code:
DEFLNG A-Z
C = 1
INPUT "Input small number, big number"; A, B
WHILE C < B
I = I + 1
C = C * A
IF C = B THEN PRINT A; "^"; I; "="; B: END
WEND
PRINT B; "is not a power of "; A
Quib,

Compiled and tested your solution. It works. Nice job!

However, I would suggest you add some validation of the input values. Garbage input will cause errors in your program. Take a look at input validation in Neo's solution.
*****
Stop making horrible iterative procedures to acheive this; it's silly. The whole point of a logarithmic function is to acheive what you're trying to do.

Given the number X, LN(X) / LN(B) is an integer if X is a power of B.

It's that simple. Smile That's what my solution did, but never mind. Neo did the same.
Quote:Stop making horrible iterative procedures to acheive this; it's silly. The whole point of a logarithmic function is to acheive what you're trying to do.

Given the number X, LN(X) / LN(B) is an integer if X is a power of B.

It's that simple. Smile That's what my solution did, but never mind. Neo did the same.
Your right, shiftlynx, but I got bashed earlier in this thread for not allowing iterative procedures. So I was forced to allow them.
*****
Quote:Stop making horrible iterative procedures to acheive this; it's silly. The whole point of a logarithmic function is to acheive what you're trying to do.

Given the number X, LN(X) / LN(B) is an integer if X is a power of B

I did a little research, and some testing. My iterative solution is faster than the logarithmic one, but it only works for integers... However, if I changed it to handle floating point numbers, it would be slower, if X is very small and B is very large. The logarithmic function seems to calculate at a constant speed no matter what. I looked up natural lograthims on the internet, the equation to solve them seems to be iterative... does this mean that the natural log function is built into most FPU's?
Lithium,

I tested the solution above that you submitted May 31. Congratulations, it works fine.
*****
Pages: 1 2 3 4 5