PDA

View Full Version : Exponential Loop Python Script



yuvlevental
August 9th, 2007, 02:57 AM
a = 1
while 1 == 1:
a = a * 2
print a

Pretty Sweet
Its infinite

Nekiruhs
August 9th, 2007, 03:00 AM
This isn't exponential though. In order for it to be exponential you'd have to do
a = a ** 2 Instead of
a = a * 2

xtacocorex
August 9th, 2007, 03:00 AM
That's an infinite loop.

yuvlevental
August 9th, 2007, 03:01 AM
exponential as in exponential growth

Nekiruhs
August 9th, 2007, 03:03 AM
exponential as in exponential growth
But its not exponential. Theres no exponenets going on. 6 times 2 is not exponential. 6 squared is exponential.

yuvlevental
August 9th, 2007, 03:31 AM
Wikipedia's (http://en.wikipedia.org/wiki/Exponential_growth) definition agrees with the script.

hod139
August 9th, 2007, 03:37 AM
At each iteration of the loop, a doubles. The variable a has an exponential growth. However, the loop is infinite, it will never terminate.

RussianVodka
August 9th, 2007, 04:16 AM
Wikipedia's (http://en.wikipedia.org/wiki/Exponential_growth) definition agrees with the script.

According to wikipedia, for your loop to have exponential growth it would have to do this:



a = #number
b = #number

while true:
a = b ** a
print a

Mathiasdm
August 9th, 2007, 08:16 AM
Ugh, exponential is too slow! Try this (http://en.wikipedia.org/wiki/Ackermann_function#Implementations):

(Source: wikipedia)


def ackermann(m, n):
if m == 0:
return n+1
elif m > 0 and n == 0:
return ackermann(m-1, 1)
elif m > 0 and n > 0:
return ackermann(m-1, ackermann(m, n-1))

Give 'ackermann(4,1)' a try ;-)

slimdog360
August 9th, 2007, 08:27 AM
just do the infinite taylor series

popch
August 9th, 2007, 09:36 AM
a = 1
while 1 == 1:
a = a * 2
print a

Pretty Sweet
Its infinite

It's not infinite, unless (a) Python has infinite precision (or rather infinite magnitude) arithmetic and (b) you have an infinite amount of RAM. If not, the program will simply crash after a while.

A bit closer to infinity will be:

a = 1
while a == a:
print a

This will keep on running as long as you can keep the pc alive. That could be a very long time if you are using a VM.

popch
August 9th, 2007, 10:09 AM
But its not exponential. Theres no exponenets going on. 6 times 2 is not exponential. 6 squared is exponential.

After doing the loop n times, the value of a will be 1 * (2 ** n), thats '2 to the n-th power. That's exponential.

proalan
August 9th, 2007, 10:25 AM
err most programmers try to avoid infinite loops, especially exponential ones.

But since the original script wasn't exponential, and we don't know what exponential the op intended it could mean anything
2 to the power of n
n to the power of n
n to the power of 2
...