Kaprekar’s constant 6174

Post Reply
Henko
Posts: 846
Joined: Tue Apr 09, 2013 12:23 pm
My devices: iPhone,iPad
Windows
Location: Groningen, Netherlands
Flag: Netherlands

Kaprekar’s constant 6174

Post by Henko »

Code: Select all

' Kaprekar's constant 6174
'
' start with any 4 digit number, with at least 2 digits 
' different from each other, and greater than 1000.
' Make two new numbers by sorting the number digits upward and
' downward.
' Calculate the difference and repeat the previous step with
' that difference.
' After a maximum of 7 steps the difference will become 6174
' and stays at that value
'
input "number (4 digits): ":n  ' input a 4 digit number
dim a(4),b(4)
do
  print n         ' print the number to be processd
  num2arr(n,a)    ' convert to array  a with the 4 digits
  sort a          ' sort the array a (descending)
  revert(a,b)     ' revert array a into ascending array b
  dif=arr2num(b)-arr2num(a)  ' convert into numbers & subtract
  if dif=n then break else n=dif  ' done if output = input
  until forever
print dif
end

def num2arr(x,a())
n$=str$(x)
for i=0 to 3 ! a(i)=val(mid$(n$,i,1)) ! next i
end def

def revert(x(),y())
for i=0 to 3 ! y(3-i)=x(i) ! next i
end def

def arr2num(x())
res=0
for i=0 to 3 ! res+=10^(3-i)*x(i) ! next i
return res
end def







smbstarv
Posts: 111
Joined: Wed Nov 27, 2013 3:44 pm
My devices: Ipad 6th gen
Flag: Netherlands
Contact:

Re: Kaprekar’s constant 6174

Post by smbstarv »

Funny !
AI told me: 6174 kan worden geschreven als de som van de eerste drie machten van 18: 18³ + 18² + 18¹ = 5832 + 324 + 18 = 6174, en toevallig is 6 + 1 + 7 + 4 = 18.
Grappig !

Post Reply