I tried the python version of "Consideration of Conner Davis's answer" Printing numbers from 1 to 100 without using loops, recursion, and goto "

I was looking at the Qiita trend page in my usual routine, and I saw an article like this!

Considering Conner Davis's answer, "Print numbers from 1 to 100 without looping, recursion, or goto" https://qiita.com/xtetsuji/items/19d07c629852876da401

No no no

1000\div999^{2}
= 0.001002003004005...099100

Isn't it too interesting and amazing to be able to get a number from 1 to 100 with this?

It is reproduced in perl and ruby in the article, but it seems that it is not reproduced in python that I usually use, so I tried it.

make_num.py


from decimal import *
import re
getcontext().prec = 298

data = Decimal(1000) / Decimal(999**2) #Calculation part
data = (str(data).split(".")[1])

print(re.findall(r'...', data)) #Split+Output part

@shiracamus @ Louisa0616 The calculation part and the division part have been changed according to the two people's suggestions. ↓ Before change data = Decimal("1000")/Decimal(str(999**2)) print(re.split('(...)',data)[1::2])

In normal python division, the result is float and it is not possible to output up to 300 digits. Therefore, the decimal library is used to output up to the specified number of digits. Also, since the calculation results are stuck together like 0.001002003 ...., I use a regular expression to separate them.

The output looks like this:

$ python make_num.py
['001', '002', '003', '004', '005', '006', '007', '008', '009', '010',
 '011', '012', '013', '014', '015', '016', '017', '018', '019', '020',
 '021', '022', '023', '024', '025', '026', '027', '028', '029', '030',
 '031', '032', '033', '034', '035', '036', '037', '038', '039', '040',
 '041', '042', '043', '044', '045', '046', '047', '048', '049', '050',
 '051', '052', '053', '054', '055', '056', '057', '058', '059', '060',
 '061', '062', '063', '064', '065', '066', '067', '068', '069', '070',
 '071', '072', '073', '074', '075', '076', '077', '078', '079', '080',
 '081', '082', '083', '084', '085', '086', '087', '088', '089', '090',
 '091', '092', '093', '094', '095', '096', '097', '098', '099', '100']

I can't predict where it will be used in the future, but the idea of being able to display numbers from 1 to 100 without using a for statement was interesting.

References

  • https://techacademy.jp/magazine/19130
  • https://kaworu.jpn.org/python/Python%E3%81%A7%E6%96%87%E5%AD%97%E5%88%97%E3%82%922%E6%96%87%E5%AD%97%E3%81%9A%E3%81%A4%E5%88%86%E5%89%B2%E3%81%99%E3%82%8B

Recommended Posts

I tried the python version of "Consideration of Conner Davis's answer" Printing numbers from 1 to 100 without using loops, recursion, and goto "
I tried to deliver mail from Node.js and Python using the mail delivery service (SendGrid) of IBM Cloud!
I tried to extract and illustrate the stage of the story using COTOHA
I tried to verify and analyze the acceleration of Python by Cython
I tried to notify the update of "Hamelin" using "Beautiful Soup" and "IFTTT"
[Python] I tried to judge the member image of the idol group using Keras
I tried to summarize the string operations of Python
I tried to notify the update of "Become a novelist" using "IFTTT" and "Become a novelist API"
Python practice 100 knocks I tried to visualize the decision tree of Chapter 5 using graphviz
I tried to automate the article update of Livedoor blog with Python and selenium.
I tried to compare the processing speed with dplyr of R and pandas of Python
I tried to find the entropy of the image with python
I tried using the Python library from Ruby with PyCall
I tried to get the number of days of the month holidays (Saturdays, Sundays, and holidays) with python
I tried face recognition from the video (OpenCV: python version)
[Python] I tried to visualize the follow relationship of Twitter
I want to know the features of Python and pip
[Python] I tried collecting data using the API of wikipedia
I tried to enumerate the differences between java and python
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
Implementation of recommendation system ~ I tried to find the similarity from the outline of the movie using TF-IDF ~
Introduction to AI creation with Python! Part 1 I tried to classify and predict what the numbers are from the handwritten number images.
I became horror when I tried to detect the features of anime faces using PCA and NMF.
I tried to predict the up and down of the closing price of Gurunavi's stock price using TensorFlow (progress)
I tried to make a regular expression of "amount" using Python
How to get followers and followers from python using the Mastodon API
[Introduction to Python] I compared the naming conventions of C # and Python.
I tried to make a regular expression of "time" using Python
I tried to create a list of prime numbers with python
I tried to make a regular expression of "date" using Python
I tried to display the video playback time (OpenCV: Python version)
I tried to improve the efficiency of daily work with Python
[Python] I tried to reproduce the emergency escape program to return from the world to return from the modified world of "The disappearance of Haruhi Suzumiya"
A super introduction to Django by Python beginners! Part 2 I tried using the convenient functions of the template
I tried to get and analyze the statistical data of the new corona with Python: Data of Johns Hopkins University
Python programming: I tried to get company information (crawling) from Yahoo Finance in the US using BeautifulSoup4
I tried to transform the face image using sparse_image_warp of TensorFlow Addons
I tried to execute SQL from the local environment using Looker SDK
I tried to get the batting results of Hachinai using image processing
I tried to visualize the age group and rate distribution of Atcoder
I tried to estimate the similarity of the question intent using gensim's Doc2Vec
I tried to solve the 2020 version of 100 language processing [Chapter 3: Regular expressions 25-29]
I tried to get the authentication code of Qiita API with Python.
(Python) I tried to analyze 1 million hands ~ I tried to estimate the number of AA ~
I tried to learn the angle from sin and cos with chainer
I tried to streamline the standard role of new employees with Python
I tried to get the movie information of TMDb API with Python
I tried the common story of using Deep Learning to predict the Nikkei 225
Using COTOHA, I tried to follow the emotional course of Run, Melos!
I tried to create a sample to access Salesforce using Python and Bottle
I tried to analyze the New Year's card by myself using python
From Python to using MeCab (and CaboCha)
I tried using UnityCloudBuild API from Python
I read "Quantum Computer Accelerates Artificial Intelligence" and tried to outline it in Python without using mathematical formulas.
I tried to understand the learning function of neural networks carefully without using a machine learning library (first half).
I tried to predict the number of people infected with coronavirus in consideration of the effect of refraining from going out
I tried to predict the deterioration of the lithium ion battery using the Qore SDK
I tried to solve the 2020 version of 100 language processing knocks [Chapter 3: Regular expressions 20 to 24]
Try to find the probability that it is a multiple of 3 and not a multiple of 5 when one is removed from a card with natural numbers 1 to 100 using Ruby and Python.
[Python] Deep Learning: I tried to implement deep learning (DBN, SDA) without using a library.
I tried using the Python library "pykakasi" that can convert kanji to romaji.