Mathematica 9 is now available
Student Support Forum
-----
Student Support Forum: 'Why does this function return an error?' topicStudent Support Forum > General > Archives > "Why does this function return an error?"

Next Comment >Help | Reply To Topic
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: ,

Subject (listing for 'Why does this function return an error?')
Author Date Posted
Why does this function return an error? John 07/21/12 2:56pm
Re: Why does this function return an error? Forum Modera... 07/21/12 7:08pm
Re: Why does this function return an error? John 07/21/12 7:21pm
Re: Why does this function return an error? Peter Pein 07/22/12 05:16am
Next Comment >Help | Reply To Topic