Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm to determine if a number A is a power of B.
#11
Quote: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
Yes, the laws of logs make for a good solution.
However, the output of your entry does not look like the sample above. It does not say if in fact it is a power of the base number. It just displays the exponent, which could have decimals when it is NOT a power.

Also, you input the values into strings and convert them to integers. If an input has a non-numeric character, the VAL will just ignore it and proceed with whatever is left. Not very clean code.

If the A value is greater that 1 and the B value is 1, you get a error of "division by zero".

All in all, your solution needs more work.
*****
Reply
#12
Quote:Big Grin Not exactly sure if this is what you want, but it seems simular to the above ones so I'll go on and submit:
Sorry, its late, give me a chance to try yours tomorrow.
*****
Reply
#13
And mine?
i]"But...it was so beautifully done"[/i]
Reply
#14
Quote:Yes, the laws of logs make for a good solution.
However, the output of your entry does not look like the sample above. It does not say if in fact it is a power of the base number. It just displays the exponent, which could have decimals when it is NOT a power.

Also, you input the values into strings and convert them to integers. If an input has a non-numeric character, the VAL will just ignore it and proceed with whatever is left. Not very clean code.

If the A value is greater that 1 and the B value is 1, you get a error of "division by zero".

All in all, your solution needs more work.
*****

1) Did you actually run the program? It does tell you whether the number is a power of the base number. If it isn't it tells you that it isn't a power. If it does, it tells you what the power is.

2) Algorithms can contain loops.

3) Division-by-zero, fine that is an unforeseen consequence. But very simple to fix.

4) If you expected somebody to check that the input was actually a number and not that some incompetent fool input characters as a number, why didn't you say that? It's clean code because it's not unnecessarily cluttered.

5) All in all, I think your judging is flawed and this "challenge" is a joke. It is trivial to determine whether a number is a power of a given base, as shown by my program which implements extremely simple math.

Why didn't you make the challenge: "Write an algorithm to find out which number must be added to A to make B". It would be equally trivial.

These challenges are awful.


By the way, your original challenge stated to write an algorithm, so:

1) Given A^x = B where A and B are known, positive integers (where A > 1), use x = ln(B) / ln(A) to determine a value of x.
2) If x is an integer value then B is an integer power of A.

That actually meets your original challenge requirements.

-shiftLynx

[edit: typo]
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#15
Oh, ya thanks...Unnecessarially Cluttered, my FOOT! Tongue

There's a way to check for an intger in 1 line, Z!re knows it, I forgot it, Z!re left, its on Pete's somewhere.

If you don't like this challege then shut up, and go on your way! :evil:
i]"But...it was so beautifully done"[/i]
Reply
#16
Quote:Big Grin 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]

Sorry, I don't understand your logic.
The program runs, but if I give it 8 then 2
it says 2 is not the #th root of 8,
whateever #th root means.
I tried 25 then 5, and got the same error message.

Have you tested it?
*****
Reply
#17
Quote:Oh, ya thanks...Unnecessarially Cluttered, my FOOT! Tongue

There's a way to check for an intger in 1 line, Z!re knows it, I forgot it, Z!re left, its on Pete's somewhere.

If you don't like this challege then shut up, and go on your way! :evil:

Thanks Mit, exactly my feelings.
*****
Reply
#18
Ok, I made this thing during a project class at uni during a break... so don't expect me having tested it, I just wrote it from my thoughts right away Wink

[syntax="QBASIC"]DECLARE FUNCTION GetExponent%(Value AS INTEGER, BaseValue AS INTEGER)

'/********************************************************************
' Calculates and returns the integer exponent of BaseValue^x=Value,
' if it exists, in which x is a positive integer number in I.
' Value and BaseValue are required to be postive integer numbers
' Returns -1 if the exponent doesn't exist
' The parameters and return value all span I-positive < 32767
' ********************************************************************/
FUNCTION GetExponent(Value AS INTEGER, BaseValue AS INTEGER)
' /*****************************************************
' Exclude some general mathematical exceptions to LOG
' *****************************************************/
IF BaseValue = 1 AND Value <> 1 THEN
GetExponent = -1: EXIT FUNCTION
ELSEIF (BaseValue = 1 AND Value = 1) OR (BaseValue = 0 AND Value = 0) THEN
GetExponent = 1: EXIT FUNCTION
END IF
IF BaseValue < 1 OR Value < 0 THEN
GetExponent = -1: EXIT FUNCTION
END IF

'/**********************************
' Calculate the exponent required
' **********************************/
Exponent# = LOG(CDBL(Value)) / LOG(CDBL(BaseValue))

'/**********************************************************
' See if it is an integer exponent and return it if it is
' **********************************************************/
IF Exponent# = INT(Exponent#) THEN
GetExponent = INT(Exponent#)
ELSE
GetExponent = -1
END IF
END FUNCTION
[/syntax]Forgive me this bad-looking code, I haven't coded in a while so it may look a bit messy.

You can call it like
[syntax="QBasic"]PRINT GetExponent(128, 2) 'should return 7 because 2^7=128[/syntax]

A small UI program incorporating this can be as follows (warning: simple and bad code Tongue (don't have much time to do complex fun))
[syntax="QBASIC"]INPUT "Enter the value:", v%
INPUT "Enter the base:", b%

e% = GetExponent(v%, b%)
IF e% = -1 THEN
PRINT "The integer exponent of value "; v%; " and base "; b%; " does not exist"
ELSE
PRINT "The integer exponent of value "; v%; " and base "; b%; " equals "; e%
END IF[/syntax]

Hope it works Big Grin
Reply
#19
I just wanted to point out something: Algorithms can have loops. Upon my definitions, a program is a collection of routines and subroutines, which are made of algorithms.

For example, the bubble sort is called "bubble sort algorithm" and it contains loops.
SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
Reply
#20
Quote:There's a way to check for an intger in 1 line, Z!re knows it, I forgot it

Thats easy

Code:
IF a = INT(a) THEN 'its an integer!
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)