Pthon Module 4
Pthon Module 4
import math
print(" The Square root of 4.5 is", math.sqrt(4.5)) # Pass the decimal number
print(" The Square root of 627 is", math.sqrt(627)) # Pass the decimal number
print(" The Square root of 6.25 is", math.sqrt(6.25)) # Pass the decimal number
• If the number is 10, then its divisors are 1,2,5 and 10. We will ignore 1 and
consider 2 as the smallest divisor for the number.
eg- num=15
num = int(input("Enter a number : ")) i=2
while (i<=num/2)
for i in range(2, num+1):
if num % i == 0: {
print ("The smallest divisor for {} is {}".format(num, i))
if (num % i==0)
break
{
Enter a number : 13
The smallest divisor for 13 is 13 print (i)
Enter a number : 14
i+1;
The smallest divisor for 14 is 2
Unit 4 Factoring Methods / 10
}
Smallest Divisor
1. Take in an integer from the user.
2. Use a for loop where the value of i ranges from 2 to the integer.
3. If the number is divisible by i, the value of i is appended to the list.
4. The list is then sorted and the smallest element is printed.
5. Exit.
Output
The gcd of 60 and 48 is : 12
A prime number is a natural number greater than 1 that has no positive divisors
other than 1 and itself.
For example: 2, 3, 5, 7, 11
Composite number:
Other natural numbers that are not prime numbers are called composite numbers.
For example: 4, 6, 9 etc. are composite numbers.
A prime number is a natural number greater than 1 that has no positive divisors
other than 1 and itself. The first few prime numbers are {2, 3, 5, 7, 11, ….}.
# Python program to print all prime number in an interval number should be greater than 1
start = 11
end = 25
for i in range(start, end+1): Output:
if i > 1: 11
for j in range(2, i): 13
if(i % j == 0): 17
break 19
else: 23
print(i)
If the user enters the number as 12, then the output must be '2, 2, 3, and if the
input is 315; the output should be "3 3 5 7".
The program must return the prime all prime factor of given number.
n=int(input("Enter an integer:"))
print("Factors are:")
i=1
while(i<=n):
k=0 Output:
if(n%i==0): 2
j=1 2
while(j<=i): 2
if(i%j==0): 5
k=k+1 5
j=j+1
if(k==2):
print(i)
i=i+1
Unit 4 Factoring Methods / 21
Generating Pseudo-random numbers
Function What it does
randint(x, y) Generates a random integer from x to y, including the x and y.
randrange(start, stop, step) Generates a random integer in the range(start, stop, step)
random() Generates a random floating-point number in the interval [0,1)
1. Python randint()
This function generates an integer between the specified limits. It takes two
arguments x and y and produces integer i such that x <= i <= y.
Output:
4
2. Python randrange()
This function takes 2 arguments start and stop along with 1 optional argument step. And
returns a random integer in the range(start, stop, step). The default value of step is 1.
Output:
3
In this example, the output will be a random integer from [1, 3, 5, 7, 9] as the start and
stop values are 1 and 10 respectively and the step value is 2.
4. Python uniform()
This function lets you generate a floating-point number within a specific limit. It takes two
arguments to start and stop and then returns a float between the start and stop (including
the limits).
>>> import random
>>> random.uniform(6, 9)
Output:
6.338126781525701
Unit 4 Factoring Methods / 25
Generating Pseudo-random numbers
5. Python choice()
If you want to choose a random element from a specific sequence, you can use this
function. It takes one argument – the sequence. And it returns a random element from the
sequence.
>>> import random
>>> seq = (12, 33, 67, 55, 78, 90, 34, 67, 88)
>>> random.choice(seq)
Output:
67
Output:
[33, 90, 78, 88, 12]
Output:
[20, 40, 30, 50, 10]
Ref:
https://techvidvan.com/tutorials/python-random-number-generator/
Another way to exponentiate values is with the built-in pow() function (Python.org, n.d. a).
This function accepts two arguments.
The first is the base, or the number that we want to raise to a particular power. The second
is the exponent to use. pow() always calculates an exact integer power.
pow(3, 2) # Returns: 9
#list Step 2- Check if the number is less than or equal to zero or not
fib.append(fib[i-1] + fib[i-2]) Step 8- return the value of the nth term from the list
return fib[n-1] Step 9- Take input of the value n from the user