Skip to content

Commit 014a8a3

Browse files
Commited
1 parent 8a0c0e4 commit 014a8a3

18 files changed

Lines changed: 516 additions & 0 deletions

10001stprime.py

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
'''
2+
Created on May 8, 2015
3+
4+
@author: Chocolate
5+
'''
6+
def SieveofEras(arr):
7+
arr[0]=True;
8+
arr[1]=True;
9+
for i in xrange(1,len(arr)):
10+
if arr[i]==False:
11+
for j in xrange(i*2,len(arr),i):
12+
arr[j]=True;
13+
return arr;
14+
15+
ll=[False]*10002
16+
ll=SieveofEras(ll);
17+
for _ in xrange(input()):
18+
n=input();
19+
idx=2;
20+
while idx<len(ll):
21+
#print idx;
22+
if(n==0):
23+
break;
24+
if(ll[idx]==False):
25+
n-=1;
26+
idx+=1;
27+
print(idx-1);

BSLArgestPalinDromProduct.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
'''
2+
Created on May 7, 2015
3+
https://www.hackerrank.com/contests/projecteuler/challenges/euler004
4+
@author: Chocolate
5+
'''
6+
def binSearch(arr,elm):
7+
lo=0;
8+
hi=len(arr)-1;
9+
#print hi;
10+
while(lo<=hi):
11+
mid=lo+((hi-lo)/2);
12+
#print mid
13+
if(arr[mid]==elm):
14+
return mid;
15+
elif(arr[mid]>elm):
16+
hi=mid-1;
17+
else:
18+
lo=mid+1;
19+
#print lo
20+
return hi;
21+
ll=[];
22+
for i in range(100,1000):
23+
for j in range(100,1000):
24+
mulnum=i*j;
25+
mulnums=str(mulnum);
26+
if(mulnums==mulnums[::-1]):
27+
ll.append(mulnum);
28+
ll=sorted(ll);
29+
#print ll;
30+
for _ in xrange(input()):
31+
n=input();
32+
print(ll[binSearch(ll, n)]);
33+
34+
35+

CoinSum.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
'''
2+
Created on May 6, 2015
3+
int target = 200;
4+
int[] coinSizes = { 1, 2, 5, 10, 20, 50, 100, 200 };
5+
int[] ways = new int[target+1];
6+
ways[0] = 1;
7+
8+
for (int i = 0; i < coinSizes.Length; i++) {
9+
for (int j = coinSizes[i]; j <= target; j++) {
10+
ways[j] += ways[j - coinSizes[i]];
11+
}
12+
}
13+
https://www.hackerrank.com/contests/projecteuler/challenges/euler031
14+
http://www.mathblog.dk/project-euler-31-combinations-english-currency-denominations/
15+
@author: Chocolate
16+
'''
17+
for _ in xrange(input()):
18+
n=input();
19+
coinSizes=[ 1, 2, 5, 10, 20, 50, 100, 200 ];
20+
ways=[0]*(n+1);
21+
ways[0]=1;
22+
for i in coinSizes:
23+
j=i;
24+
while j<=n:
25+
ways[j]+=ways[j - i];
26+
j+=1;
27+
28+
print(ways[n]%(10**9+7));
29+

Compo.py

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
'''
2+
Created on May 10, 2015
3+
4+
@authorr: Chocolate
5+
'''
6+
from math import sqrt;
7+
class MyClass(object):
8+
def __init__(self, real=0.0,imag=0.0):
9+
self.real=real;
10+
self.imag=imag;
11+
def __add__(self, x):
12+
r=self.real+x.real;
13+
i=self.imag+x.imag;
14+
return MyClass(r,i);
15+
def __sub__(self, x):
16+
r=self.real-x.real;
17+
i=self.imag-x.imag;
18+
return MyClass(r,i);
19+
def __mul__(self, x):
20+
r=(self.real*x.real)-(self.imag*x.imag);
21+
i=(self.real*x.imag)+(self.imag*x.real);
22+
return MyClass(r,i);
23+
def __div__(self, x):
24+
'''
25+
float div=(x.real*x.real) + (x.imag*x.imag);
26+
complex tmp;
27+
tmp.real=(real*c.real)+(imag*c.imag);
28+
tmp.real/=div;
29+
tmp.imag=(imag*c.real)-(real*c.imag);
30+
tmp.imag/=div;
31+
return tmp;
32+
'''
33+
d=(x.real*x.real) + (x.imag*x.imag);
34+
r=(self.real*x.real)+(self.imag*x.imag);
35+
r=r/d;
36+
i=(self.imag*x.real)-(self.real*x.imag);
37+
i=i/d;
38+
return MyClass(r,i);
39+
def modu(self):
40+
z=(self.real*self.real)+(self.imag*self.imag);
41+
return sqrt(z);
42+
def __str__(self):
43+
if(self.imag<0):
44+
#print "%.2f %.2f" % (self.real, self.imag),"i"
45+
return str("%.2f - %.2fi" % (self.real, abs(self.imag)))
46+
47+
#print "%.2f + %.2f" % (self.real, self.imag),"i"
48+
return str("%.2f + %.2fi" % (self.real, self.imag))
49+
50+
n1,n2=map(float,raw_input().split());
51+
n3,n4=map(float,raw_input().split());
52+
a=MyClass(n1+0.0,n2+0.0);
53+
b=MyClass(n3+0.0,n4+0.0);
54+
print(a+b);
55+
print(a-b);
56+
print(a*b);
57+
print(a/b);
58+
print(round(a.modu(),2));
59+
print(round(b.modu(),2));
60+
#print a+b;

