Skip to content

Instantly share code, notes, and snippets.

@christianp
Created June 15, 2016 11:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save christianp/a35438e16079c46575591d13a15c2a45 to your computer and use it in GitHub Desktop.
Save christianp/a35438e16079c46575591d13a15c2a45 to your computer and use it in GitHub Desktop.
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": false
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"101^2 = 10201\n",
"101^3 = 1030301\n",
"101^4 = 104060401\n",
"202^2 = 40804\n",
"212^2 = 44944\n",
"121^2 = 14641\n",
"1001^2 = 1002001\n",
"1001^3 = 1003003001\n",
"1001^4 = 1004006004001\n",
"2002^2 = 4008004\n",
"10001^2 = 100020001\n",
"10001^3 = 1000300030001\n",
"10001^4 = 10004000600040001\n",
"20002^2 = 400080004\n",
"11011^2 = 121242121\n",
"11011^3 = 1334996994331\n",
"10101^2 = 102030201\n",
"10101^3 = 1030607060301\n",
"20102^2 = 404090404\n",
"10201^2 = 104060401\n",
"11211^2 = 125686521\n"
]
}
],
"source": [
"def palindromes():\n",
" ps = [[]]+[[x] for x in range(0,10)]\n",
" for p in ps:\n",
" yield pint(p)\n",
" while True:\n",
" nps = []\n",
" for p in ps:\n",
" for i in range(0,10):\n",
" np = [i]+p+[i]\n",
" if i>0:\n",
" yield pint(np)\n",
" nps.append(np)\n",
" ps = nps\n",
"\n",
"def pint(p):\n",
" t = 0\n",
" for x in p:\n",
" t = 10*t + x\n",
" return t\n",
" \n",
"def is_palindrome(s):\n",
" return s[::-1]==s\n",
" \n",
"def is_repdigit(s):\n",
" return len(set(s))==1\n",
" \n",
"for p,_ in zip(palindromes(),range(1000)):\n",
" if p>10 and not is_repdigit(str(p)):\n",
" for i in range(2,10):\n",
" q = p**i\n",
" if is_palindrome(str(q)):\n",
" print('{}^{} = {}'.format(p,i,q))"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment