-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment6.2.py
60 lines (47 loc) · 1.76 KB
/
assignment6.2.py
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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# -*- coding: utf-8 -*-
"""
Created on Sat Jun 15 17:29:43 2019
@author: Shri
"""
#Given a number n, write a program to find the sum of the largest prime factors of each of nine consecutive numbers starting from n.
#g(n) = f(n) + f(n+1) + f(n+2) + f(n+3) + f(n+4) + f(n+5) + f(n+6) + f(n+7) + f(n+8)
#where, g(n) is the sum and f(n) is the largest prime factor of n
#For example,
#g(10)=f(10)+f(11)+f(12)+f(13)+f(14)+f(15)+f(16)+f(17)+f(18)
# =5 + 11 + 3 + 13 + 7 + 5 + 2 + 17 + 3
# =66
def find_factors(num):
#Accepts a number and returns the list of all the factors of a given number
factors = []
for i in range(2,(num+1)):
if(num%i==0):
factors.append(i)
return factors
def is_prime(num, i):
#Accepts the number num and num/2 --> i and returns True if the number is prime ,else returns False
if(i==1):
return True
elif(num%i==0):
return False;
else:
return(is_prime(num,i-1))
def find_largest_prime_factor(list_of_factors):
#Accepts the list of factors and returns the largest prime factor
d=[]
for i in list_of_factors:
if(is_prime(i,i//2) == True):
d.append(i)
return max(d)
def find_f(num):
b=find_factors(num)
c=find_largest_prime_factor(b)
#Accepts the number and returns the largest prime factor of the number
return c
def find_g(num):
sum=0
for i in range(9):
sum=sum+find_f(num + i)
return sum
#Accepts the number and returns the sum of the largest prime factors of the 9 consecutive numbers starting from the given number
#Note: Invoke function(s) from other function(s), wherever applicable.
print(find_g(10))