Discuss Scratch

BacktoFunk
Scratcher
2 posts

Raising a number to a power using loops

Can anyone please help me raise a number to a power using loops? I can't figure out how to loop through the answer of the first exponentiation. If that didn't make sense maybe this will:

What I want to do is multiply the first number by itself, and loop through that, the exact number of times as my second number.

Base = 2 Power = 3 Total = 8 2*2=4 base*total or (b*b*b) 4*2=8 Output = 8

How do I make it continue looping to bigger powers and stop when the base has multiplied enough times? Do I have to use:

Power Module I would have so many modules….how can I just loop the base*base to the power???
Input Power
If Power = 3
NewTotal = base*base*base
Output NewTotal
End If
End Module
BoltBait
Scratcher
1000+ posts

Raising a number to a power using loops

More like this:


set [result v] to (1)
repeat (power)
set [result v] to ((result) * (base))
end repeat

Just be aware, it will not work for negative powers (fractions) or fractional powers (roots).

Last edited by BoltBait (May 21, 2013 20:14:53)

BacktoFunk
Scratcher
2 posts

Raising a number to a power using loops

What does the first mean? This will work for any power? 5^9? Thanks for the help.
BoltBait
Scratcher
1000+ posts

Raising a number to a power using loops

BacktoFunk wrote:

What does the first mean? This will work for any power? 5^9? Thanks for the help.

Yes, it will work for 5^9. It will work for all normal, positive, integer powers.

It will not work for 5 ^ -1 which would normally evaluate to 1/5. Nor will it work for 25 ^ 0.5 which would normally evaluate to 5.

DadOfMrLog
Scratcher
1000+ posts

Raising a number to a power using loops

Note that Scratch is missing an x^y operator (is that the only reason you're working it out with a loop?)

However, you can do x^y by using the e^ and ln operators (both available in the drop-down menu of the green operator that normally shows “sqrt”).

x ^ y = e^ ( y * ln(x) )

This will work for any value of y, and any positive x (negative values of x will probably not work because ln(x) then often gives a complex number - maybe you've heard of those…? Square root of minus one, and all that? I doubt Scratch deals with complex numbers at the moment…)

Just in case maybe you don't really have to do it with a loop…
andre_rifaut
Scratcher
100+ posts

Raising a number to a power using loops

Citing (and modifying a lttle) your first post:

For: Base = 2 Power = 3 Total = 8“

Step 1: 2*2=4
Step 2: base*total or (b*b*b)
which results to 4*2=8
Output = 8

We can generalize that:
Step1 : 1*base = base –> total = base
Step 2: total * base = base*base –> total = base*base
Step 3: total * base = base*base*base –> total = base*base*base
Step 4: …

So, if you choose to use ”repeat“ be careful about the way to do it.
The statement ”set to ((base) * (base))“ i ambiguous (cfr. BoltBait).

Just modify your module as follows:

Input base
Power Module
Input Power
NewTotal = 1
Repeat (Power)
NewTotal = base*NewTotal
End repeat
Output NewTotal
End Module

If you use the formula ”e^ ( y * ln(x) )“ with integers x and y, you might have to use ”round" for seting the result an integer.

Last edited by andre_rifaut (May 21, 2013 20:02:42)

ahplayer
Scratcher
55 posts

Raising a number to a power using loops

definenumbertothepowerofpowersetoutputto1repeatpowersetoupuptooutput*number
I recently made a “New Blocks” project that includes this and much more!

Powered by DjangoBB