usr bin env python def primes if return elif return range mroot half 2

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
#! /usr/bin/env python
def primes(n):
if n==2: return [2]
elif n<2: return []
s=range(3,n+1,2)
mroot = n ** 0.5
half=(n+1)/2-1
i=0
m=3
while m <= mroot:
if s[i]:
j=(m*m-3)/2
s[j]=0
while j<half:
s[j]=0
j+=m
i=i+1
m=2*i+3
return [2]+[x for x in s if x]
def dec2rim(n):
rim=''
d={1:'I',4:'IV',5:'V',9:'IX',10:'X',40:'XL',50:'L'}
while n!=0:
for i in [50,40,10,9,5,4,1]:
if i<=n:
n=n-i
rim+=d[i]
break
return rim
# MMMCMXCIX = 3999
answer = ''.join([dec2rim(i) for i in primes(3999)]).count('X')
print answer
>>> ================================ RESTART ================================
>>>
903