I encourage Pythonistas into teaching Python in the context of generic
computer science and mathematics, high school and above (and below for the
precocious) to consider "indigs" an accessible example of recursion at
work.

I'm testing it out in a warm up section students might visit if feeling
shaky on their Python, or just wanting more background reading.

https://github.com/4dsolutions/clarusway_data_analysis/blob/main/python_warm_up/warmup_recursion.ipynb

The idea of an indig is very simple and inherits from numerology, a kind of
primordial NLP:

Take any positive integer, split it into individual digits, add those
digits, if the sum is a single digit you're done, otherwise: repeat.

Examples:

32986513 = 3+2+9+8+6+5+1+3 = 37 = 3+7 = 10 = 1+0 = 1

59865279171 = 5+9 = 14+8 = 22+6 = 28+5 = 33+2 = 35+7 = 42+9 = 51+1 = 52+7 =
59+1 = 60 = 6+0 = 6

I got it down to this, but it's no crime to take a few more lines.

def indig(n: int):
    return n if len(str(n))==1 else indig(sum(map(int, list(str(n)))))

It's not important that "taking the indig of a number" be considered
"meaningful" in any way.  What's useful about it is it introduces type
conversion (int to string digits, back to single ints for summing) as well
as recursion.

This idea and algorithm may well have been proposed and used before in the
literature.  Sounds Martin Gardnery.  Maybe it's in Knuth already. I'm not
trying to establish priority for anything, only circling what looks like
promising pedagogy going forward and sharing it with peers.

Kirby
_______________________________________________
Edu-sig mailing list -- edu-sig@python.org
To unsubscribe send an email to edu-sig-le...@python.org
https://mail.python.org/mailman3/lists/edu-sig.python.org/
Member address: arch...@mail-archive.com

Reply via email to