2010年2月22日月曜日

2010年2月21日日曜日

リストを操作して合計を求める。


L = [1,2,3,4,5]

result = 0
for e in L:
    result += e
print result

(別解)
print reduce(lambda a,b: a+b, L)

project euler promblem5 python


2520 is the smallest number that can be divided by each of the numbers from 1 to 10 with What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

total = 0
for a in range(200000000,300000000):
    if a%3 == 0 and a%5 == 0 and a%4 == 0 and a%5 == 0 and a%6 == 0 and a%7 == 0 and a%8 == 0 and a%9 == 0 and a%10 == 0 and a%11 == 0 and a%12 ==0 and a%13 == 0 and a%14 == 0 and a%15 == 0 and a%16 == 0 and a%17 == 0 and a%18 == 0 and a% 19 == 0 and a%20 == 0:
        print a
    else :
        pass
~                   



eureler problem 4 in python

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91  99.

Find the largest palindrome made from the product of two 3-digit numbers.


for a in range(100,999):
 for b in range(100,999):
  num = a*b
  num = str(num)
  if num == num[::-1]:
   print num
  else:
   pass  

学んだこと


1.文字列の反転には
["反転したい文字"::-1]
を用いる。

2.スライスは文字列のみ。

3.for 文は繰り返しを意味する。




2010年2月20日土曜日

欲望が漏れるんだ。早く止めないと。

今日路上詩人に字を書いてもらったんだけど本当に感動した。字とかもすごいんだけどさ、その人の生き方に。

僕も勉強頑張ろう!って思った。

best effort!

書道か。
素晴らしいと思うよ。
でも、僕は明日から絵の練習をする。万年筆ですっばらしい絵が描けるように頑張るよ。
万年筆で絵を描くほうがインクを変えるだけでボールペンの替え芯を買うよりも安上がりだからね。

勉強の分野は広げるのがだいすきなんだ。一日は24時間もあるんだからね。多趣味でいいんだよ。

考えの爆発

自分が路上に迷った時にどうやって生きていくのか考えてみる。

数学をしながら旅をするポールエルディシュや、詩を全国の商店街で販売する路上詩人のような生き方を自分の理想とする僕にとって、僕はどっちかのジャンルにある程度、傾倒しなければならない。

あんまり得意じゃないけど数学に力を入れてみたい。数学ばっかりやりたい。僕は神童だったわけじゃないし、特別に今も頭がいい感じはしない。でも数学と心中したい。

今までこんな気持ちになったことはないのに、どうしてこんな気持ちになったのかって?

それは最近いろんな人と出会ってて、その人たちが型にはまらない生き方を実に楽しそうにやってるんだよね。

笑顔ふぇちの路上詩人や旅人、刑務所上がりの人などみんな僕に会う人は多種多様な、あっ、うらやましいなと思える生き方をしてて、就活がいかにくっだらないかってみんなが教えてくれるようだ。本当に頑張る人はそんなものに頼らずに自分の力で自分の未来を切り開くんだってね。

だから僕も自分の中に芯を一本通して生きていきたい。

2010年2月19日金曜日

problem 6 in python


Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.


自分の解答

sum1 = 0
sum2 = 0
for x in range(1,101):
sum1 += x*x
for x in range(1,101):
sum2 += x
sum3 = sum2 * sum2
print sum1,sum3
print sum3 -sum1


コードがひどすぎる。もっと上手にかきたいんだけど、思いつかない。付け焼刃みたい。

模範解答

total1=0
total2=0
for i in range(1, 101):
total1 =total1+(i**2)
total2 = total2+i
print (total2**2)-total1

2010年2月18日木曜日

problem2 in python


Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.



自分の解答


def fib(n):
list = range(3)
if n ==1 or n == 2 or n == 3:
return n
for i in range(n):
if not(i == 0 or n == 1 or i == 1):
if(list[i]+list[i-1]) >4000000:
break
list.append(list[i]+list[i-1])
list.remove(0)
return list
sum = 0
ans = fib(400000)
for j in ans:
if j %2 == 0:
print j
sum +=j
print sum

Python でリスト内の足し算を行う

リストが1000までだとすると、


a = range(1000)
sumA = 0
for A in a:
sumA = A++
#499500

problem1 in python


10未満の自然数のうち、3 もしくは 5 の倍数になっているものは 3, 5, 6, 9 の4つがあり、 これらの合計は 23 になる。
同じようにして、1,000 未満の 3 か 5 の倍数になっている数字の合計を求めよ。



a = range(0,1000,3)
b = range(0,1000,5)
c = range(0,1000,15)
sum3 = 0
sum5 = 0
sum15 = 0
for elem3 in a:
sum3 += elem3

for elem5 in b:
sum5 += elem5

for elem15 in c:
sum15 += elem15

print sum3 + sum5 - sum15





sum = 0
for i in range(1,1000):
if i % 3 ==0 or i % 5 == 0:
sum += i
print sum