Counting_Fractions.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
'''
2+
Created on May 6, 2015
3+
https://www.hackerrank.com/contests/projecteuler/challenges/euler072
4+
@author: Chocolate
5+
'''
6+
7+
def P(L):
8+
phi = range(L+1)
9+
for n in xrange(2, L+1):
10+
if phi[n] == n:
11+
for k in xrange(n, L+1, n):
12+
phi[k] -= phi[k] // n
13+
return sum(phi) - 1
14+
15+
for _ in xrange(input()):
16+
n=input();
17+
print(P(n));

EvenFibonaccinumbers.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
'''
2+
Created on May 6, 2015
3+
4+
@author: Chocolate
5+
'''
6+
import abc
7+
'''
8+
9+
When taking the following rules into account:
10+
11+
even + even = even
12+
13+
even + odd = odd
14+
15+
odd + even = odd
16+
17+
odd + odd = even
18+
19+
The parity of the first Fibonacci numbers is:
20+
21+
o o e o o e o o e ...
22+
23+
Thus basically, you simply need to do steps of three. Which is:
24+
25+
(1,1,2)
26+
(3,5,8)
27+
(13,21,34)
28+
29+
Given (a,b,c) this is (b+c,b+2*c,2*b+3*c).
30+
31+
This means we only need to store the two last numbers, and calculate given (a,b), (a+2*b,2*a+3*b).
32+
33+
Thus (1,2) -> (5,8) -> (21,34) -> ... and always return the last one.
34+
35+
This will work faster than a "filter"-approach because that uses the if-statement which reduces pipelining.
36+
37+
def checkTheMultiple(arr, num, n):
38+
# print("In checker");
39+
i = 2;
40+
a = num * i;
41+
while a <= n:
42+
# print(a);
43+
arr[a - 1] = 1;
44+
# print(arr);
45+
i = i + 1;
46+
a = num * i;
47+
48+
49+
def checkPrime(n):
50+
resultlist=[];
51+
if n >= 2:
52+
llist = [0] * n;
53+
for j in xrange(1, n, 1):
54+
if llist[j] == 0:
55+
checkTheMultiple(llist, j + 1, n);
56+
# print("Back");
57+
# print(llist);
58+
resultlist.extend([j+1]);
59+
return resultlist;
60+
'''
61+
def fibo(n):
62+
a=[0]*(n+1);
63+
a[0]=1;
64+
a[1]=2;
65+
for i in xrange(2,n+1,2):
66+
a[i]=a[i-2]+(a[i-1]*2);
67+
a[i+1]=(2*a[i-2])+(a[i-1]*3);
68+
if a[i+1]>=n:
69+
break;
70+
return a;
71+
for _ in xrange(input()):
72+
N=input();
73+
abc=[x for x in fibo(N) if x % 2 == 0];
74+
#print fibo(N);
75+
bca=[x for x in abc if x<N];
76+
#print abc;
77+
print(sum(bca));
78+
79+

LargestPrimeFactor.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'''
2+
Created on May 7, 2015
3+
https://www.hackerrank.com/contests/projecteuler/challenges/euler003
4+
@author: Chocolate
5+
'''
6+
def prime(n):
7+
i = 2
8+
#print n;
9+
while (n % i != 0 and i*i< n):
10+
i += 1
11+
#print "In While",i
12+
if (i*i < n):
13+
#print "In if",i;
14+
return prime (n / i)
15+
else:
16+
print n
17+
18+
for _ in xrange(input()):
19+
n = input()
20+
prime(n)

Multiplesof3and5.py

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
'''
2+
Created on May 6, 2015
3+
https://www.hackerrank.com/contests/projecteuler/challenges/euler001
4+
@author: Chocolate
5+
6+
'''
7+
def sum1(n,d):
8+
m = int(n/d);
9+
return (d*m*(m+1)/2);
10+
for _ in xrange(input()):
11+
n=input();
12+
print(sum1(n-1,3)+sum1(n-1,5)-sum1(n-1,15));
13+
'''
14+
def check15(num):
15+
if num%15==0:
16+
return 1;
17+
else:
18+
return 0;
19+
20+
21+
for _ in xrange(input()):
22+
n=input();
23+
a=3;
24+
b=5;
25+
i=a;
26+
j=b;
27+
sum1=0;
28+
count=2;
29+
var=1;
30+
if n<3:
31+
print(0);
32+
continue;
33+
while var==1:
34+
if (i>=n) and (j>=n):
35+
break;
36+
if(i<n):
37+
sum1+=i;
38+
i=a*count;
39+
if(j<n):
40+
sum1+=j;
41+
j=b*count;
42+
count+=1;
43+
a=15;
44+
i=a;
45+
count=2;
46+
while var==1:
47+
if i>=n:
48+
break;
49+
sum1=sum1-i;
50+
i=a*count;
51+
count+=1;
52+
print(sum1);
53+
'''
54+
'''def checkDiv(num):
55+
if (num%3==0) or (num%5==0):
56+
return 1;
57+
else:
58+
return 0;
59+
60+
61+
for _ in xrange(input()):
62+
n=input();
63+
sum1=0;
64+
for i in xrange(3,n,1):
65+
if(checkDiv(i)==1):
66+
sum1+=i;
67+
print(sum1);
68+
'''
69+
70+

RangeLCM.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
'''
2+
Created on May 7, 2015
3+
4+
@author: Chocolate
5+
'''
6+
def RangeLCM(first, last):
7+
factors = range(first, last+1)
8+
for i in range(0, len(factors)):
9+
if factors[i] != 1:
10+
n = first + i
11+
for j in range(2*n, last+1, n):
12+
factors[j-first] = factors[j-first] / factors[i]
13+
return reduce(lambda a,b: a*b, factors, 1)

RangeLCM.pyc

808 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)