0% found this document useful (0 votes)
69 views7 pages

Day 8 - Assignment Answers

The document describes a Python program to check if a number is prime. It explains that the program uses print statements to output whether the number is prime or not. If the number is less than or equal to 1, it is not prime. Otherwise, it iterates from 2 up to the square root of the number, checking if the number is divisible by any of them. If it is divisible, it prints that it is not prime. If the loop finishes without finding a divisor, it prints that the number is prime.

Uploaded by

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

Day 8 - Assignment Answers

The document describes a Python program to check if a number is prime. It explains that the program uses print statements to output whether the number is prime or not. If the number is less than or equal to 1, it is not prime. Otherwise, it iterates from 2 up to the square root of the number, checking if the number is divisible by any of them. If it is divisible, it prints that it is not prime. If the loop finishes without finding a divisor, it prints that the number is prime.

Uploaded by

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

Day 8 of Python Assignment Answers -

22. Write a python program to to check whether a number is


a prime?

Pradeepchandra Reddy S C soopertramp07


we use print statements to output whether the number is prime or
not. If the number is less than or equal to 1, we print that it is not
prime.

Otherwise, we iterate through a range of numbers from 2 up to the


square root of the number (inclusive), checking if the number is
divisible by any of them.

If it is, we print that the number is not prime and exit the loop using
the break statement. If the loop completes without finding a
divisor, we print that the number is prime using the else clause that
is executed after the loop completes successfully.

Pradeepchandra Reddy S C soopertramp07


The reason we use the square root of num in the range of the loop is to optimize
the algorithm and reduce the number of computations needed to check if a number
is prime.

Suppose we have a number num that is not prime. Then there must be two factors
of num, say a and b, such that num = a * b. One of a and b must be less than or equal
to the square root of num, and the other must be greater than or equal to the
square root of num. Otherwise, their product would be greater than num.

For example, suppose num = 24. The factors of num are 1, 2, 3, 4, 6, 8, 12, 24. Notice
that the largest factor less than or equal to the square root of num (which is
4.89897948557...) is 4, and the smallest factor greater than or equal to the square
root of num is 6. So if we check all the factors of num up to 4, we can be sure that if
none of them divide num evenly, then num is not prime.

Therefore, we only need to check for factors of num up to the square root of num.
This reduces the number of computations required and makes the algorithm more
efficient.

Pradeepchandra Reddy S C soopertramp07


23. Write a Python program to print all prime numbers in
the interval 0f 1 - 10000?

Pradeepchandra Reddy S C soopertramp07


The print_primes function takes two optional arguments, start and end,
which specify the range of numbers to check for primes.

The function then iterates through each number in that range, checking
whether it is prime or not.

If the number is greater than 1, it checks if it is prime by dividing it by all


numbers from 2 to the square root of the number.

If the number is divisible by any of those numbers, it is not prime, and the
loop exits using the break statement.

If the loop completes without finding a divisor, the number is prime, and it
is printed to the console using the print statement.

Pradeepchandra Reddy S C soopertramp07


24. Write a Python program to find the factorial of a number

Pradeepchandra Reddy S C soopertramp07


The factorial function takes an integer n as input and returns
its factorial as an integer.

The function first checks that the input is non-negative, raising


a ValueError if it is not.

The base case is that the factorial of 0 is 1, which is returned


immediately.

For all other values of n, the function computes the factorial of


n-1 recursively and multiplies the result by n. This continues
until the base case is reached.

Pradeepchandra Reddy S C soopertramp07

You might also like