«The quest for the prime numbers» by henklass

on 11 Feb'13 16:59 in fmprime numbers

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.

code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
//Prime numbers
		
Server.default.boot;
(
//maximum number
~max=100;

//synthdefs: 2 filtered sawteeth for number and divisor
SynthDef ( \gsaw, {arg g;
	Out.ar(1, 
		RLPF.ar ( Saw.ar(20*g), 10000*(1-(g/~max)), 0.2, 0.2)
		)
}).add;

SynthDef ( \dsaw, {arg d, g;
	Out.ar(0,
		RLPF.ar( Saw.ar(20*d), 10000*d/sqrt(g), 0.2, 0.2)
		// the square root is important: if you search for the divisors of a number,
		// don't search beyond the square root of the number. There won't be any.
	)
}).add;

//a fm-sound with carrier 20*number and modulator 20*divisor, modulation index = 10
SynthDef (\fm, {arg m, c;
	Out.ar(0,
		Pan2.ar(SinOsc.ar(20*c+(SinOsc.ar(20*m, 0, 200*m, 0))), 0)
	)
}).add;

)
(
r=Routine({
	var number, divisor;
	//the list of prime numbers
	l=Array.newClear(indexedSize: 0);

	for (2, ~max,{arg number;
		x=Synth(\gsaw, [\g, number]); //play the number
		divisor=2;
		p=true;		//prime number?
		while ({((divisor <= (number.sqrt)).and(p == true))},
			{	
				y=Synth(\dsaw, [\d, divisor, \g, number]); //play the divisor
				z=Synth(\fm, [\m, divisor, \c, number]); //play the fm sound of number and divisor
				1.wait; //1 second should be enough to hear if a sound is harmonic
							// harmonic sound means: not a prime number
				y.free; 
				z.free; 
				if(  number.mod(divisor) == 0, {p = false} ); //not a prime number
				divisor = divisor + 1;
			}
		);
		x.free;
		if (p == true){		//primenumber!
					l=l.add(number); //add primenumber to the list
					for (1, l.size, {	arg i;  //play the list of prime numbers
							z=Synth(\fm, [\m, l.size, \c, l[i]]);
							0.1.wait;
							z.free;
					});
		}

	});
});
)
r.play;
raw 1711 chars (focus & ctrl+a+c to copy)
reception
comments