SHARE
TWEET

Untitled

a guest Dec 13th, 2018 64 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. '''
  2. Group Members
  3. =============
  4. '''
  5.  
  6. userids = ['Thormundur15','Ernir17'] # fill in this array with strings of usernames
  7. def m1p1(n):
  8.     '''Project Euler Problem 1
  9. Given a positive integer n calculate the sum of all multiples of 3 and 5 less than n.
  10.     '''
  11.     multiple_of_3 = [3*i for i in range(1,n) if 3*i < n]
  12.     multiple_of_5 = [5*i for i in range(1,n) if 5*i < n]
  13.     multiple_of_15 = [15*i for i in range(1,n) if 15*i < n]
  14.     return (sum(multiple_of_3 + multiple_of_5) - sum(multiple_of_15))
  15.  
  16. def m1p2(n):
  17.     '''Project Euler Problem 2.
  18. Given a positive integer n find the sum of the even valued Fibonacci numbers less than n.
  19.     '''
  20.     if (n < 2) :
  21.         return 0
  22.     num1 = 0
  23.     num2 = 2
  24.     sum = num1 + num2
  25.     while ((4 * num2 + num1) <= n) :
  26.         num3 = 4 * num2 + num1
  27.         num1 = num2
  28.         num2 = num3
  29.         sum = sum + num2
  30.        
  31.     return sum
  32.  
  33. def m1p3(n):
  34.     '''Project Euler Problem 3.
  35. Given an positive integer n find the largest prime factor of n
  36.     '''
  37.     max = -1
  38.     while(n%2==0):
  39.         max = 2
  40.         n /= 2
  41.     for i in range(3, int(math.sqrt(n)) + 1, 2):
  42.         while(n%i==0):
  43.             max = i
  44.             n /= i
  45.     if(n > 2):
  46.         max = n
  47.     return int(max)
  48.  
  49. def m1p4(n):
  50.     '''Project Euler Problem 4.
  51. Given a positive integer n. Find the largest palindrome made from the product of two n digit numbers
  52.     '''
  53.     plaindormes = []
  54.     plaindormes.append(9)
  55.     for i in range(10**(n-1), (10**n)):
  56.         for j in range(10**(n-1), (10**n)):
  57.             num = str(i * j)
  58.             if len(num) == n*2:
  59.                 for k in range(0, n):
  60.                     if(num[k] != num[len(num)-1-k]):
  61.                         break
  62.                 else:
  63.                     plaindormes.append(i*j)
  64.     return max(plaindormes)
  65.  
  66. def m1p5(n):
  67.     '''Project Euler Problem 5.
  68. Given a positive integer n. Find the smallest positive number evenly divisible by all numbers from 1 to n
  69.     '''
  70.     i = n
  71.     n = n-1
  72.     while (n != 0) :
  73.         i = n * i / gcd(n, i)
  74.         n = n - 1
  75.     return i
  76.  
  77. def m1p6(n):
  78.     '''Project Euler Problem 6.
  79. Given a positive integer n. Find the difference between the square of the sum and the sum of the squares of the
  80. first n natural numbers
  81.     '''
  82.     i = 0
  83.     p = 0
  84.     for x in range(1, n+1):
  85.         i = i + x
  86.         p = p + x**2
  87.     i = i**2
  88.     return i - p
  89.  
  90. def m1p7(n):
  91.     '''Project Euler Problem 7.
  92. Given a positive integer n. Find the nth Prime.
  93.     '''
  94.     P = Primes()
  95.     return P.unrank(n-1)
  96.  
  97. def m1p8(n,k):
  98.     '''Project Euler Problem 8.
  99. Given positive integers n and k. Find the greatest product of k adjacent digits in n.
  100.     '''
  101.     Biggest = 0
  102.     numStr = str(n)
  103.     for i in range(0, len(numStr)-k):
  104.         cnt = 0
  105.         val = int(numStr[i])
  106.         for j in range(0, k-1):
  107.             val = val * int(numStr[i+j+1])
  108.         if(val > Biggest):
  109.             Biggest = val
  110.     return Biggest
  111.  
  112. def m1p9(n):
  113.     '''Project Euler Problem 9.
  114. Given a positive integer n. Find a Pythagorean triple such that a+b+c=n
  115.     '''
  116.     for i in range(1, n/3 + 1):
  117.         for j in range(i+1, n/2 + 1):
  118.             c = n - i - j
  119.             if(i*i + j*j == c*c):
  120.                 return (i,j,c)
  121.  
  122. def m1p10(n):
  123.     '''Project Euler Problem 10.
  124. Given a positive integer n. Find the sum of all primes less than n.
  125.     '''
  126.     P = Primes()
  127.     i = P.first()
  128.     nSum = 0
  129.     while (i < n):
  130.         nSum = nSum + i
  131.         i = P.next(i)
  132.     return nSum
  133.  
  134. def m1p11(M,k):
  135.     '''Project Euler Problem 11.
  136. Given a matrix m (as a list of lists) and integer k. Find the greatest product of k vertical, horizontal, or diagonal entries in m.
  137.     '''
  138.    
  139.     greatest = 0
  140.     for i in range(0, len(M)):
  141.         for j in range(0, len(M)):
  142.             val1 = 1
  143.             val2 = 1
  144.             val3 = 1
  145.             val4 = 1
  146.             for p in range(0, k):
  147.                 if(j<len(M)-k+1):
  148.                     val1 = val1 * M[i][j+p]
  149.                 if(i<len(M)-k+1):
  150.                     val2 = val2 * M[i+p][j]
  151.                 if(i<len(M)-k+1 and j<len(M)-k+1):
  152.                     val3 = val3 * M[i+p][j+p]
  153.                 if(i<len(M)-k+1 and j>=k-1):
  154.                     val4 = val4 * M[i+p][j-p]
  155.                    
  156.             val = max(val1, val2, val3, val4)
  157.             if(val > greatest):
  158.                 greatest = val
  159.     return greatest
  160.  
  161. def m1p12(n):
  162.     '''Project Euler Problem 12.
  163. Given an integer n. Find the smallest triangular number with more than n divisors.
  164.     '''
  165.    
  166.     def divisors(Num):
  167.         limit = int(sqrt(Num))
  168.         NumDiv = 0
  169.         for i in range(1, limit):
  170.             if Num % i == 0:
  171.                 limit = Num / i
  172.                 if limit != i:
  173.                     NumDiv += 1
  174.                 NumDiv += 1
  175.         return NumDiv
  176.  
  177.     for i in range(1, 65535):
  178.         Num = i * (i + 1) >> 1
  179.         NumDiv = divisors(Num)
  180.  
  181.         if NumDiv >= n:
  182.             return int(Num)
  183.  
  184.  
  185. def m1p13(L,k):
  186.     '''Project Euler Problem 13.
  187. Given a list L of integers and an integer k. Find the first k digits of the sum of the elements of L.
  188.     '''
  189.     ANS = sum(L)
  190.     return int(str(ANS)[:k])
  191.  
  192. def m1p14(n):
  193.     '''Project Euler Problem 14.
  194. Which starting number under n produces the longest Collatz chain.
  195.     '''
  196.     return 0
  197.     ''':/'''
  198. def m1p15(n,m):
  199.     '''Project Euler Problem 15.
  200. How many paths are there with only steps right and down through an n*m grid.
  201. If you use a mathematical formula as a shortcut in your solution, then justify
  202. why it can be used.
  203.     '''
  204.     return 0
  205. ''':/'''
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top