A colleague of mine gave the following question to his C programming class which i found very interesting. It can easily be done in any programming language and immediately i thought wolfram. The question is this:
The number 25 is a unique perfect square, If we increase each digit by one, it becomes 36 which is also a a perfect square! write a program to find another set of numbers with the same qualities.
I am sure this can be easily done in mathematica. Can someone explain how i can do this in mathematica. please note the reason of the question is just an excuse to get me into mathematica programming of which i know nothing.
thanks to all.
Instead of throwing you into the ocean, lets help you paddle around in the shallow end of the pool first.
n=1;
While[n<100,
d=IntegerDigits[n];(*get the list of digits making up n*)
newd=d+1;(*add one to every element of the list giving a new list*)
newn=FromDigits[newd];(*turn that new list of digits back into a number*)
If[IntegerQ[Sqrt[newn]],Print[{n,newn}]];
n++
]
That doesn't only look at square values of n, but it might give you the hint needed about how to increment the digits and test for a square result.
There are always at least a dozen different ways of doing anything in Mathematica and some of the culture revolves around making the programs as short, and potentially cryptic, as possible. You can start picking that up later. Simplicity seems better when getting started with a new language.
I hope you have fun.