0% found this document useful (0 votes)
21 views3 pages

Sathvika

Uploaded by

vedadarbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views3 pages

Sathvika

Uploaded by

vedadarbar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

PROGRAM 1:

1.1 Aim of the program :

Given a list of tuples. Write a program to find tuples that have all
elements divisible by K from a list of tuples.

Example:

Input:

n: 3

6 24 12

60 12 6

12 8 21

K: 6

Output:

[(6, 24, 12), (60, 12, 6)]

1.2 Source code :

n=int(input("n: "))
lst = [ ]
for i in range(n):
a = tuple(map(int,input().split()))
lst.append(a)
k = int(input("K: "))
selectedTupList = [tup for tup in lst if all(values % k == 0 for values
in tup)]
print(selectedTupList)
1.3 Input & Output :

Text Case -

Output :

PROGRAM 2 :

2.1 Aim of the program :

The Sieve of Eratosthenes is an algorithm used to generate all prime


numbers smaller than N. The method is to take increasingly larger prime
numbers, and mark their multiples as composite.

For example, to find all primes less than 100, we would first mark [4, 6,
8, ...] (multiples of two). then [6, 9, 12, ...] (multiples of three), and so on.
Once we have done this for all primes less than 'N', the unmarked
numbers that remain will be prime.

Implement this algorithm.

Bonus: Create a generator that produces primes indefinitely (that is,


without taking 'N' as an input).
2.2 Source code :

Primes = [0] * 500001


def SieveOfEratosthenes(n) :
Primes[0] = 1
i=3
while(i*i <= n) :
if (Primes[1 // 2] == 0) :
for j in range(3*i, n+1, 2*i) :
Primes[j // 2] = 1
i += 2
n = int(input("Enter a number: "))
SieveOfEratosthenes(n)
for i in range(1, n+1) :
if (i == 2) :
print(i, end = " ")
elif (i % 2 == 1 and Primes[i // 2]==0) :
print( i,end=" ")

2.3 Input & Output :

Text Case -

Output :

You might also like