i=1
print "Enter a positive integer: "
n = int(raw_input())
s=0
while (i <= n):
s += i
i += 1
print "The sum of the first %d integers is %d" %(i,s)
print "Enter a positive integer: "
bound = int(raw_input())
s=0.0
i=0
while (s < bound):
i += 1
s += 1.0/i
print "The sum of the first %d reciprocals is %f" %(i,s)
import math
print "Enter a positive number: "
x = float(raw_input())
while (x > 0):
print "sqrt(%d) = %f "%(x,math.sqrt(x))
print "Enter another positive number (or 0 to quit): "
x = float(raw_input())
i=1
print "Enter a positive integer: ";
n = int(raw_input())
s=0
while(True):
if (i > n):
break # terminates the loop immediately
s += i
i += 1
print "The sum of the first %d integers is %d" %(n,s)
print "Enter a positive integer: "
bound = int(raw_input())
print "Fibonacci numbers < %d:\n0, 1" % bound ,
f0=0
f1=1
while (True):
f2 = f0 + f1
if (f2 > bound):
break
print ", %d" % f2,
f0 = f1
f1 = f2
import sys
print "Enter a positive integer: "
bound = int(raw_input())
print "Fibonacci numbers < %d:\n0, 1" % bound ,
f0=0
f1=1
while (True):
f2 = f0 + f1
if (f2 > bound):
sys.exit(0)
print ", %d" % f2,
f0 = f1
f1 = f2
print "Enter a positive integer: "
bound = int(raw_input())
print "Fibonacci numbers < %d:\n0, 1" % bound ,
f0=0
f1=1
# Error : infinite loop !
while (True):
f2 = f0 + f1
# By commenting the below if statement, it goes to infinite.
if (f2 > bound):
break
print ", %d" % f2,
f0 = f1
f1 = f2
i=0
print "Enter a positive integer: "
n = int(raw_input())
s=0
while i<=n:
s += i
i += 1
print "The sum of the first %d integers is %d" %(n,s)
print "Enter a positive integer: "
bound = int(raw_input())
print "Factorial numbers < %d:\n1, 1" %bound,
f=1
i=1
while f < bound:
i += 1
f *= i
print ", %d" %f,
print "Enter a positive integer: "
n = int(raw_input())
s=0;
for i in range(0,n+1):
s += i
print "The sum of the first %d integers is %d" %(n,s)
print "Enter a positive integer: "
n = int(raw_input())
s=0
for i in range(1,n/2): # the scope of this i is this loop
s += i
for i in range(n/2,n+1): # the scope of this i is this loop
s += i
print "The sum of the first %d integers is %d" % (n,s)
print "Enter a positive integer: "
bound = int(raw_input())
print "Factorial numbers that are <= %d:\n1, 1" %bound,
f=1
for i in range(2,bound+1):
f *= i
print ", %d" % f,
for i in range(10,0,-1):
print i,
prime = True
print "Enter a positive integer: "
n = int(raw_input())
if (n < 2):
print "%d is not prime." %n
prime = False
elif (n < 4):
print "%d is prime." %n
prime = False
elif (n%2 == 0):
print "%d = 2* %d" %(n,n/2)
prime = False
else:
for d in range(3,n/2+1):
if (n%d == 0):
print "%d = %d * %d" %(n,d,n/d)
prime = False
if prime:
print "%d is prime."%n
print "Enter positive integers (0 to quit): ";
n = int(raw_input())
m = n
while n > 0:
n = int(raw_input())
if n > m :
m = n
print "max = %d" % m
print "Enter positive integers (0 to quit): ";
n = int(raw_input())
m = n
while n > 0:
if n < m :
m = n
n = int(raw_input())
print "min = %d" % m
m = 95
n = 11
while m%n > 0:
print "%d modulo %d = %d" %(m,n,m%n)
m -= 3
n += 1
for x in range(1,13):
for y in range(1,13):
print "%4d" % (x*y),
print ""
import math
# defines pow() and log()
print "Enter a positive integer: "
n = int(raw_input())
d=0 # the discrete binary logarithm of n
p2d=1 # = 2^d
i = n
while i > 1:
# INVARIANT: 2^d <= n/i < 2*2^d
p2d=math.pow(2,d) # = 2^d
print "%2d <= %2d" %(p2d,2*p2d)
i /= 2
d += 1
p2d=math.pow(2,d) # = 2^d
print "%2d <= %2d < %2d" %(p2d,n,2*p2d)
print " The discrete binary logarithm of is %d" % d
lgn = math.log(n)/math.log(2) # base 2 logarithm
print "The continuous binary logarithm of is %f" % lgn
i=1
print "Enter a positive integer: "
n = int(raw_input())
s=0
while (True):
if (i > n):
break
s += i
i += 1
print "The sum of the first %d integers is %d" %(i,s)
count=0
s=0
print "Enter positive integers (0 to quit):"
while True: # "forever"
print "\t %d :" %(count + 1),
n = int(raw_input())
if (n <= 0):
break
count += 1
s += n
print "The average of those %d positive numbers is " %count,
print float(s)/count
for x in range(1,13):
for y in range(1,13):
if y>x:
break
else:
print '%4d' %(x*y),
print ''
while True:
n = int(raw_input('Enter int : '))
if (n%2 == 0):
continue
if (n%3 == 0):
break
print "\tBottom of loop.\n"
print "\tOutside of loop.\n"
N=5
done=False
for i in range(N):
for j in range(N):
if done:
break
for k in range(N):
if done:
break
if (i+j+k>N):
done = True
else:
print i+j+k,
print " ",
print "* "
print "."
done = False
N=5
done=False
for i in range(N):
for j in range(N):
if done:
break
for k in range(N):
if done:
break
if (i+j+k>N):
done = True
else:
print i+j+k,
print " ",
print "* "
print "."
done = False
import random
# prints pseudo-random numbers:
for i in range(0,8):
print random.random()
import random
# prints pseudo-random numbers:
print "Enter seed: "
seed = int(raw_input())
random.seed(seed);
for i in range(0,8):
print random.random()
import random
for i in range(0,8):
print random.random()
import random
print "Enter minimum and maximum: "
m = int(raw_input())
n = int(raw_input())
# lowest and highest numbers
r = n - m + 1
# number of numbers in range
for i in range(0,20):
j = int(random.random()*100 % r + m)
print j,
print " ",