[Python] How to get divisors of natural numbers at high speed

Introduction

Thank you for reading this article! !! This is ** Nakazon **, who made his programming debut in June 2020!

It is my daily routine to solve at least 5 AtCorder A-D questions.

In my article, from the perspective of a programming beginner, Learning and hardships discovered through daily routines I would like to tell you what I was happy about.

I will try to make the article learning for the same beginners! Advanced people watch with warm eyes and We look forward to your positive wisdom and advice!

Let's move on to today's content! !!

problem

Click here for the issues that led to today's learning

C - Walk on Multiplication Table https://atcoder.jp/contests/abc144/tasks/abc144_c

image.png

Considering N = 10 in the example, there are two candidates for i and j, (1, 10) and (2, 5). Consider the combination of i and j that satisfies N = i * j with the smallest i + j. Then, when I thought, "Let's get the divisor!" I fall into the state of "What? What should I do?" .. .. Lol

This is how you get the divisor of the natural number N! !!

Since N is a maximum of 10 ** 12, it is not possible to search all from 1 with a for statement. It takes too much time, so you need to devise it.

qiita.rb


N = int(input())
divisors = []
for i in range(1, int(N**0.5)+1):
    if N % i == 0:
        divisors.append(i)
        if i != N // i:
            divisors.append(N//i)

divisors.sort()

If you search to √N (the turning point of the divisor) like this The point is that you can get all the divisors.

Finally

I want to be a person who notices with such an algorithm element. It ’s amazing to notice

Recommended Posts

[Python] How to get divisors of natural numbers at high speed
How to create large files at high speed
How to get dictionary type elements of Python 2.7
How to get the number of digits in Python
How to shuffle a part of a Python list (at random.shuffle)
How to get a list of built-in exceptions in python
How to scrape at speed per second with Python Selenium
[Python] Find Fibonacci numbers at high speed (memoization, dynamic programming)
How to get the Python version
How to get started with Python
[Python] Convert natural numbers to ordinal numbers
How to speed up Python calculations
[Python] How to get the first and last days of the month
How to speed up instantiation of BeautifulSoup
[Python] How to display random numbers (random module)
How to get rid of long comprehensions
How to get a stacktrace in python
[Python2.7] Summary of how to use unittest
Summary of how to use Python list
[Python2.7] Summary of how to use subprocess
[Question] How to use plot_surface of python
[Python] How to use two types of type ()
Summary of how to import files in Python 3
Summary of how to use MNIST in Python
How to specify attributes with Mock of python
How to get the files in the [Python] folder
Speed: Add element to end of Python array
Note: How to get the last day of the month with python (added the first day of the month)
[Flask + Keras] How to infer multiple models at high speed on the server
How to get a list of files in the same directory with python
[Introduction to Python] How to get the index of data with a for statement
How to get the variable name itself in python
How to install Python
I tried to summarize how to use matplotlib of python
PostgreSQL-For those who want to INSERT at high speed
How to install python
How to write a list / dictionary type of Python3
How to convert floating point numbers to binary numbers in Python
How to use Python Kivy ① ~ Basics of Kv Language ~
How to add page numbers to PDF files (in Python)
How to get mouse wheel verdict with Python curses
[Python] Summary of how to specify the color of the figure
python, php, ruby How to convert decimal numbers to n-ary numbers
[Python] I tried to get Json of squid ring 2
Perform half-width / full-width conversion at high speed with Python
Example of how to aggregate a large amount of time series data using Python at a reasonable speed in a small memory environment
[Question] How to get data of textarea data in real time using Python web framework bottle
How to get the information of organizations, Cost Explorer of another AWS account with Lambda (python)
How to increase the processing speed of vertex position acquisition
Try to get the function list of Python> os package
[Python] How to make a list of character strings character by character
Offline real-time how to write Python implementation example of E14
How to run a Python file at a Windows 10 command prompt
[Linux] [C / C ++] Summary of how to get pid, ppid, tid
[Python] How to set variable names dynamically and speed comparison
[Python] Summary of how to use split and join functions
I tried "How to get a method decorated in Python"
How to develop in a virtual environment of Python [Memo]
Comparison of how to use higher-order functions in Python 2 and 3
How to get the last (last) value in a list in Python
How to get into the python development environment with Vagrant