Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon.

Pages: 1-

/prog/ challenge V̅MCLXXIV: Dr. Kaprekar

Name: Anonymous 2018-08-31 12:52

Write the function KaprekarsConstant(num). num parameter being passed will be a 4-digit number with at least two distinct digits. Your program should perform the following routine on the number: Arrange the digits in descending order and, separately, in ascending order (adding leading zeroes to fit it to a 4-digit number), and subtract the smaller of the two resulting numbers from the bigger number. Repeat. Performing this routine will always cause you to reach a fixed number: 6174. Then performing the routine on 6174 will always give you 6174 (7641 - 1467 = 6174). Your program should return the number of times this routine must be performed until 6174 is reached.

。゜*:。:。☆゚・.+♩*:****。: *.。.゚   ・・❀o❀・゚。✧゚・ : 。*  ;:.* ゜+.。○。 ♩;゚゚ 。


For example: if num is 3524 your program should return 3 because of the following steps:
(1) 5432 - 2345 = 3087,
(2) 8730 - 0378 = 8352,
(3) 8532 - 2358 = 6174.

Shortest byte count wins. Good luck!

Name: Anonymous 2018-08-31 13:05

Executable or source byte count?

Name: Anonymous 2018-08-31 13:38

>>2
Source, anusboi. Don't be obtuse.

Name: Anonymous 2018-08-31 15:49

>>3
So I create an interpreter that has built-in support for solving this challenge, and perhaps a zero-byte source is interpreted to mean solve the challenge exactly, I would win?

Name: Anonymous 2018-09-01 8:13

>>4
This is life in codegolf.stackexchange.com ;w;
The competition should be between people using the same language, or maybe by counting the steps that the program would take in a turing machine, or the number of beta reductions one would have to do in lambda calculus.

Name: Anonymous 2018-09-02 12:45

We could autimically nitpick and try to prove our superiority in finding the misdefined minutae of the challenge. Or we could just fuck off and write some code. It can even be in Haskell.

Name: Anonymous 2018-09-02 13:00

why does it go to 6174 though?

Name: Anonymous 2018-09-02 13:13

10,000 / !4 = 416.7 ? lol .66

Name: Anonymous 2018-09-02 13:37

10*9*8*7 / 24 = 210

24^x = 10000? or 210?

Name: Anonymous 2018-09-02 14:15

>>7
Numberphile.jpg because it loops with it

Name: Anonymous 2018-09-11 15:17

FORTH to the rescue again!

: SEPARATE
DUP 2DUP
<<# 0 # # # #>>
DROP ROT
<<# 0 # # #>>
DROP 10 MOD
ROT
<<# 0 # #>>
DROP 100 MOD 10 MOD
2SWAP SWAP
1000 MOD 100 MOD 10 MOD
ROT ROT SWAP 2SWAP ;

: ?SWAP
2DUP < IF SWAP THEN ;

: 3SORT
?SWAP >R ?SWAP R> ?SWAP ;

: SORT
3SORT 2>R ?SWAP 2R> 3SORT ;

: COMBINE
SWAP 10 * +
SWAP 100 * +
SWAP 1000 * + ;

VARIABLE NUM
VARIABLE COUNT

: KAPREKAR
NUM !
BEGIN
NUM @ DUP
SEPARATE SORT COMBINE
SWAP SEPARATE SORT
SWAP 2SWAP SWAP COMBINE
- DUP
NUM !
COUNT DUP @ 1 + SWAP !
6174 =
UNTIL
COUNT ? ;

Name: Anonymous 2018-09-12 3:31

JAVA SCRIPT to the rescue



ε = '';
ℕ = Number;
(0).__proto__.s = ε.split;
[].__proto__.j = [].join;

digits = num => [(t = num.s("").sort()).j(ε), t.reverse().j(ε)].map(ℕ)

kc = num => {
for (i = 1; 1; i++) {
let [a, b] = digits(num);
if ((num = b - a) == 6174)
return i
}
}

console.log(kc(3524)) // 3
console.log(kc(6174)) // 1


Spaces added for readability, it minifies down to 237 bytes

Name: Anonymous 2018-09-12 5:30

HASKELL is a dead do g

Name: Anonymous 2018-09-12 6:49

basic FIOC3 solution, probably can be golfed harder:
x=input();i=0
while 1:
x=''.join(sorted(x));x=str(abs(int(x)-int(x[::-1])));i+=1
if x=='6174':break
print(i)

Name: Anonymous 2018-09-12 7:53

>>14
i+=1
can be i++ for robustness

Name: Anonymous 2018-09-12 7:55

>>15
python doesn't have the pre/post-incrementation syntax:

Python 3.5.2 (default, Nov 23 2017, 16:37:01)
Type "copyright", "credits" or "license" for more information.

IPython 2.4.1 -- An enhanced Interactive Python.
? -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help -> Python's own help system.
object? -> Details about 'object', use 'object??' for extra details.

In [1]: i=0;i++
File "<ipython-input-1-cac78eb7531c>", line 1
i=0;i++
^
SyntaxError: invalid syntax

Name: Anonymous 2018-09-12 9:28

>>16
Why?

Name: Anonymous 2018-09-12 9:30

>>17
just guido things

Name: Anonymous 2018-09-12 10:35

>>18
Guido is dead. Time to implement post-increment operator and generics.

Don't change these.
Name: Email:
Entire Thread Thread List