Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Algorithm to determine if a number A is a power of B.
#21
Quote:
Mitth'raw'nuruodo Wrote: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! Big Grin j/k Wink

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



Quote:For example, the bubble sort is called "bubble sort algorithm" and it contains loops
I don't like the idea of sorting without loops.... Big Grin
Reply
#22
Quote:You looked at my code! Not fair! Big Grin j/k Wink

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

Sorry! but I didn't look at your code. =)
Reply
#23
Quote: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.
*****
Reply
#24
Quote: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.
*****
Reply
#25
Formulae can use loops, sort of... reduction formulae is a classic example.

-shiftLynx
img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Reply
#26
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: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
Reply
#27
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,
*****
Reply
#28
hehe Smile 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]
Reply
#29
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. Big Grin
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
*****
Reply
#30
Thanks Moneo Smile

But to be honest...
Quote: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 Big Grin
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)