Qbasicnews.com
September 21, 2019, 09:03:23 AM *
Welcome, Guest. Please login or register.

Login with username, password and session length
News: Back to Qbasicnews.com | QB Online Help | FAQ | Chat | All Basic Code | QB Knowledge Base
 
   Home   Help Search Login Register  
Pages: 1 [2] 3 4
  Print  
Author Topic: Algorithm to determine if a number A is a power of B.  (Read 27104 times)
Moneo
Na_th_an
*****
Posts: 1971


« Reply #15 on: March 03, 2005, 12:17:38 AM »

Quote from: "Rattrapmax6"
Cheesy 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?
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #16 on: March 03, 2005, 12:18:51 AM »

Quote from: "Mitth'raw'nuruodo"
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.
*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #17 on: March 03, 2005, 06:13:12 AM »

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 Cheesy
Logged
na_th_an
*/-\*
*****
Posts: 8244



WWW
« Reply #18 on: March 03, 2005, 07:54:51 AM »

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.
Logged

SCUMM (the band) on Myspace!
ComputerEmuzone Games Studio
underBASIC, homegrown musicians
[img]http://www.ojodepez-fanzine.net/almacen/yoghourtslover.png[/i
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #19 on: March 03, 2005, 07:58:47 AM »

Quote from: "Mitth'raw'nuruodo"
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!
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #20 on: March 03, 2005, 10:27:44 AM »

Quote from: "dark_prevail"
Quote from: "Mitth'raw'nuruodo"
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!

You looked at my code! Not fair! Cheesy j/k Wink

[syntax="QBASIC"]IF Exponent# = INT(Exponent#) THEN[/syntax]



Quote from: "nathan"
For example, the bubble sort is called "bubble sort algorithm" and it contains loops

I don't like the idea of sorting without loops.... Cheesy
Logged
KiZ
__/--\__
*****
Posts: 2879


WWW
« Reply #21 on: March 03, 2005, 03:18:00 PM »

Quote from: "Neo"
You looked at my code! Not fair! Cheesy j/k Wink

[syntax="QBASIC"]IF Exponent# = INT(Exponent#) THEN[/syntax]


Sorry! but I didn't look at your code. =)
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #22 on: March 03, 2005, 11:49:38 PM »

Quote from: "na_th_an"
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.

Nathan,
You're right, considering the general usage of the word algorithm. I looked for the definition of the word in Donald Knuth's Book "The Art of Programming, Fundamental Algorithms". After a long explanation of the derivation of the word, Knuth then uses an example: The Euclid Algorithm, which uses a loop. So, I stand corrected.

Just let me say that in the programming world which I've lived in for 44 years, an algorithm was almost the same as a formula, although algorithms can use AND, OR, XOR where formulas generally don't.  And, formulas don't use loops --- not any that I've seen.

Therefore, Knuth or not, I would call the Euclid Algorithm a program, a routine, a subroutine, or a function.
*****
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #23 on: March 03, 2005, 11:52:43 PM »

Quote from: "Neo"
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

Neo,
I'll get back to test your "lunch break work of art" as soon as I can.
*****
Logged
shiftLynx
Wandering Guru
***
Posts: 340



WWW
« Reply #24 on: March 04, 2005, 01:05:22 PM »

Formulae can use loops, sort of... reduction formulae is a classic example.

-shiftLynx
Logged

img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Neo
Na_th_an
*****
Posts: 2150



« Reply #25 on: March 04, 2005, 11:35:42 PM »

It seems I forgot an exception... :lol:

Corrected exceptions, version I:
[syntax="QBasic"]   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
   ELSEIF (Value = 0 AND BaseValue <> 0) THEN
      GetExponent = -1: EXIT FUNCTION
   END IF
   IF BaseValue < 1 OR Value < 0 THEN
      GetExponent = -1: EXIT FUNCTION
   END IF[/syntax]

Quote from: "Moneo"
Neo,
I'll get back to test your "lunch break work of art" as soon as I can.

Probably, my lunch break prog won't be that much of a "work of art" ... But oh well Wink
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #26 on: March 05, 2005, 11:23:37 PM »

Neo, Would you please put all your solution into only one block of code, so I can copy it in one piece to my DOS environment, compile it and test.
Thanks,
*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #27 on: March 06, 2005, 02:38:26 PM »

hehe Smiley I told what had to be replaced and what it had to replaced with... oh well

[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
   ELSEIF (Value = 0 AND BaseValue <> 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]
Logged
Moneo
Na_th_an
*****
Posts: 1971


« Reply #28 on: March 06, 2005, 11:43:20 PM »

Neo,

Your solution was incomplete, you only provided the function. I had to write the front-end of the program in order to test it. (See code below.)

However, your function is excellent. I was able to find no fault with it.

I especially liked your input checking, which you callled "Exclude some general mathematical exceptions to LOG".  With this you isolated all the weird conditions which could cause the main algorithm to fail.

Based on all this, I have to consider you the winner, so far.

Good work, Neo. Cheesy
Code:

rem Neo's Power-of solution

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
' ********************************************************************/

dim v as integer
dim b as integer

print "Enter the value";
input v
print "Enter the base";
input b

print GetExponent(v,b)

system

END

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
   ELSEIF (Value = 0 AND BaseValue <> 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

*****
Logged
Neo
Na_th_an
*****
Posts: 2150



« Reply #29 on: March 07, 2005, 05:13:50 AM »

Thanks Moneo Smiley

But to be honest...
Quote from: "Moneo"
Your solution was incomplete, you only provided the function. I had to write the front-end of the program in order to test it. (See code below.)

What's this then?
[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]
It was supplied the first time I gave you a possible solution. As you already know, I treat Front-End and Algorithms/Functions separately and thus they were supplied separately. As the Front-End didn't change, I didn't update that one and only updated the function. (Note this is based on the DRY principle - Don't Repeat Yourself, only when it's necessary I add to it Wink)
So actually I did write it, although it might have been a bit tricky to find it, apologies if it was.

Again thanks Moneo Cheesy
Logged
Pages: 1 [2] 3 4
  Print  
 
Jump to:  

Powered by MySQL Powered by PHP Powered by SMF 1.1.21 | SMF © 2015, Simple Machines Valid XHTML 1.0! Valid CSS!