Prime factorization of integers entered in Python ver.1

Introduction

The previously created "Program to determine whether a number entered in Python is a prime number" has a function that performs prime factorization when it is a composite number. I added it.

Program principle

Recalling the method of prime factorization that I learned when I was in elementary school, I decided to continue dividing from the smallest prime number. Specifically, create a prime number list using SymPy's primerange and continue to divide by a divisible number from that list. Then, when the division was performed to the end of the prime number list, it was finished.

Actual program

from sympy import primerange
inn = n = int(input("Enter the number you want to check if it is a prime number.>> "))
primlist = list(primerange(2,(inn+1) / 2)) #1
yaku = []

for i in primlist:
    while n % i == 0: #2
        n /= i
        yaku.append(i) #3

if not yaku: #4
    print(n, "Is a prime number.")
else:
    print(inn, "Is a composite number, and when factored into prime factors",yaku, "is.")

Rough flow

  1. Create a prime number list up to (n + 1) / 2
  2. Find a divisible number in the prime number list and continue to divide the integer n until it is no longer divisible.
  3. Each time you divide, add the divided prime number to the list called yaku.
  4. If there is nothing in yaku, it is displayed as a prime number. If anything is included, the result of prime factorization is displayed.

What you are actually doing

  1. Created in # 1 using Sympy as before.
  2. In # 2, by using the while syntax, it is possible to put into the operation of continuing to divide immediately without using if.
  3. In # 3, add the prime i divided by yaku by doing .append (i).
  4. In # 4, I noticed that if yaku contains a number, it is a composite number. Specifically, if you use the if syntax and set if ** not ** yaku, when there is nothing in yaku, the processing below if will be started.

Difficulties

When I first programmed, the range of the prime number list created by primerange () was the same as last time, "primerange (2, int (n ** (1/2)) + 1)". Then, when the input integer was "2 x prime number", only 2 was added to yaku, and there was a problem that it could not be factored accurately. Specifically, when you enter a number such as 14 (2 × 7), the result is "14 is a composite number, and when factored into prime factors, it is [2]. ] Has come out. Therefore, I set the range of the prime number list to "prime range (2, (inn + 1) / 2)". By doing this, I decided to solve the problem I mentioned earlier. At this time, by adding +1 to inn, a prime number list can be created up to a prime number even with an integer of "2 x prime number". Specifically, when 14 is entered, it becomes primerange (2, 7.5), so the content of primelist becomes [2, 3, 5, 7]. If +1 is not done, it will be primerange (2, 7), so the contents of primelist will be [2, 3, 5], and 7 will not be included in the result of prime factorization, so accurate prime factorization Will not be possible.

Reflections

If the range of prime number list creation is too wide and the integers entered are large (7 digits or more), the calculation speed will drop immediately. Therefore, I would like to improve the range setting and prime factorization method a little. Thank you for reading to the end.

Recommended Posts

Prime factorization of integers entered in Python ver.1
Prime factorization ver.2 of integers input in Python
[Python 3] Prime factorization in 14 lines
Reversible scrambling of integers in Python
Project Euler # 10 "sum of prime numbers" in Python
Find the divisor of the value entered in python
Prime numbers in Python
Prime number 2 in Python
Implementation of quicksort in Python
Pixel manipulation of images in Python
Division of timedelta in Python 2.7 series
Infinite prime number generator in Python3
MySQL-automatic escape of parameters in python
Handling of JSON files in Python
Implementation of life game in Python
Waveform display of audio in Python
Law of large numbers in python
Implementation of original sorting in Python
A program that determines whether a number entered in Python is a prime number
Conversion of string <-> date (date, datetime) in Python
Project Euler # 3 "Maximum Prime Factors" in Python
Check the behavior of destructor in Python
(Bad) practice of using this in Python
Output tree structure of files in Python
Display a list of alphabets in Python 3
Comparison of Japanese conversion module in Python3
Summary of various for statements in Python
Project Euler # 7 "1000 1st prime number" in Python
The result of installing python in Anaconda
Gang of Four (GoF) Patterns in Python
The basics of running NoxPlayer in Python
Bulk replacement of strings in Python arrays
Project Euler # 16 "Sum of Powers" in Python
Traffic Safety-kun: Recognition of traffic signs in Python
Summary of built-in methods in Python list
I searched for prime numbers in python
Non-logical operator usage of or in python
In search of the fastest FizzBuzz in Python
Practical example of Hexagonal Architecture in Python
Project Euler # 17 "Number of Characters" in Python
Double pendulum equation of motion in python
Get rid of DICOM images in Python
Status of each Python processing system in 2020
Project Euler # 1 "Multiples of 3 and 5" in Python
Meaning of using DI framework in Python
Type in Python to implement algebraic extension (1) ~ Monoids, groups, rings, ring of integers ~
Output the number of CPU cores in Python
Draw a graph of a quadratic function in Python
[Python] Sort the list of pathlib.Path in natural sort
Receive websocket of kabu station ® API in Python
Summary of how to import files in Python 3
Unattended operation of Google Spreadsheets (etc.) in Python
Get the caller of a function in Python
Match the distribution of each group in Python
View the result of geometry processing in Python
Prime number enumeration and primality test in Python
Make a copy of the list in Python
Summary of how to use MNIST in Python
Rewriting elements in a loop of lists (Python)
Find prime numbers in Python as short as possible
Getting rid of DICOM images in Python Part 2