def test(x, y, debug=True):
num = x**y
digitalsum = sum(int(a) for a in str(num))
if x == digitalsum and debug:
print x, y, num
return x == digitalsum
for i in range(1,1000):
for j in range(1,100):
if test(i, j):
print i, j
Re:A happy numeric coincidence
by lmcarreiro on Wednesday January 02, 2019 @11:13AM
(#18807155)
After you calc X^Y, when you will calc X^(Y+1), doesn't it worth to calc using the previous result (X^Y)*X instead of another power X^(Y+1) ?
You will find here that the run-time is swamped by the digit breakdown and summing, so that exponentiation versus accumulated product doesn't make a palpable difference.
TXR lisp:
This is the TXR Lisp interactive listener of TXR 203.
Quit with :quit or Ctrl-D on empty line. Ctrl-X ? for cheatsheet.
1> (each ((x (range 2 1000))) ;; 1 is uninteresting
(each ((y (range 1 100)))
(let ((z (expt x y)))
(when (= x (sum (digits z)))
(put-line `@x @y`)))))
2 1
3 1
4 1
5 1
[...]
963 69
964 75
964 78
991 71
999 75Using successive multiplication instead of exponent operation:
6> (each ((x (range 2 1000)))
(each ((y (range 1 100)) ;; needed just for the put-line
(z [giterate true (op * x) x]))
(when (= x (sum (digits z)))
(put-line `@x @y`))))
(Makes no difference; run time is vastly dominated by the contribution of the (sum (digits z)) business.)
digitalsum(999^75) = 999