| Author |
Comment/Response |
John
|
07/21/12 2:56pm
The purpose of this function is to determine whether or not a given number, called *num*, is a power of another number, called *base*.
###################
IsPower[num_, base_] := (
If[num == 0, Return[False]];
While[Mod[num, base] == 0, num = num / base;]
Return[num == 1];
);
##################
In pseudo-code, this function does this:
1. Checks if *num* is 0. If it is, returns false. Otherwise, continue.
2. Calculate the modulus of *num* and *base*. If it isn't 0, return whether or not *num* == 1. If the modulus is 0, divide *num* by *base* and repeat step 2.
Running this function on IsPower[9, 3] yields an error and a program that runs in an infinite loop. This is the error:
######################3
Set::setraw: Cannot assign to raw object 9. >>
Set::setraw: Cannot assign to raw object 9. >>
Set::setraw: Cannot assign to raw object 9. >>
General::stop: Further output of Set::setraw will be suppressed during this calculation. >>
##########################
What is wrong with this function?
URL: , |
|