Kaprekar numbers: Difference between revisions
m (→{{header|Tcl}}: duuuh…) |
(→{{header|Python}}: Shorter - considers valid input range.) |
||
Line 13: | Line 13: | ||
=={{header|Python}}== |
=={{header|Python}}== |
||
(Swap the commented return |
(Swap the commented return statement to return the split information). |
||
<lang python>>>> def k(n): |
<lang python>>>> >>> def k(n): |
||
⚫ | |||
if n <1: |
|||
⚫ | |||
return None |
|||
⚫ | |||
elif n == 1: |
|||
⚫ | |||
return n |
|||
return n |
|||
⚫ | |||
else: |
|||
⚫ | |||
⚫ | |||
⚫ | |||
⚫ | |||
return n |
|||
⚫ | |||
>>> [x for x in range(10000) if k(x)] |
>>> [x for x in range(1,10000) if k(x)] |
||
[1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999] |
[1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999] |
||
>>> len([x for x in range(1000000) if k(x)]) |
>>> len([x for x in range(1,1000000) if k(x)]) |
||
54 |
54 |
||
>>> </lang> |
>>> </lang> |
Revision as of 09:30, 7 June 2011
A positive integer is a Kaprekar number if the string representation of its square can be split once into parts that when summed add up to the original number.
For example 2223 is a Kaprekar number as 2223*2223 == 4941729 and 494 + 1729 == 2223
The series of kaprekar numbers begins: 1, 9, 45, 55, ....
The task is to generate and show all the Kaprekar numbers less than 10,000
As a stretch goal count and show how many Kaprekar numbers their are that are less than one million.
Note: In comparing splits of the square, a split of all zeroes is not counted - as zero is not considered positive. However a conceptual single split at the very end or before the first digit that produces one empty string does have the empty string counted.
Python
(Swap the commented return statement to return the split information). <lang python>>>> >>> def k(n): n2 = str(n**2) for i in range(len(n2)): a, b = int(n2[:i] or 0), int(n2[i:]) if b and a + b == n: return n #return (n, (n2[:i], n2[i:]))
>>> [x for x in range(1,10000) if k(x)]
[1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999]
>>> len([x for x in range(1,1000000) if k(x)])
54
>>> </lang>
Tcl
<lang tcl>package require Tcl 8.5; # Arbitrary precision arithmetic, for stretch goal only proc kaprekar n {
if {$n == 1} {return 1} set s [expr {$n * $n}] for {set i 1} {$i < [string length $s]} {incr i} {
scan $s "%${i}d%d" a b if {$b && $n == $a + $b} { return 1 #return [list 1 $a $b] }
} return 0
}
- Base goal
for {set i 1} {$i < 10000} {incr i} {
if {[kaprekar $i]} {lappend klist $i}
} puts [join $klist ", "]
- Stretch goal
for {set i 1} {$i < 1000000} {incr i} {
incr kcount [kaprekar $i]
} puts "$kcount Kaprekar numbers less than 1000000"</lang> Output:
1, 9, 45, 55, 99, 297, 703, 999, 2223, 2728, 4879, 4950, 5050, 5292, 7272, 7777, 9999 54 Kaprekar numbers less than 1000000