Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm to determine if a number A is a power of B.
#41
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
Reply
#42
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.
i]"But...it was so beautifully done"[/i]
Reply
#43
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...
COUNT HACKED BY RAZVEEE

RAZVEE IS A SCRIPT KIDDIE- hacker9
Reply
#44
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
Reply
#45
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.
*****
Reply
#46
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.
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#47
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.
*****
Reply
#48
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?
COUNT HACKED BY RAZVEEE

RAZVEE IS A SCRIPT KIDDIE- hacker9
Reply
#49
Lithium,

I tested the solution above that you submitted May 31. Congratulations, it works fine.
*****
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)