Talk:Kaprekar numbers: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
m (→‎Just for fun: added a new talk section.)
 
(22 intermediate revisions by 4 users not shown)
Line 142: Line 142:
For fun I tried reversing the digits in the squared number (e.g. using "5203" as the "squared number" for 55 instead of "3025") to see if there would be any pattern in the new results. There were some overlaps for numbers that were all repeating digits. I didn't see anything notable. I got 17 rakerpak (kaprekar backwards....get it?) each for base 10 and 17. The code isn't notable either (just add a bit to one line to reverse the string representation of the squared number). I thought it would be kinda neat to think about. --[[User:Mwn3d|Mwn3d]] 15:16, 8 August 2011 (UTC)
For fun I tried reversing the digits in the squared number (e.g. using "5203" as the "squared number" for 55 instead of "3025") to see if there would be any pattern in the new results. There were some overlaps for numbers that were all repeating digits. I didn't see anything notable. I got 17 rakerpak (kaprekar backwards....get it?) each for base 10 and 17. The code isn't notable either (just add a bit to one line to reverse the string representation of the squared number). I thought it would be kinda neat to think about. --[[User:Mwn3d|Mwn3d]] 15:16, 8 August 2011 (UTC)



----
== ooRexx ==

I tried this for ooRexx
I tried this for ooRexx
Had to change # to n
Had to change # to n
Line 148: Line 150:
It works with Numeric Digits 14
It works with Numeric Digits 14
--[[User:Walterpachl|Walterpachl]] 14:53, 29 June 2012 (UTC)
--[[User:Walterpachl|Walterpachl]] 14:53, 29 June 2012 (UTC)


== Common Lisp Implementation ==

Let me start by saying that I feel the criteria that should be used to modify existing code are the following:
*Correctness
*Fulfilling additional tasks / generality
*Improved readability
*Use of a programming languages idioms - For example, in CL the use of multiple return values is preferred to say setting multiple global variables.
*Improved performance
*(Note: There may be some special situations missing from this list)

Using these criteria, I modified the original CL implementation mainly because I thought that I could improve the readability by using more Lisp language features like multiple return values. In short, I felt like I was reading a C program written in Lisp: The original code isn't 'Lispy'. Later, I made some changes to the basic algorithm that substantially improved the performance (and even later came across the CO9 technique which again substantially improved performance). Because the original version supports additional number bases, I decided to add a separate fast version and retain the older, more general, version. Recently, [[Nigel Galloway]] added support for all number bases to the 'fast implementation'. Based on the criteria above, I would normally find this to be an improvement, however, the code again resembles a C implementation written in Lisp (especially with the global bindings of BASE and BASE-1) and the performance has also decreased substantially. My version is able to finish the first 1 million Kaprekar numbers in ~9.1 seconds; The new version finishes in ~124 seconds. Memory usage and garbage collection also increased by an order of magnitude.

Because the new version lacks additional generality beyond the 'slow version', decreases readability and the use of Lisp idioms, and is substantially slower than the previous version, I've decided to revert the changes made by Nigel. I added this talk section to explain my rationale for making the revision. --[[User:Lhignight|Larry Hignight]] 23:40, 18 September 2012 (UTC)
: Hi Lhignight, its good to see your criteria - I hope they are in decreasing order of importance for the general task, as optimising for speed can detract from the other concerns if done to excess - especially if a task can be 'adequately' fulfilled without it. Although you could add another implementation that was faster and leave a more idiomatic example alone. --[[User:Paddy3118|Paddy3118]] 16:08, 19 September 2012 (UTC)
:: Fleshing this out a bit more, in terms of the order of importance, I consider correctness to be the most important criteria, performance the least (unless specifically mentioned in the task), and the remaining criteria are about equal in my opinion. In most programming languages, improved readability and use of language idioms are often highly correlated (at least among experienced users of the language). Performance is a nice bonus item, but if it detracts from any of the other criteria, it should be added as a separate task assuming that it is correct and significantly faster: 5% faster, skip it; 5x faster, add it. --[[User:Lhignight|Larry Hignight]] 23:38, 21 September 2012 (UTC)

