# Qbasicnews.com

You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3 4 5
Im sure we have seen algorithms to determine if a number is a power of 2.

Now, write an algorithm to determine if a number A is a power of a number B.

Example:
A=125
B=5
Is 125 a power of 5?

Both A and B must be positive whole numbers. The algorithm that I already wrote only handles a value of A up to 32k.

See what you can come up with!
*****
I do programs like these at compitions. Took me not even 5 mins.

Code:
```CLEAR CLS INPUT "Number: ", A INPUT "Base: ", B Value = 0 IsIt = 0 index = -1 DO IF Value > A THEN EXIT DO index = index + 1 Value = B ^ index IF Value = A THEN IsIt = 1: EXIT DO LOOP Message\$ = "No, it is not a power of" + STR\$(B) IF IsIt = 1 THEN Message\$ = "Yes, it is a power of" + STR\$(B) PRINT Message\$ END```

There you go! Challenge Complete!

BTW, Test your program: A = 1, B = anynumber. 1 is a power of ANY number! anynumber ^ 0 = 1.
Using the laws of logs, consider:

a^y = x

Log both sides:

log(a^y) = log(x)

Using laws of logs:

ylog(a) = log(x)

Hence:

y = log(x) / log(a)

Written in code:

[syntax="QBASIC"]
cls

b# = -1
num# = -1
while (b# <= 0) and (num# <= 0)
line input "Number: ", num\$
line input "Base: ", b\$

num# = val(num\$)
b# = val(b\$)

if b# <= 0 then
print "Invalid base; must be greater than zero."
print
elseif num# <= 0 then
print "Invalid number; must be greater than zero."
print
end if
wend

' calculate the exponent.
exponent# = log(num#) / log(b#)
print num\$ + " is " + b\$ + "^" + ltrim\$(str\$(exponent#))

end
[/syntax]

This will also work for negative indices. For example, try putting the number as '0.2' and the base as 5.

-shiftLynx
Well the exponent due to the logs will be any number, even non integer.

See, 6 is a power of 5...just not an integer power. I think that the original challenge was to see whether it was a whole power.
Not exactly sure if this is what you want, but it seems simular to the above ones so I'll go on and submit:

[syntax="qbasic"]CLS
INPUT "Input Number: ", num1
INPUT "Input its #th root to check: ", num2
tst! = (num1) ^ (1 / num2)
text\$ = STR\$(tst!)
cont = 0
DO
cont = cont + 1
IF cont = LEN(text\$) THEN EXIT DO
IF MID\$(text\$, cont, 1) = "." THEN PRINT num2; " is not the #th root of"; num1: END
LOOP
PRINT num2; " is the #th root of "; num1[/syntax]

Ah, yes, he does say they must be whole numbers.

New program:

[syntax="QBASIC"]
cls

b% = -1
num% = -1
while (b% <= 0) or (num% <= 0)
line input "Number: ", num\$
line input "Base: ", b\$

num% = int(val(num\$))
b% = int(val(b\$))

if b% <= 0 then
print "Invalid base; must be greater than zero."
print
elseif num% <= 0 then
print "Invalid number; must be greater than zero."
print
end if
wend

' calculate the exponent.
exponent! = log(num%) / log(b%)
if exponent! <> fix(exponent!) then
print num\$ + " is not an integer power of " + b\$
else
print num\$ + " is a power of " + b\$ + " (exponent=" + ltrim\$(str\$(int(exponent!))) + ")"
end if

end
[/syntax]

Sample:

Code:
```Number: 78125 Base: 5 78125 is a power of 5 (exponent=7)```

-shiftLynx
negatives?
Quote:negatives?

Read:
Quote:Both A and B must be positive whole numbers.

-shiftLynx
Oh, hehe..... hifty: ....ummm...hehe.
Quote:
Code:
```CLEAR CLS INPUT "Number: ", A INPUT "Base: ", B Value = 0 IsIt = 0 index = -1 DO IF Value > A THEN EXIT DO index = index + 1 Value = B ^ index IF Value = A THEN IsIt = 1: EXIT DO LOOP Message\$ = "No, it is not a power of" + STR\$(B) IF IsIt = 1 THEN Message\$ = "Yes, it is a power of" + STR\$(B) PRINT Message\$ END```
Your solution is actually a program or a routine, not an algorithm, because it has a loop.
However, it does work, with one exception. If you enter a number greater than 1 as the A number and then a 1 for the B or base value, it goes into an endless loop.

Nice work, anyway.
*****
Pages: 1 2 3 4 5