Sujet : Re: Simple math/programming challenge for the "REAL programmer" Feeb
De : none (at) *nospam* none.none (Tyrone)
Groupes : comp.os.linux.advocacyDate : 10. Mar 2024, 17:52:17
Autres entêtes
Message-ID : <09acnYe4UemsSnD4nZ2dnZfqnPidnZ2d@supernews.com>
References : 1 2 3
User-Agent : Usenapp for MacOS
On Mar 10, 2024 at 10:46:27 AM EDT, "DFS" <
nospam@dfs.com> wrote:
On 3/10/2024 3:41 AM, Yaxley Peaks wrote:
Does emacs lisp count?
;; ------------------
(defun kaprekarp (n)
(let* ((num (* n n))
(snum (number-to-string num))
(num-digits (length (number-to-string n)))
(num-list (string-split snum "" t))
(right-side (reverse (take num-digits (reverse num-list))))
(left-side (reverse (nthcdr num-digits (reverse num-list))))
(left-side-num (string-to-number (mapconcat 'identity left-side)))
(right-side-num (string-to-number (mapconcat 'identity right-side))))
(= n (+ right-side-num left-side-num))))
(defun solve-problem (low high)
(seq-keep (lambda (x)
(and (kaprekarp x) x))
(number-sequence low high)))
(solve-problem 1 100000000)
;; ------------------
A very naive solution, but I am sorry, I am very drunk right now lol
ha! Were you drunk before you started writing the Lisp code?
I submitted it here:
https://rextester.com/l/common_lisp_online_compiler
And got:
Error(s), warning(s):
*** - EVAL: undefined function SEQ-KEEP
When you sober up:
------------------------------------------------------------------------
import sys, time
k=1
start = time.time()
for i in range(4,int(sys.argv[1].replace(',', ''))):
s, n = str(i**2), len(str(i))
if len(s) % 2 != 0: n-=1
p1, p2 = int(s[0:n]), int(s[n:])
if p1 + p2 == i:
print("%4d. %7d ^ 2 = %-16s %8s + %8s = %d" % (k, i, s, p1, p2,
p1+p2))
k+=1
print("%.1fs" % (time.time() - start))
------------------------------------------------------------------------
$ python Knums.py 10,000,000
1. 9 ^ 2 = 81 8 + 1 = 9
2. 45 ^ 2 = 2025 20 + 25 = 45
3. 55 ^ 2 = 3025 30 + 25 = 55
4. 99 ^ 2 = 9801 98 + 1 = 99
5. 297 ^ 2 = 88209 88 + 209 = 297
6. 703 ^ 2 = 494209 494 + 209 = 703
7. 999 ^ 2 = 998001 998 + 1 = 999
8. 2223 ^ 2 = 4941729 494 + 1729 = 2223
9. 2728 ^ 2 = 7441984 744 + 1984 = 2728
10. 4950 ^ 2 = 24502500 2450 + 2500 = 4950
11. 5050 ^ 2 = 25502500 2550 + 2500 = 5050
12. 7272 ^ 2 = 52881984 5288 + 1984 = 7272
13. 7777 ^ 2 = 60481729 6048 + 1729 = 7777
14. 9999 ^ 2 = 99980001 9998 + 1 = 9999
15. 17344 ^ 2 = 300814336 3008 + 14336 = 17344
16. 22222 ^ 2 = 493817284 4938 + 17284 = 22222
17. 77778 ^ 2 = 6049417284 60494 + 17284 = 77778
18. 82656 ^ 2 = 6832014336 68320 + 14336 = 82656
19. 95121 ^ 2 = 9048004641 90480 + 4641 = 95121
20. 99999 ^ 2 = 9999800001 99998 + 1 = 99999
21. 142857 ^ 2 = 20408122449 20408 + 122449 = 142857
22. 148149 ^ 2 = 21948126201 21948 + 126201 = 148149
23. 181819 ^ 2 = 33058148761 33058 + 148761 = 181819
24. 187110 ^ 2 = 35010152100 35010 + 152100 = 187110
25. 208495 ^ 2 = 43470165025 43470 + 165025 = 208495
26. 318682 ^ 2 = 101558217124 101558 + 217124 = 318682
27. 329967 ^ 2 = 108878221089 108878 + 221089 = 329967
28. 351352 ^ 2 = 123448227904 123448 + 227904 = 351352
29. 356643 ^ 2 = 127194229449 127194 + 229449 = 356643
30. 390313 ^ 2 = 152344237969 152344 + 237969 = 390313
31. 461539 ^ 2 = 213018248521 213018 + 248521 = 461539
32. 466830 ^ 2 = 217930248900 217930 + 248900 = 466830
33. 499500 ^ 2 = 249500250000 249500 + 250000 = 499500
34. 500500 ^ 2 = 250500250000 250500 + 250000 = 500500
35. 533170 ^ 2 = 284270248900 284270 + 248900 = 533170
36. 538461 ^ 2 = 289940248521 289940 + 248521 = 538461
37. 609687 ^ 2 = 371718237969 371718 + 237969 = 609687
38. 643357 ^ 2 = 413908229449 413908 + 229449 = 643357
39. 648648 ^ 2 = 420744227904 420744 + 227904 = 648648
40. 670033 ^ 2 = 448944221089 448944 + 221089 = 670033
41. 681318 ^ 2 = 464194217124 464194 + 217124 = 681318
42. 791505 ^ 2 = 626480165025 626480 + 165025 = 791505
43. 812890 ^ 2 = 660790152100 660790 + 152100 = 812890
44. 818181 ^ 2 = 669420148761 669420 + 148761 = 818181
45. 851851 ^ 2 = 725650126201 725650 + 126201 = 851851
46. 857143 ^ 2 = 734694122449 734694 + 122449 = 857143
47. 961038 ^ 2 = 923594037444 923594 + 37444 = 961038
48. 994708 ^ 2 = 989444005264 989444 + 5264 = 994708
49. 999999 ^ 2 = 999998000001 999998 + 1 = 999999
50. 4444444 ^ 2 = 19753082469136 1975308 + 2469136 = 4444444
51. 4927941 ^ 2 = 24284602499481 2428460 + 2499481 = 4927941
52. 5072059 ^ 2 = 25725782499481 2572578 + 2499481 = 5072059
53. 5555556 ^ 2 = 30864202469136 3086420 + 2469136 = 5555556
54. 9372385 ^ 2 = 87841600588225 8784160 + 588225 = 9372385
55. 9999999 ^ 2 = 99999980000001 9999998 + 1 = 9999999
6.2s
And 1 is somehow considered a Kaprekar number.
1 IS a Kaprekar number.
Other than 1, your list above is missing at least 4 numbers: 4879, 5292, 38962
and 627615. The reason is you are not handling zeros correctly.
But seriously, these numbers are constants. There is no need to waste my time
and computer time calculating them. Put them in an array and do a search for
whatever number you want to check. "Is 123456 a Kaprekar number?" Not in the
array, so no it is not.
Done.
If I was writing a program that used PI (3.14159265) in many places, I would
define it as a CONST. I would not waste my time calculating it every time I
needed it. PI has already been calculated to one million digits.
Why waste time re-inventing the wheel?