Contact
CoCalc Logo Icon
StoreFeaturesDocsShareSupport News AboutSign UpSign In
| Download
Project: Blog
Views: 75
Author: Vincent J. Matsko
Date: 7 September 2015, Day003
Post: CrossNumber Puzzles
# Here is the code to easily create lists of numbers with certain properties. # To make some changes, save this file in one of your own projects. # Then change the numbers as you wish. # Remember, to evaluate, hold down th for i in range(10,22): print i, i^3;
10 1000 11 1331 12 1728 13 2197 14 2744 15 3375 16 4096 17 4913 18 5832 19 6859 20 8000 21 9261
for i in range(6,10): print i, i^4;
6 1296 7 2401 8 4096 9 6561
# Gets the third digit from the right using integer division and modular arithmetic. def get_third(n): return (n // 100) % 10
get_third(34)
0
get_third(123456789)
7
# Generalizes the function get_third. # The power of 10 we need comes from the digit that we want. def get_which(n, digit): return (n // 10^(digit - 1)) % 10
get_which (987654321,6)
6
# To get digits from the left, it may be helpful to know how many digits a number has. # You can use logarithms for this, or use this simple recursive function. # Assume that n is an integer (to avoid error-checking). def how_many(n): if n < 10: return 1 else: return 1 + how_many(n // 10);
how_many(3)
1
how_many(123456789)
9