{
   "name" : "Kaprekar",
   "author" : "henklass",
   "ancestor_list" : [],
   "description" : "Kaprekar\r\nTake any four-digit number except multiples of 1111. Sort the digits. Make numbers of the digits in ascending and in descending order. Repeat with the difference. After no more than 7 steps you will reach 6174, Kaprekar's Constant. \r\nThis makes for some fine music, lasting about 13 hours.",
   "labels" : [
      "arpeggio",
      "math",
      "kaprekars constant"
   ],
   "code" : "/*\r\nKaprekar\r\nTake any four-digit number except multiples of 1111. Sort the digits. Make numbers of the digits in ascending and in descending order. Repeat with the difference. After no more than 7 steps you will reach 6174, Kaprekar's Constant. \r\nIf you use three-digit numbers, you will get 495 in no more than 6 steps.\r\n*/\r\ns.boot;\r\n\r\n(\r\n//Synthdefs: sinewaves for numbers, with pan\r\nSynthDef( \\numbers, {|freq=30, panpos|\r\n\t\tOut.ar(0,\r\n\t\t\tPan2.ar(SinOsc.ar(freq, mul: 0.5*EnvGen.kr(Env.linen(0.01, 0.1, 0.01, 0.6), doneAction: 2)),panpos)\r\n\t\t)\r\n}).add;\r\n//sawtooth for digits, filter associated with number of repetitions\r\nSynthDef( \\digitsplayer, {|freq=30, cutoff|\r\n\t\tOut.ar(0,\r\n\t\t\tPan2.ar(LPF.ar(Saw.ar(freq, mul: 0.5*EnvGen.kr(Env.perc(0.01,0.75), doneAction: 2)), cutoff),0)\r\n\t\t)\r\n}).add;\r\n)\r\n\r\n(\r\n\r\nvar number, theNumber, greatest, smallest;\r\nvar digits=Array.newClear(4);\r\n\r\n\r\n{\r\ng=0; //greatest number of repetitions\r\nfor (1, 9998, { arg i;\r\n\tif ( i.mod(1111)!= 0, {\r\n\t\tnumber = i;\r\n\t\tnumber.postln;\r\n\t\ttheNumber=number; //theNumber is used to be able to mess with the value while retaining the value of number\r\n\t\tif (theNumber < 150, {theNumber = theNumber + 150});\r\n\t\to=Synth( \\numbers, [freq: theNumber/5, panpos: 0]);\r\n\t\t0.1.wait;\r\n\t\ta=0; //number of repetitions\r\n\t\twhile ( {number != 6174}, {\r\n\t\t\tdigits.put(0, number.mod(10)); //isolate digits\r\n\t\t\tnumber=number.div(10);\r\n\t\t\tdigits.put(1, number.mod(10));\r\n\t\t\tnumber=number.div(10);\r\n\t\t\tdigits.put(2, number.mod(10));\r\n\t\t\tnumber=number.div(10);\r\n\t\t\tdigits.put(3, number.mod(10));\r\n\t\t\tdigits.sort; //sort digits\r\n\t\t\t//digits.postln;\r\n\t\t\tgreatest=(1000*digits[3])+(100*digits[2])+(10 * digits[1]) + digits[0];\r\n\t\t\tgreatest.post; \" \".post;\r\n\t\t\t\r\n\t\t\tsmallest=digits[3] + (10*digits[2])+(100*digits[1])+(1000*digits[0]);\r\n\t\t\tsmallest.post; \" \".post;\r\n\t\t\t//(digits.mirror2+60).postln;\r\n\t\t\t\r\n\t\t\t//play the digits\r\n\t\t\tPbind(\r\n\t\t\t\\instrument, \\digitsplayer,\r\n\t\t\t\\freq, Pseq(digits.mirror2+60).midicps,\r\n\t\t\t\\cutoff, 500*(a+1),\r\n\t\t\t\\dur, 0.1\r\n\t\t\t).play;\r\n\t\t\t0.8.wait;\r\n\t\t\r\n\t\t\t//play greatest and smallest\r\n\t\t\t\r\n\t\t\tif (smallest < 150, {smallest = smallest + 150;\r\n\t\t\t\t\t\t\t\t\t\tgreatest = greatest + 150});\r\n\t\t\to=Synth( \\numbers, [freq: smallest/5, panpos: -1]);\r\n\t\t\tp=Synth( \\numbers, [freq: greatest/5, panpos: 1]);\r\n\t\t\t0.1.wait;\r\n\t\t\t\r\n\t\t\tnumber=greatest-smallest;\r\n\r\n\t\t\ttheNumber=number;\r\n\t\t\tif (theNumber < 150, {theNumber = theNumber + 150});\r\n\t\t\to=Synth( \\numbers, [freq: theNumber/5, panpos: 0]);\r\n\t\t\tnumber.postln;\r\n\t\t\t0.1.wait;\r\n\t\t\ta=a+1;\r\n\t\t});\r\n\t\ta.postln;\r\n\t\tif (a>g, {g=a});\r\n\t});\r\n});\r\ng.postln;\r\n}.fork;\r\n)",
   "id" : "1-4Wv",
   "is_private" : null
}
