Objective:
To find the count of numbers in a
given range having odd number of factors.
#Solution Code:
#assignment 23
def count_factors(Number):
factor_count=0
for
i in
range(1,int(Number+1)):
if Number % i == 0:
factor_count += 1
return
factor_count
"""Function
Ended"""
"""Main program begins here"""
a = int(input("Enter the lower
range of number: "))
b = int(input("Enter the Upper range of number(this will be included):
"))
print("**************************************************************************")
number_elements = 0
for number
in range(a,b+1):
Number_Factor = count_factors(number)
if
Number_Factor % 2 == 1:
number_elements += 1
print("No. of numbers having odd number of factors are: ",number_elements)
#Ouptut
#Explaination:
Step 1:
Firstly we define a function named a count_factors() which takes a int variable as its parameter. Here Number is passed as an formal parameter.
Now we initialize a int variable factor_count which stores a value 0.
Now we run a for loop and run it from i = 1 to i = Number. (note: we pass i =Number+1 so that i = Number is included).
#FOR LOOP BODY
In for loop body we check a condition
whether Number % i == 0 (if i is a factor of Number). If this is true then we
increase the value of factor_count by 1.
At the end of for loop we return
factor_count.
Step 2:
#Drivers Code
We input a lower range value and store it in variable a and an upper range value and store it in variable b.
We then initialize a variable number_elements = 0.
Now we again run a for loop from number = a to number = b+1 (b+1 so that number = b is included).
In #For body we pass number in count_factors() as parameter and store the value returned in a variable Number_factor.
Now we check if Number_factor is odd ( if Number_factor % 2 == 1 : )
If true then we increment value of
number_elements by 1 else we continue the loop.
Step 3: End
We print the value of number_elements
obtained after completion of for loop.