CSH 4.5 MrG 2012.1126 Iterations!

2971 days ago by CSH2012

#1) find 1+3+5+7+...+2*n-1=n^2 def sumOdd(nMax): n=1 sum=0 while n<=nMax: sum+=2*n-1 n+=1 return sum sumOdd(5) 
       
25
25
#2) print any times table def timesTable(num,nMax): count=1 ans=num print "n\tnum*n" print "=\t=====" while ans<=nMax: print str(count)+"\t"+str(ans) count+=1 ans+=num timesTable(25,400) 
       
n	num*n
=	=====
1	25
2	50
3	75
4	100
5	125
6	150
7	175
8	200
9	225
10	250
11	275
12	300
13	325
14	350
15	375
16	400
n	num*n
=	=====
1	25
2	50
3	75
4	100
5	125
6	150
7	175
8	200
9	225
10	250
11	275
12	300
13	325
14	350
15	375
16	400
 
       
#3) 1*2*3*...*n=n! def fact(nMax): n=1 prod=1 while n<=nMax: prod*=n n+=1 return prod fact(6) 
       
#4) make a table: #4) n s1=sum(k,k,1,n) s2=sum(k^2,k,1,n) 3*s2/s1 def mystery(nMax): n=1 sum1=0 sum2=0 print "n\tsum1\tsum2\t3*sum2/sum1" print "=\t====\t====\t===========" while n<=nMax: sum1+=n sum2+=n^2 print str(n)+"\t"+str(sum1)+"\t"+str(sum2)+"\t"+str(3*sum2/sum1) n+=1 mystery(20) 
       
n	sum1	sum2	3*sum2/sum1
=	====	====	===========
1	1	1	3
2	3	5	5
3	6	14	7
4	10	30	9
5	15	55	11
6	21	91	13
7	28	140	15
8	36	204	17
9	45	285	19
10	55	385	21
11	66	506	23
12	78	650	25
13	91	819	27
14	105	1015	29
15	120	1240	31
16	136	1496	33
17	153	1785	35
18	171	2109	37
19	190	2470	39
20	210	2870	41
n	sum1	sum2	3*sum2/sum1
=	====	====	===========
1	1	1	3
2	3	5	5
3	6	14	7
4	10	30	9
5	15	55	11
6	21	91	13
7	28	140	15
8	36	204	17
9	45	285	19
10	55	385	21
11	66	506	23
12	78	650	25
13	91	819	27
14	105	1015	29
15	120	1240	31
16	136	1496	33
17	153	1785	35
18	171	2109	37
19	190	2470	39
20	210	2870	41
#6) find base^exp without using ** or ^ def myPow(base,exp): n=1 prod=1 while n<=exp: prod*=base n+=1 return prod myPow(150,160) 
       
149486319377230137805759483878505189467004793163723444040216561147727142\
391850235204133974416657780156711685759576899901837392894626006211372532\
526439338429469216862344183027744293212890625000000000000000000000000000\
000000000000000000000000000000000000000000000000000000000000000000000000\
0000000000000000000000000000000000000000000000000000000000000
1494863193772301378057594838785051894670047931637234440402165611477271423918502352041339744166577801567116857595768999018373928946260062113725325264393384294692168623441830277442932128906250000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000