We don’t have to constrain ourselves to this arbitrary world we happen to live in. Math is general, universal— it’s not made in our image.

## MiniZinc cryptarithms improved

Previously, I posted an experiment in solving cryptarithms with Python and MiniZinc. I've just put an improved version on GitHub, since I figured out how to generalize the problem somewhat--Python now just converts your problem into a dictionary and feeds that to an existing script that doesn't have to change. This new model still has some rough edges--artifacts of the learning process--but it works well enough.

## Kaprekar Process

The *AMS Page a Day Calendar* for January 17 discusses the Kaprekar Process:

D.R. Kaprekar discovered an interesting phenomenon that occurs when one takes a four-digit number, such that all four digits are not equal, and computes the difference between its decreasing and increasing rearrangements. He found that within seven iterations of this process you will always reach the number 6174, and this process became known as the Kaprekar Process.

-- Ellis, R., & Lewis, J. (2017). Investigations into the Kaprekar Process.

Rose-Hulman Undergraduate Mathematics Journal,3(2). https://scholar.rose-hulman.edu/rhumj/vol3/iss2/4

It's easy enough to do this calculation in Python

def kaprekar(x):
i = 0
xs = []
while True:
xs.append(x)
if x in (6174,0):
return (i, xs)
i+=1
while x < 1000: x = x * 10
x = list(str(x))
x1 = int(''.join(sorted(x)))
x2 = int(''.join(sorted(x, reverse=True)))
#print(''.join(x), x1, x2)
x = max([x1, x2]) - min([x1, x2])

>>> kaprekar(2021)
(3, [2021, 2088, 8532, 6174])
>>> kaprekar(4627)
(7, [4627, 5175, 5994, 5355, 1998, 8082, 8532, 6174])

We can also verify that at most seven iterations are required to reach 6174:

>>> ks = [kaprekar(x) for x in range(1,9999)]
>>> max([cnt for cnt, hist in ks])
7

>>> from collections import Counter
>>> Counter([x[0] for x in ks]).most_common()
[(3, 2400), (7, 2184), (6, 1656), (5, 1518), (4, 1272), (2, 576), (1, 391), (0, 1)]

And finally, for the sake of having an image on this post:

>>> sns.set_theme(style="whitegrid")
>>> sns.countplot(x=[x[0] for x in ks], color=(.3, .7, .3), alpha=.8, saturation=.3)

#### About Me

Developer at Brown University Library specializing in instructional design and technology, Python-based data science, and XML-driven web development.

#### Tags

- #100daystooffload (7)
- Gemini (1)
- PowerShell (1)
- advertising (2)
- advertising_industry (1)
- aerospace (1)
- agriculture (4)
- airlines (1)
- animals (3)
- art (5)
- artificial_intelligence (6)
- automotive (2)
- aviation_ (3)
- biopharma_industry (6)
- blockchain (1)
- books (2)
- business (14)
- cannabis (1)
- chemistry (6)
- cinema (4)
- climate (5)
- code (0)
- crime (8)
- cryptarithms (1)
- data_breach (1)
- data_science (8)
- digital_transformation (1)
- diversity (9)
- economy (17)
- education (35)
- electronics (4)
- energy (1)
- entertainment (1)
- entrepreneurship (1)
- finance (2)
- finance_industry (1)
- foreign_policy (16)
- games (6)
- gardening (2)
- geography (1)
- geopolitics (5)
- global_health (3)
- gopher (1)
- guns (3)
- hacking (2)
- health (5)
- immigration (2)
- insurance (1)
- investing (9)
- law (10)
- legal_tech (3)
- libraries (2)
- lifestyle (2)
- links (41)
- machine_learning (1)
- maps (1)
- market_reports (1)
- mathematics (3)
- media_entertainment_industry (3)
- medical_devices_industry (1)
- military (4)
- minizinc (2)
- music (3)
- neuroscience (5)
- nlp (4)
- parenting (4)
- personal_finance (1)
- pharma (7)
- political_science (25)
- politics (20)
- product_review (1)
- programming (2)
- python (8)
- quotes (1)
- real_estate (2)
- reality (1)
- retail (1)
- semiconductor (1)
- smart_cities (1)
- soccer (2)
- space (2)
- stocks (3)
- sustainable_development (3)
- systems (4)
- tech (5)
- technology (1)
- video_games (2)
- visualization (2)
- weather (3)