Python if Input Is Wrong Start Again

Introduction

In this article, nosotros will run across a python program to impress all the prime factors of the given number. If a number is a prime number and perfectly divides the given number and then that number is said to be a prime factor of the given number. Here, we will meet what is a prime cistron, a method to discover a prime factor, and the python program.

What is a prime number factor of a number?

Prime factors of a number are the prime number which on multiplying together gives the number. we can bank check the prime number factor of a number past two weather:

  • The number should be a prime number.
  • The number should dissever the number perfectly.
python prime factor of a number

Steps to find the prime factors of a number

  1. Allow the number be denoted by num.
  2. while num is divisible by 2, we will print 2 and divide the num by ii.
  3. Subsequently step 2, num must exist e'er odd.
  4. Start a loop from I = 3 to the square root of northward. If i split up num, print i, and divide num by i. Later on i fail to divide num, increment the i value by two and continue.
  5. If num is a prime number number and is greater than 2, then the num cannot become 1.
  6. So, print num if it is greater than 2.

Examples of Printing the Prime Factors of a Number in Python

Permit u.s. sympathise the plan for prime factors of the number in details with the help of different examples:

1. Prime Factor of a number in Python using While and for loop

In this plan, We volition be using while loop and for loop both for finding out the prime factors of the given number. we volition import the math module in this program so that we can use the square root function in python. Afterwards that, nosotros will apply the loop and attempt to discover the prime factors of the given number.

# python programme to print prime factors  import math def primefactors(n):    #fifty-fifty number divisible    while north % 2 == 0:       print (two),       north = n / 2        #n became odd    for i in range(3,int(math.sqrt(due north))+one,2):            while (n % i == 0):          print (i)          n = northward / i        if due north > 2:       print (north)  north = int(input("Enter the number for calculating the prime factors :\due north")) primefactors(north)          

Output:

          Enter the number for calculating the prime factors : 650 2 5 five thirteen        

Explanation:

Here firstly we have imported the math library from the python module. Secondly, nosotros have taken the input n every bit the number and called the function primefactors(). Thirdly, we accept taken primefactors() as the part and applied while loop and checked if the modulo of the number is coming 0 by dividing it by ii. Fourthly, the while loop volition be executed till the number is even and divisible by ii and printing it and split up the number by 2 at each iteration. After that, we will apply the for loop from 'i=three' till the square root of north+one. Then over again nosotros will employ while loop inside the for loop and check the status. At last, if north is greater than 2 then we take printed the number. Hence, all the prime factors of the number get printed.

ii. using for loop only

In this plan, We will be using for loop only for finding out the prime factors of the given number. we volition be applying multiple for loops and effort to observe the prime factors of the given number.

#using for loop  n = int(input("Enter the number for calculating the prime factors :\north")) for i in range(2,n + one):     if n % i == 0:         count = ane         for j in range(two,(i//2 + i)):             if(i % j == 0):                 count = 0                 interruption         if(count == ane):             impress(i)          

Output:

          Enter the number for calculating the prime factors : 350 ii 5 7        

Explanation:

In this instance, we will be using for loop only. Firstly, we accept taken the input from the user as northward. Secondly, we have applied the for loop from i=2 to i=n+1. Then, we will check if the modulo of the value of i and number is equal to 0. And then we keep the count value = 1 and again apply the for loop inside the for loop from j=2 to j=i//two+i. and check the given if status. if the condition satisfies count value is ready to 0 and we break the statement. We come out of the for loop and bank check the condition if count ==1 and print the value of i. hence the primefactors are printed with the single-single value of them.

          Notation:  Suppose nosotros take input as 650 : the prime factors are 2,five,five,thirteen then, it will impress two,5,thirteen as all integers one time.                  

3. Prime Factor Of A Number In Python using while loop only

In this program, We volition be using a while loop only for finding out the prime factors of the given number. nosotros will be applying multiple while loops and endeavor to detect the prime factors of the given number.

#using while loop  n = int(input("Enter any Number for calculating the prime number factors: ")) i = i  while(i <= northward):     c = 0     if(north % i == 0):         j = i         while(j <= i):             if(i % j == 0):                 c = c + ane             j = j + i                      if (c == 2):             print(i)     i = i + 1          

Output:

          Enter any Number for computing the prime factors: 350 ii 5 7        

Explanation:

In this example, we volition be using a while loop only. Firstly, we have taken the input from the user as northward. Secondly, we will set i value equally 1. Thirdly, we volition use a while loop with checking status as i should be less than or equal to n. Inside the loop, we will set the c value equally 0 and apply the if and while the condition in it. At last, we will be checking if the value of c becomes equal to 2 and then nosotros impress the value of i. hence, the prime factors are printed with the single-single value of them.

          NOTE:  Suppose we accept input as 650 : the prime factors are 2,5,5,13 then, it volition print two,5,13 as all integers once.        

Decision

In this tutorial, we have seen how to observe the prime number factors of the number with 3 different methods. All the methods are explained in detail with the help of examples and their caption. You can use any method which you like according to your demand.

huntaccur1975.blogspot.com

Source: https://www.pythonpool.com/prime-factorization-python/

0 Response to "Python if Input Is Wrong Start Again"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel