{
   "id" : "1-4Sq",
   "is_private" : null,
   "code" : "//Prime numbers\r\n\t\t\r\nServer.default.boot;\r\n(\r\n//maximum number\r\n~max=100;\r\n\r\n//synthdefs: 2 filtered sawteeth for number and divisor\r\nSynthDef ( \\gsaw, {arg g;\r\n\tOut.ar(1, \r\n\t\tRLPF.ar ( Saw.ar(20*g), 10000*(1-(g/~max)), 0.2, 0.2)\r\n\t\t)\r\n}).add;\r\n\r\nSynthDef ( \\dsaw, {arg d, g;\r\n\tOut.ar(0,\r\n\t\tRLPF.ar( Saw.ar(20*d), 10000*d/sqrt(g), 0.2, 0.2)\r\n\t\t// the square root is important: if you search for the divisors of a number,\r\n\t\t// don't search beyond the square root of the number. There won't be any.\r\n\t)\r\n}).add;\r\n\r\n//a fm-sound with carrier 20*number and modulator 20*divisor, modulation index = 10\r\nSynthDef (\\fm, {arg m, c;\r\n\tOut.ar(0,\r\n\t\tPan2.ar(SinOsc.ar(20*c+(SinOsc.ar(20*m, 0, 200*m, 0))), 0)\r\n\t)\r\n}).add;\r\n\r\n)\r\n(\r\nr=Routine({\r\n\tvar number, divisor;\r\n\t//the list of prime numbers\r\n\tl=Array.newClear(indexedSize: 0);\r\n\r\n\tfor (2, ~max,{arg number;\r\n\t\tx=Synth(\\gsaw, [\\g, number]); //play the number\r\n\t\tdivisor=2;\r\n\t\tp=true;\t\t//prime number?\r\n\t\twhile ({((divisor <= (number.sqrt)).and(p == true))},\r\n\t\t\t{\t\r\n\t\t\t\ty=Synth(\\dsaw, [\\d, divisor, \\g, number]); //play the divisor\r\n\t\t\t\tz=Synth(\\fm, [\\m, divisor, \\c, number]); //play the fm sound of number and divisor\r\n\t\t\t\t1.wait; //1 second should be enough to hear if a sound is harmonic\r\n\t\t\t\t\t\t\t// harmonic sound means: not a prime number\r\n\t\t\t\ty.free; \r\n\t\t\t\tz.free; \r\n\t\t\t\tif(  number.mod(divisor) == 0, {p = false} ); //not a prime number\r\n\t\t\t\tdivisor = divisor + 1;\r\n\t\t\t}\r\n\t\t);\r\n\t\tx.free;\r\n\t\tif (p == true){\t\t//primenumber!\r\n\t\t\t\t\tl=l.add(number); //add primenumber to the list\r\n\t\t\t\t\tfor (1, l.size, {\targ i;  //play the list of prime numbers\r\n\t\t\t\t\t\t\tz=Synth(\\fm, [\\m, l.size, \\c, l[i]]);\r\n\t\t\t\t\t\t\t0.1.wait;\r\n\t\t\t\t\t\t\tz.free;\r\n\t\t\t\t\t});\r\n\t\t}\r\n\r\n\t});\r\n});\r\n)\r\nr.play;",
   "labels" : [
      "fm",
      "prime numbers"
   ],
   "description" : "The search for prime numbers from 1 to 100 is represented by sound. Channel 1: the numbers, channel 0: the divisors. In the center: a fm-sound with a carrier that is related to the number and a modulator that is related to the divisor. A harmonic sound means the carrier is a multiple of the modulator, so the number is a multiple of the divisor. Therefor a harmonic sound means: not a prime number. When a new prime number is encountered, the list of prime numbers is played.",
   "ancestor_list" : [],
   "author" : "henklass",
   "name" : "The quest for the prime numbers"
}
