# Enigmatic Code

Programming Enigma Puzzles

## Enigma 262: Think of a number

From New Scientist #1409, 10th May 1984 [link]

I thought of an integer, added 1 and multiplied the total by the number I’d first thought of. I added 1 and multiplied the total by the number I’d first thought of. I added 1 and multiplied the total by the number I’d first thought of. Then I added 1. The final total was a perfect square. What is more, if I told you what that square was, then you’d be able to deduce the number I first thought of.

What was the number I first thought of?

Enigma 52 and Enigma 147 are also called “Think of a number”.

[enigma262]

### 2 responses to “Enigma 262: Think of a number”

1. Jim Randell 2 March 2015 at 8:11 am

The hard part of this problem is the analysis to limit the number of integers we need to consider:

The procedure followed, starting with integer n is:

f(n) = (((n + 1) × n + 1) × n + 1) × n + 1 = n^4 + n^3 + n^2 + n + 1

Now consider:

16 f(n) = 16n^4 + 16n^3 + 16n^2 + 16n + 16 = (4n^2 + 2n + 1)^2 + (4n^2 + 12n + 15)

writing p = 4n^2 + 2n + 1:

16 f(n) = p^2 + (4n^2 + 12n + 15)

so:

16 f(n) > p^2

also:

16 f(n) = 16n^4 + 16n^3 + 16n^2 + 16n + 16 = (4n^2 + 2n + 2)^2 – (4n^2 – 8n – 12)

16 f(n) = (p + 1)^2 – 4(n + 1)(n – 3)

16 f(n) < (p + 1)^2 when n > 3 or n < -1

So for n < -1 or n > 3 we have:

p^2 < 16 f(n) < (p + 1)^2

i.e. 16 f(n) lies between 2 consecutive squares, so it cannot be a perfect square.

Now suppose f(n) = q^2 for some integer q, then:

16 f(n) = 16(q^2) = (4q)^2

But 16 f(n) cannot be a perfect square, hence neither can f(n).

So we only need to explore values for n = -1, 0, 1, 2, 3, which we can do by hand or by program:

```from collections import defaultdict
from enigma import irange, is_square, printf

# record starting numbers by the final square
r = defaultdict(list)

# we only need to consider -1, 0, 1, 2, 3
for n in irange(-1, 3):
s = (((n + 1) * n + 1) * n + 1) * n + 1
k = is_square(s)
if k is not None:
printf("[f({n}) = {s} = {k}^2]")
r[k].append(n)

# find non-ambiguous solutions
for (k, v) in r.items():
if len(v) == 1:
printf("f({v[0]}) = {k}^2")
```

Solution: The number first thought of was 3.

2. Naim Uygun 2 March 2015 at 9:01 am
```def is_square(n):
h = n & 0xF
if (h > 9) :
return False
if h in [0,1,4,9]:
t =int(n**0.5 )
return (t*t == n)
return False
"""
1)Let  n  be  the thought number
2) n^2+n
3) n^3+n^2+n
4) n^4+n^3+n^2+n
5) n^4+n^3+n^2+n+1 is a square
Output: Thought number= 3 Square number= 121
"""
for n in range(1,10):
s=n**4+n**3+n**2+n+1
if is_square(s)==True:
print("Thought number=",n,"Square number=",s)
```

This site uses Akismet to reduce spam. Learn how your comment data is processed.