Hi Larry, what implementation of common lisp are you using? I only replaced a function number of digits, which calculated the number of digits by dividing by 10 until less than 10 with floor (log N / log Base) which is a reasonable way to determine the number of digits in a number. This should be faster unless your implementation has no reasonable log function. Note that BASE and BASE-1 are not variables they are constants. I didn't change the logic. A mute point now as ledrug has done tha task properly.--[[User:Nigel Galloway|Nigel Galloway]] 18:44, 19 September 2012 (UTC)
: Hi Nigel, I did the initial testing using CLISP. I've since tested both versions using LispWorks, which improved your times substantially, but it is still noticeably slower. I'll separate out your other points so that you can respond to each comment directly. --[[User:Lhignight|Larry Hignight]] 22:42, 23 September 2012 (UTC)

"I didn't change the logic."<br>Perhaps you didn't intend to change the logic, but your version fails to identify 9,999,999 and 99,999,999 as Kaprekar numbers whereas mine correctly identifies them, so yes, you did change the logic. --[[User:Lhignight|Larry Hignight]] 22:42, 23 September 2012 (UTC)

"Note that BASE and BASE-1 are not variables they are constants."<br>While I applaud the additional mathematical insight that you provided, I'm concerned that you don't have very much experience as a Lisp developer and should probably refrain from modifying existing Lisp solutions. For example, the CLHS explicitly states that [http://www.lispworks.com/documentation/HyperSpec/Body/s_setq.htm setq] is used with variables "other than a constant variable." In addition to BASE and BASE-1 not being constants, which isn't what you didn't want in the first place, there are a number of other issues:
*In CL, the standard method for defining a constant is the [http://www.lispworks.com/documentation/HyperSpec/Body/m_defcon.htm defconstant] form. It is also the convention in CL to name constants with leading and trailing + characters (eg +base+).
*The unnecessary use of dynamically scoped variables is discouraged. The preferred method for incorporating base would have been to define it as an optional function parameter with a default value of 10.
*The manner in which you replaced the num-digits macro resulted in code that was less readable. In my version, it was very clear when the number of digits were being calculated because I used an abstraction to separate this calculation from the function. You eliminated a useful abstraction.
*For some reason, you replaced the let* form with the lh and rh values with a multiple-value-bind form. I really have no idea why you would do this.
*I'm not sure why you feel that two calls to log followed by floor() and adding 1 would be faster than a loop that runs in theta(log n) time. --[[User:Lhignight|Larry Hignight]] 22:42, 23 September 2012 (UTC)

"A mute point now as ledrug has done tha task properly."<br>Yeah, it's kind of a shame that someone broke it though. --[[User:Lhignight|Larry Hignight]] 22:42, 23 September 2012 (UTC)

A lot of hot air for somthing that doesn't exist! I did not break the origional solution, I made it solve the task. Obviously in a way that doesn't suit you. In place of personal abuse you might have changed it to suit you and solve the task. Returning it to a version which doesn't solve the task was not helpful. I was going to restore my version and let you change it, but I'll leave it to you to restore and correct if you think it adds anything. I have a much better idea.--[[User:Nigel Galloway|Nigel Galloway]] 10:40, 26 September 2012 (UTC)
:YOUR version is not CORRECT. Anyone can view the history, try the examples above, and see this is true. I left untouched a slower version that solved the 'Extra extra credit' task, which you seem to be obsessed with for some reason. Why you would expect anyone to fix an incorrect, slower, less readable, and less lispy version of your software, when better versions already existed, is way beyond me. --[[User:Lhignight|Larry Hignight]] 01:17, 30 September 2012 (UTC)
Now with +SCARY CAPITALS+! I dont't expect anyone to do anything, it was merely a suggestion that you 'put up or shut up'. I modified your solution so that it worked, with sample output provided. If it didn't work for large numbers (ouside the scope of the task) it is probably because your log algorithms are inaccurate. You changed it back to your solution, which doesn't solve the task. Your situation is that ledrug didn't like your solution and has replaced it with his. If you have nothing to put up on the task page then all this is just hot air. None of your comments and insults have indicated that which you wish me to do about your situation. As I said I have a better idea, which I have placed on the task page.--[[User:Nigel Galloway|Nigel Galloway]] 11:38, 1 October 2012 (UTC)
:How on earth is something that doesn't work CORRECTLY a better idea! And for the record, Ledrug replaced YOUR version, not mine. --[[User:Lhignight|Larry Hignight]] 08:24, 4 October 2012 (UTC)

Well, joining the fray. First, the message left on my user talk page by one genius:
:Please leave [http://rosettacode.org/wiki/Kaprekar_numbers#In_the_style_of_the_C.2B.2B_generator this code] alone until you can demonstrate an understanding of this problem. Your second attempt at this is an improvement over your first naive attempt, but still not as interesting as [http://rosettacode.org/wiki/Kaprekar_numbers#In_the_style_of_the_C.2B.2B_generator this]. Maybe I shall seperate the [http://rosettacode.org/wiki/Casting_out_nines Casting_out_nines] from the Kaprecar part: <lang clisp>(let ((kk (* N N))) (do ((B Base (* B Base))) ()(let (( nr (/ (* N (- B N)) (- B 1)))) (if (< 0 nr) (let ((q (floor (- N nr)))) (if (= kk (+ nr (* q B)))</lang> (unsigned, left by [[User:Nigel Galloway]])
I'll make a few comments, but won't discuss it any more after that, since that would be obviously futile.
# I don't know what kind of antique lisp machine you have installed in your basement, but your code does not compile on either SBCL or Clisp. Maybe geniuses shan't be bothered by such trifles.
# Your code, once made compile with SBCL (change that <code>do (...) ()</code> to <code>do (...) (nil)</code>, does exactly the samething mathematically: raise a power of a base repeately, until either it splits the square of n with the right sum, or it's too large. Except you are doing it in a convoluted way, using non-integer methods on integers, and ends up with something literally 100 times slower then my edit you reverted (on SBCL that is, I don't know about your antique lisp machine).
# You had one good idea of checking congruence, and a whole lot of terrible ones: being thoughtless in dealing with datatypes (<code>pow</code> and <code>log</code> on integers in C++, <code>/</code> and <code>floor</code> in Lisp); being sloppy in performance tuning (your "v.fast" C++ code isn't all that fast); being vengeful (<code>paddy_cnt</code>?); being narcissistic (own name as variable?); being inconsiderate to code readers (what kind of person posts unindented Lisp?); and generally being an all-around dick.
# Your sarcasm in the lisp code was neither subtle nor funny. You give British humor a bad name.

You are probably not stupid, but it's safe to say you are not the smartest person on RC, by a long shot. And smart people around here tend to have good manners, unlike you or I. Stop treating yourself like you are the one true genius, and try to do something that's helpful to people instead of showing off, OK? (my guess: probably not. Oh well.) --[[User:Ledrug|Ledrug]] 00:10, 4 October 2012 (UTC)
:Well said. He is clearly more interested in being a pompous dick then contributing quality code to RC. --[[User:Lhignight|Larry Hignight]] 08:24, 4 October 2012 (UTC)

Latest revision as of 17:58, 24 October 2020

We've been linked to from a prestigious source

The On-Line Encyclopedia of Integer Sequences has a link to this RC page! (The link is to the google cache - for the highlight) --Paddy3118 22:24, 29 August 2011 (UTC)

And on this entry A194218 we get mentioned a second time as a place to compare programs! This is just brilliant to me as I have admired OEIS for what it collates for some time. --Paddy3118 06:11, 30 August 2011 (UTC)

Sweet. --Michael Mol 13:15, 30 August 2011 (UTC)

Java count missing

Just add (and show), the count of how many there are <1million and you will have completed the stretch goal! --Paddy3118 14:07, 7 June 2011 (UTC)

I had actually added it as a test because I saw the other examples do it, but for some reason I didn't read it as a requirement so I took it out. Mornings are hard. --Mwn3d 14:47, 7 June 2011 (UTC)
:-)
--Paddy3118 17:29, 7 June 2011 (UTC)

Why the complexity?

The wikipedia page says "Let X be a non-negative integer. X is a Kaprekar number for base b if there exist non-negative integers n, A, and positive number B satisfying ...". In other words A can be zero. Why do we have to have a bunch of text claiming that A cannot be zero but A can be an empty string and that it is meaningful to add an empty string to a number? (Can't we just take advantage of the fact that leading zero digits do not change the value of a number?) --Rdm 20:56, 7 June 2011 (UTC)

To me the text "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" means that A can be zero. Unless I'm confused about what A and B are. --Mwn3d 22:30, 7 June 2011 (UTC)

Consider 100*100 = 10000 which could be split as 100 + 00. Now the 00 is disallowed. Compare that with 1*1 = 1 which is a K-number. For this to be a k-number then it must be expressed as either 1 plus no digists to the right or no digits to the left + 1. Either way no digits is treated as a positive integer of value zero but any string of one or more noughts is expressly forbidden.

Having 1 in the series seemed to add that complexity that needed explaining in my mind. --Paddy3118 23:14, 7 June 2011 (UTC)

It has to do with the way that the splitting is done mathematically, i.e., and where ; in the case of , it becomes possible to use . This only works for that value; nothing larger is equal to itself when squared and zero is inadmissible by the constraint on . Trust a mathematician to fluff the operation of splitting a number-string and introduce an unwanted special case! –Donal Fellows 23:48, 7 June 2011 (UTC)
I was reading the expressions, but the implication just fazed me. I had a mental block about the 1 case, even when it was explained more explicitely. Gosh, I'm not getting senile? er, I'll change that ? to ! --Paddy3118 09:05, 8 June 2011 (UTC)

Which sounds better?

What should we say, "split into parts" or "split once into whole number components made of groups of neighbouring digits from within the number"?

We are splitting into the number into numeric values the belong to the set of "whole numbers" aren't we? Or is there a misunderstanding here?

"split into parts" sounds too vague to me.

--Markhobley 16:06, 8 June 2011 (UTC)

If I had to change it from what we have now I'd say "split into two parts". The way it is now seems fine to me especially with the examples in the description. Since the number is specified as an integer, I don't think we need to add "whole number" noise. --Mwn3d 16:16, 8 June 2011 (UTC)
Right. The "whole numbers" verbage avoids the issue of explaining that chains of zeros do not fulfil the criteria, (although we would probably still need to keep that as a note to remind the task implementers.) --Markhobley 16:22, 8 June 2011 (UTC)
"Whole numbers", to me, includes 0 so I don't think that works that way. Is "whole numbers" an official mathematical set like "natural numbers"? Maybe say "split in to two positive parts"? --Mwn3d 16:33, 8 June 2011 (UTC)
There isn't a consensus on the definition of a whole number, natural numbers, or counting number (especially in grammer/grade/highschool/secondary school texts). See the links for "Mathworld" http://mathworld.wolfram.com/WholeNumber.html, http://mathworld.wolfram.com/NaturalNumber.html, http://mathworld.wolfram.com/CountingNumber.html, all those seem to prefer defintions based on integers (positive integers, nonnegative integers, negative intergers, ∙∙∙ --Gerard Schildberger 19:26, 21 March 2012 (UTC)
Generally zero is not considered to be a whole number, but there are people who would disagree. We could use the term "integers greater than or equal to one" --Markhobley 19:40, 8 June 2011 (UTC)
FWIW zero is positive, but not a whole number, so "positive parts" would be no clearer IMHO, I would also call them numerical components, rather than parts, because 12 can be split into parts of sizes 8 and 4, but 1 and 2 are its numerical components. --Markhobley 19:45, 8 June 2011 (UTC)
OK 0 cannot be positive because then no one would need to call anything "non-negative" ([1] [2] [3]). The phrase "string representation" earlier in the first sentence avoids the "split" definition you're thinking of. And once again, the examples following the language reinforce the wording. --Mwn3d 19:58, 8 June 2011 (UTC)

Promoting to full task status

As the resolution of any of the discussions above in unlikely to change the fundamental task goals, and we have several correct examples.--Paddy3118 20:07, 8 June 2011 (UTC)

I say yes. Go! --Markhobley 20:11, 8 June 2011 (UTC)

C++ sample code

I'm tempted to change all the "long" type to "long long" in the C++ sample code, because it would fail on 32 bit system as is. --Ledrug 17:08, 14 June 2011 (UTC)

That change seems fine since the output goes up pretty high anyway. Does that just mean they used a 64-bit machine? --Mwn3d 17:17, 14 June 2011 (UTC)
What 'long' means depends on the compiler, but it's typically defined to be the longest native integer type, while 'long long' is (I think) defined to be 64-bit. Someone correct me if I'm wrong. I'll change the code anyhow, because it overflows with 32 bit long for sure. --Ledrug 17:25, 14 June 2011 (UTC)
On every C and C++ compiler I've used on a 64-bit OS (so, GNU on Linux and MSVC/MSVC++ on Windows), 'long' is 32-bits. 'long long' is 64-bits. I don't know about, e.g. SPARC64, though. That's why you get things like uint32_t from stdint.h or DWORD from Windows.h. --Michael Mol 18:05, 14 June 2011 (UTC)
It's always sort of confusing to me anyway. For reference, see http://www.unix.org/whitepapers/64bit.html, which also has a table for integer types. It's true that if you worry about the exact length, use exactly defined types instead of the compiler native ones. --Ledrug 18:28, 14 June 2011 (UTC)
If 53 bits is enough (and I think it is, here), you might also consider using double. --Rdm 20:01, 14 June 2011 (UTC)
Heh, tried double, seg faulted. I'm not very motivated to see what's happening--probably that atol()--so I'll just leave it as is. --Ledrug 21:44, 14 June 2011 (UTC)

What's up with the people modifying the C++ code, previously (before long to long long) it returned wrong numbers, now it simply never returns on 32 bit system with g++ 4.5. Also maybe we should promote testing sample code with full warnings on: g++ gives signedness mismatch warning on line 20. --Ledrug 23:02, 15 June 2011 (UTC)

Would someone modify the code to work for a happy medium and then state on the task page that the C++ code is fragile w.r.t 32/64 bit systems and where the fragility lies? It sounds like it is an issue that should be taken into account when comparing that particular C++ code with other languages.

Come to think of it, maybe other similar languages might state why they don't have an issue? Or maybe there is a separate task here: "Do something that is otherwise straight-forward but needs slightly different code, or will not work on both 32 and 64 bit X86 systems and/or up-to-date, widely used, but different versions of compilers/interpreters"? --Paddy3118 05:06, 16 June 2011 (UTC)

I really don't think it's a language or compiler problem, though. And this task is already pretty straightforward: you take a number and look at its digits, that's it, the language certainly can handle that. --Ledrug 08:54, 16 June 2011 (UTC)

Indentation...

Can we please don't adjust someone else's code indentation just because it's different from yours... --Ledrug 20:21, 14 June 2011 (UTC)

I appreciate that it can be annoying when people touch your code, but it's a wiki. You're going to have to get used to it. My code is changed regularly. Sometimes for the better. Sometimes it's simply changed but not really improved, in my opinion. Unless you want to get involved in edit wars all the time, you're going to have to relax about it. In your case, I honestly thought that the huge indent was accidental. Otherwise I would have left it alone. Fwend 21:23, 14 June 2011 (UTC)
K&R 8 space tab is typical BSD style, I'm not sure why you'd think it's accidental. And I didn't mind someone changine my code, but changing purely because of the code indentation is prone to start an edit war, please don't get too used to it (and I didn't revert your edit, did I?). --Ledrug 21:32, 14 June 2011 (UTC)
You might try leaving the original indentation if it is consistent and "reasonable"; and adjusting to the existing style when making small edits, (helps with the diffs). If you make a large change then probably relax the rules about changing styles? --Paddy3118 04:21, 15 June 2011 (UTC)

All that being said, there's a balance to be struck here between demonstrating that an idiosyncratic style is acceptable vs demonstrating "normal" code for the culture in question. And in this case, it's true that Perl culture is quite accepting of idiosyncracies, but also that the culture as a whole tends toward 4-space indent, not 8. In fact, 4-space indent is specifically recommended in the Camel book. So a good case can be made for either, and people should not get too hung up about either idiosyncratic tabbing or having that idiosyncratic tabbing normalized to something closer to the norm. Certainly for the Perl 6 examples I reserve the right to be a bit pickier about setting the cultural norm, since many new people will be learning Perl 6 by studying these examples, whereas Perl 5 already has large collections of existing code outside of RC. TimToady 06:03, 16 June 2011 (UTC)--

Oh dear, didn't expect this to get the attention of Larry. Let me clarify: it's not because someone changed my indentation; rather since people tend to have very strong opinions about what a tab size is, changing anybody's code just to make the indenting fit one's own taste may start flame wars (well, sort of like what we have now), and people should refrain from doing that IMO. --Ledrug 07:07, 16 June 2011 (UTC)
This isn't really a flamewar, just a back-and-forth defense and clarification of different positions. It's actually pretty normal and healthy for RC. It's when the participants stop arguing in good faith (which I don't see anyone here having done) that it gets bad. --Michael Mol 15:53, 16 June 2011 (UTC)

Old D version

(Regarding the changes to the second D version, that Fwend has reverted: the current second D version returns 0/1 but the function has to return a bool, and D is not C. The C-style code was faster, and it was shorter too. Pre-conditions are a good habit in D, that makes the code safer and less wild-west-style as C code. This C-derived code is also able to accept a bigger range of input n values. And the code has a main too, so it's not just a nonworking fragment.) --Unsigned, added by 95.235.203.130

An answer to a question: it's not n*n that causes the first overflow, it's tens*=10. I think assert(n<=3_162_277_660UL) is correct.
Ah ok, I think I asked that question about overflow. Truth be told, I was only guessing, since I don't even know what a "ulong" is in D. --Ledrug 17:10, 16 June 2011 (UTC)
I think my code should stand, because it shows a different approach to solving the problem, which makes the task page more interesting. Fwend 20:33, 16 June 2011 (UTC)
Multiple approaches are allowed (and encouraged). If the examples do things differently, put them both up and notate them. --Mwn3d 20:41, 16 June 2011 (UTC)
The C version is basically the same as the second and third D versions, slight difference is that it breaks from testing if the first half of nn is already larger than n, so only needs to do half the math for most cases. The goofy looking comparison itself is just coincidence after looking at the quotient/remainder equation. --Ledrug 21:30, 16 June 2011 (UTC)
O, hey that gives me an idea: if (b > n) break; :) Fwend 22:32, 16 June 2011 (UTC)

Extra extra credit?

I just had an idea, maybe make an optional task for code to allow bases other than 10--would that be fun? --Ledrug 22:55, 16 June 2011 (UTC)

Sounds good. Go ahead and add it to the extra credit requirements. I think a reasonable amount of languages should be able to do it easily. --Mwn3d 00:02, 17 June 2011 (UTC)
Added. Now thinking of it, it may be a little hard for most examples using string based methods. Oh well. --Ledrug 00:46, 17 June 2011 (UTC)
I think I already have it for Java. Here's my full base 17 list (I have 24):
1	1	1	0 + 1
16	g	f1	f + 1
64	3d	e2g	e + 2g
225	d4	a52g	a5 + 2g
288	gg	gf01	gf + 01
1536	556	1b43b2	1b4 + 3b2
3377	bbb	8093b2	809 + 3b2
4912	ggg	ggf001	ggf + 001
7425	18bd	24e166g	24e + 166g
9280	1f1f	39b1b94	39b + 1b94
16705	36db	b992c42	b99 + 2c42
20736	43cd	10de32fg	10de + 32fg
30016	61eb	23593f92	2359 + 3f92
36801	785d	351e433g	351e + 433g
37440	7a96	37144382	3714 + 4382
46081	967b	52g94382	52g9 + 4382
46720	98b4	5575433g	5575 + 433g
53505	af26	6ga43f92	6ga4 + 3f92
62785	cd44	9a5532fg	9a55 + 32fg
66816	da36	aeg42c42	aeg4 + 2c42
74241	f1f2	d75f1b94	d75f + 1b94
76096	f854	e1f5166g	e1f5 + 166g
83520	gggg	gggf0001	gggf + 0001
266224	33334	a2c52a07g	a2c5 + 2a07g
--Mwn3d 01:23, 17 June 2011 (UTC)
Great, that looks identical to mine (except I cheated and didn't split 1, because it's not really the same as the rest). --Ledrug 01:42, 17 June 2011 (UTC)
Note that many details of that line format were listed as "for example". Now that we have examples, perhaps that sentence should be removed as superfluous? Or does someone want to promote them to extra credit work? --Rdm 11:15, 27 June 2011 (UTC)

Incomplete output

The task specifically asks that output be shown for n<10000 so when shortening output to fit the page, please ensure that these are still all shown, thanks. --Paddy3118 07:28, 22 June 2011 (UTC)

In base B <> 10

Just an idea : why bother printing in base B ? Being Kaprekar in base B is unrelated to the printing base. Then, when implementing with a loop doing divisions/multiplications by 10, just replace 10 by B to get Kaprekar numbers in base B. Toucan 17:18, 26 June 2011 (UTC)

I don't quite understand that. Can you show an example? --Mwn3d 17:29, 26 June 2011 (UTC)
Let's take an example from the implementation in C:
74241 is a Kaprekar number in base 17. But, written in base 17, it's f1f2, or if you prefer f1f2_17 = 74241_10, with obvious notation.
The fact that it's a Kaprekar number will be better showed when printing in base 17 (numbers with _17 appended), and you will write
f1f2_17^2 = d75f1b94_ 17 and d75f_17 + 1b94_17 = f1f2_17
However, the number is still f1f2_17 = 74241_10 = 12201_16 = ... You can print this number in any base you wish, it's the same number.
As Ledrug says, the only reason to print f1f2 is showing how N^2 may be cut to prove it's a Kaprekar number. But it's not really related to
Kaprekar numbers, it's a "printing in base B" subtask.
Toucan 11:09, 27 June 2011 (UTC)
True. I added the non-base10 extra requirement, and the printing part is there to clearly show if a solution decoupled the numerical aspect from a number's string representation. The base 17 was specifically chosen because languages often can handle base conversion up to 16 natively, so 17 may require a hand-rolled string conversion, which can also be interesting while not difficult. Plus, if you print it out in base 17, it's much easier for a human reader to see the correctness of the solution. --Ledrug 21:25, 26 June 2011 (UTC)

Just for fun

For fun I tried reversing the digits in the squared number (e.g. using "5203" as the "squared number" for 55 instead of "3025") to see if there would be any pattern in the new results. There were some overlaps for numbers that were all repeating digits. I didn't see anything notable. I got 17 rakerpak (kaprekar backwards....get it?) each for base 10 and 17. The code isn't notable either (just add a bit to one line to reverse the string representation of the squared number). I thought it would be kinda neat to think about. --Mwn3d 15:16, 8 August 2011 (UTC)


ooRexx

I tried this for ooRexx

Had to change # to n
But get a syntax error for j=100000 since s=1E+10
It works with Numeric Digits 14

--Walterpachl 14:53, 29 June 2012 (UTC)