######################################################################
## KACZYNSKI: Save this file as  KACZYNSKI. To use it,               #
## stay in the                                                       #
## same directory, get into Maple (by typing: maple <Enter> )        #
## and then type:  read KACZYNSKI: <Enter>                           #
## Then follow the instructions given there                          #
##                                                                   #
## Written by Lara Pudwell                                           #
## Rutgers University ,                                              #
## lpudwell at math dot rutgers dot edu.                             #
######################################################################

print(`This is KACZYNSKI, A Maple package, written by `):
print(`Lara Pudwell`):

print(`accompanying her  article: `):

print(` "Digit Reversal Without Apology"`):
print(` (to appear)`):
print():
print(`First Written: Sept. 26, 2005. Tested for Maple 9.5 `):
print(`Version of Nov. 13, 2005.  `):
print():

print():
print(`The most current version is available on WWW at:`):
print(` http://www.math.rutgers.edu/~lpudwell/maple/KACZYNSKI .`):
print(`Please report all bugs to: lpudwell at math dot rutgers dot edu .`):
print():
print(`For general help, and a list of functions,`):
print(` type "Help();". For specific help type "Help(procedure_name);" `):
print():

Help:=proc()
if args=NULL then

print(` KACZYNSKI: A Maple package for exploring integers and their reversals.`):
print(` `):
print(` Accompanies the paper "Digit Reversal Without Apology" by L. Pudwell`):
print(` `):
print(` The functions in the package are:`):
print(` get_sols, Lemma2, gen_data, KLemma, KLemma2`):

elif nargs=1 and args[1]=get_sols then
print("get_sols(n,h): returns all h-digit solutions of numbers in base n which are multiples of themselves when their digits are reversed"):
print("For example: try get_sols(10,4)"):

elif nargs=1 and args[1]=Lemma2 then
print("Lemma2(): rigorously proves Lemma 2 of Kaczynski's 1966 paper"):
print("For example: try Lemma2()"):

elif nargs=1 and args[1]=gen_data then
print("gen_data(n,h): returns all solutions with up to h digits in all bases from 2 to n"):
print("For example: try gen_data(5,4)"):

elif nargs=1 and args[1]=KLemma then
print("KLemma(n,f): returns all counterexamples to Question 1 up to base n, with f as chosen"):
print("Output is a list of pairs [n, {set of counterexamples}]"):
print("For example: try KLemma(22,-1) or KLemma(22,0)"):

elif nargs=1 and args[1]=KLemma2 then
print("KLemma2(n,f): returns all counterexamples to Question 1 in base n, with f as chosen"):
print("For example: try KLemma2(22,0)"):

else
 print(`There is no such thing as`, args):

fi:

end:



#given integers n and k, get_sols(n,k) returns all k-digit solutions of
#numbers in base n who are multiples of themselves when their
#digits are reversed
get_sols:=proc(n,k) local i, m, a, r, j, c, rat, S:
S:={}:
m:=sum((n-1)*n^(c),c=0..k-1):

for i from 1 to m do
r:=i:

for j from 0 to k-1 do
a[k-j]:=floor(r/(n^(k-1-j))):
r:=r-a[k-j]*n^(k-1-j):
od:

rat:=sum(a[k-c]*n^(k-1-c),c=0..k-1)/sum(a[c]*n^(k-c),c=1..k):

if type(rat, integer) and rat>1 and rat <n and a[k]>0 and a[1]>0 then
#print([seq(a[k-c],c=0..k-1)], [seq(a[c],c=1..k)]):
S:={op(S), [[seq(a[k-c],c=0..k-1)], [seq(a[c],c=1..k)]]}:
fi:

od:

S:

end:

Lemma2:=proc() local S, T:

S:=get_sols(2,3):
T:=get_sols(3,3):

if S={} and T={} then
return(true):
else
return(false):
fi:
end:

gen_data:=proc(n,k) local i, j, S, res:

for i from 2 to n do
for j from 2 to k do

res:=get_sols(i,j):
S[i][j]:=res:

print("for n = ", i, "h = ", j, "solutions are:"):
print(S[i][j]):

od:
od:

S:
end:

KLemma:=proc(n,f) local a,b,d,e,i,S:

S:=[]:

for i from 2 to n do
if type(i+1, prime) then
S:=[op(S), [i, KLemma2(i,f)]]:
#print(i, S):
fi:
od:

S:
end:

KLemma2:=proc(n,f) local a,b,d,e,S:

S:={}:

for a from 1 to n-2 do
for b from 0 to n-1 do
for d from 0 to n-1 do
for e from a+1 to n-1 do

if type((e*n^3+(d+f-e)*n^2+(b-a)*n+a)/(a*n^3+(b+f-a)*n^2+(d-e)*n+e),'integer')
and evalb(b+d >= a+e-f*(n+1)) then 

if evalb(f*(n+1)+b+d-a-e <n) then

S:={op(S), [a,b,d,e]}:

fi:

fi:

od:
od:
od:
od:

S:
end: