Skip to content

Project Euler problems 31 and 47 (python) #37

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Euler31.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/usr/bin/env python
# coding: utf-8

# In[58]:


target_balance = 200 # Calculating for 200
denominations = [1, 2, 5, 10, 20, 50, 100, 200] # Donimanations: [1, 2, 5, 10, 20, 50, 100, 200]
ways_n = [0] * (target_balance + 1) # Defining an array of size target_balance+1 (200+1) with value 0
ways_n[0] = 1 # Nos. of ways of creating 0 maybe 0 but for calculation we are taking it as 1


for i in denominations: # Running a loop of all denominations so as to calculate the number of ways each of them make a number
for j in range(i,target_balance+1): # Running a loop from 0 to 200 to update the number of ways each of them can be calculated
ways_n[j] = ways_n[j] + ways_n[j - i] # Calculating and adding number of ways in which i (current denomination) can calculate 0 to 200

# print(ways_n) Checking number of ways all numbers from 0 to 200 can be evaluated using the given denominations

print(ways_n[200]) # Printing the answer

79 changes: 79 additions & 0 deletions Euler47.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
#!/usr/bin/env python
# coding: utf-8

# In[27]:


def prime_no_collector(prime, n):
if n == 1: #checking whether n == 1, not appending if it is equal as 1 is not a prime number
return prime
for i in range(2, int(n**0.5)+1): #Running loop from 2 to sq_root(n) to check whether the number is prime or not
if n%i == 0: #checking whether the numbe ris divisble or not (if divisible then it isn't a prime)
return prime #if not prime then returning the array without appending
return prime.append(n)



# In[33]:


'''

#Testing the prime_no_collector function
prime = []
for i in range(1,20):
prime_no_collector(prime,i)
print(prime)
#Excpected Output: 2, 3, 5, 7, 11, 13, 17, 19
#Function Output: [2, 3, 5, 7, 11, 13, 17, 19]

'''


# In[34]:


def prime_factors(prime, n):
factors = []
for i in prime:
if n%i == 0:
factors.append(i)
return factors


# In[38]:


'''

#Testing function prime_factors:
print(prime_factors(prime, 15))
print(prime_factors(prime, 17))
print(prime_factors(prime, 14))

#Expected Output: [3, 5] [17] [2, 7]
#Function Output: [3, 5]
# [17]
# [2, 7]

'''


# In[45]:


def main():
prime = []
answer = 0
for i in range(1,1000000): #looping till 1,000,000
prime_no_collector(prime,i) #collecting all prime numbers as we iterate
if len(prime_factors(prime, i)) == 4 and len(prime_factors(prime, i+1)) == 4 and len(prime_factors(prime, i+2)) == 4 and len(prime_factors(prime, i+3)) == 4:
return i
print(main())


# In[ ]: