Qbasicnews.com
May 26, 2020, 12:13:45 AM
 Pages: 1 2 [3] 4
 Author Topic: Algorithm to determine if a number A is a power of B.  (Read 28079 times)
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #30 on: March 11, 2005, 08:19:14 PM »

So, What's wrong with mine?
 Logged

i]"But...it was so beautifully done"[/i]
Moneo
Na_th_an

Posts: 1971

 « Reply #31 on: March 12, 2005, 10:15:01 PM »

MIT,
The following was my previous evaluation of your solution.
Maybe you would like to submit a corrected version.
*****

Quote from: "Moneo"
Quote from: "Mitth'raw'nuruodo"
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.
*****
 Logged
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #32 on: March 12, 2005, 10:55:24 PM »

Oh, ok, I didn't see that post...sorry :oops: , I guess that is what is wrong...

Well what is an algo anyways and why can't it have a loop? ::
 Logged

i]"But...it was so beautifully done"[/i]
Moneo
Na_th_an

Posts: 1971

 « Reply #33 on: March 13, 2005, 11:13:45 PM »

Mit,

Regarding the algorithm having a loop, it looks like the majority here feel that it can have a loop. So, ignore my comment, although I still have the same opinion.

In my programming experience, an algorithm is like a formula, usually one or a few lines of straight-line code to obtain a specific result. If it has a loop, or even an IF or GOTO then in my opinion this code is a routine, a function, a subroutine, or a program.

You can look on the Web for definitions of algorithms, and you will find many sample solutions to problems which are being called algorithms, but which are in fact routines or programs.
*****
 Logged
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #34 on: March 14, 2005, 12:33:23 AM »

Heh...I thought a subroutine and an algo were the same....

Tomatoe, Tomatoe  <--well If you heard me pronounce it the meaning would be more clear....How about this --> A raisin is a raisin is a raisin....or is that the opposite of my point....well I'm confused so disregard.... :lol:

Ok, so mine is out, good job neo.
 Logged

i]"But...it was so beautifully done"[/i]
DrV
Na_th_an

Posts: 1553

 « Reply #35 on: March 14, 2005, 01:00:26 AM »

erm, 'tomato'...

IMHO, an algorithm is just a set of steps which one can follow in order to achieve a desired output from a specific input which works identically each time.  It doesn't really have anything to do with computer programming specifically.  But to each his own...
 Logged
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #36 on: March 14, 2005, 01:12:00 AM »

heh...ok tomato....But you get my point right?

This is how you change data:

-------Input as data --> Algo --> Output as data
The Algo could be as simple as Y=X+1. X is input, Y is output, and Y=X+1 is the algo.

You could also have is as Find_Simple_Radical(X, A, B).
That would require loops + Ifs + Arrays. But still X is input, it does a specific task, which is to find the lowest rad, and will output the same thing for each identical input to A and B. (B Radical A).

To my understanding, If my solution is not an algo then Neo's isn't either. Log is just a function in QB. It has to do some internal calcs in order to do it.

But oh well.

Sorry but I'm feeling like this today. so don't take too seriously.
 Logged

i]"But...it was so beautifully done"[/i]
shiftLynx
Wandering Guru

Posts: 340

 « Reply #37 on: March 14, 2005, 12:59:47 PM »

A subroutine, function or procedure are all specific to programming. Algorithms are not necessarily related to programming. There is nothing that says an algorithm can only be implemented on a computer. It is perfectly fine to have a loop inside an algorithm. There aren't any strict rules saying an algorithm cannot contain a loop or anything to that effect.

Besides, a formula is a 'function' in mathematical terms; this means that your argument that a formula is an algorithm, but not a function, contradicts itself.

Loops and conditional statements are essential parts of basic logic. You can't ignore them for writing algorithms...

-shiftLynx
 Logged

img]http://www.cdsoft.co.uk/misc/shiftlynx.png[/img]
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #38 on: March 14, 2005, 05:19:10 PM »

Thank you shift, that was exactly what I was trying to say in not so many words. How do you do that? lol

f(x) = x + 1 is a funtion. y = x + 1 is an equation. They are both algos.
 Logged

i]"But...it was so beautifully done"[/i]
Moneo
Na_th_an

Posts: 1971

 « Reply #39 on: March 15, 2005, 12:34:34 AM »

Quote from: "Mitth'raw'nuruodo"
Heh...I thought a subroutine and an algo were the same....

Tomatoe, Tomatoe  <--well If you heard me pronounce it the meaning would be more clear....How about this --> A raisin is a raisin is a raisin....or is that the opposite of my point....well I'm confused so disregard.... :lol:

Ok, so mine is out, good job neo.

MIT,
I didn't say your solution was out, only that it had a minor bug. So fix the bug and we'll try it again. Also, it's not out because of the loop in the algorithm issue. The general opinion is that that's ok, so I'll have to go by that.
*****
 Logged
Dr_Davenstein
Na_th_an

Posts: 2052

 « Reply #40 on: March 15, 2005, 01:15:52 AM »

Just to clear things up...

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

 Logged
Mitth'raw'nuruodo
Ancient Guru

Posts: 515

 « Reply #41 on: March 15, 2005, 04:02:18 PM »

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

i]"But...it was so beautifully done"[/i]
Lithium
Forum Regular

Posts: 123

 « Reply #42 on: May 31, 2005, 10:22:38 AM »

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

COUNT HACKED BY RAZVEEE

RAZVEE IS A SCRIPT KIDDIE- hacker9
Quibbler
New Member

Posts: 17

 « Reply #43 on: May 31, 2005, 11:16:18 AM »

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
 Logged
Moneo
Na_th_an

Posts: 1971

 « Reply #44 on: May 31, 2005, 02:18:24 PM »

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.
*****
 Logged
 Pages: 1 2 [3] 4