# Teletype: Generating prime numbers

To preface, I’m new to Teletype — having only owned it for a week now and I’ve run into a bit of a wall trying to generate a list of prime numbers in the patterns section. I know that this is probably (most likely) CPU intensive, but I just wanted to try it as a proof-of-concept. If you see the problem and ask yourself, “why are you doing [this]?” then the answer is, most likely, “I have no idea!”

I’ve been staring at Teletype’s syntax for too long and I’m starting to get confused as to what the previous script referenced. Also, I’m not sure if you can nest commands, so I’m using new scripts for each loop. In the code, I’m notating the obvious, but it will help me explain better. Here are my scripts:

#3
` L 0 3: P. N I; \$ 4`
// Set the working pattern 0-3

#4
` L 0 7: T RRND 2 25; Z I; \$ 5`
// Generate random numbers for each index (to variable T)

#5
`X 2 `
`W <= * X X T: \$ 6`
// Run script 6 while X * X is <= T

#6
`IF == % T X 0: X + X 1`
// Check if modulus of T and X is 0. If true, then increment X

`[I feel like I'm missing a recursion here]`

`ELSE: P Z T`
// Add prime number to working pattern (set by script 3)

1 Like

i can’t quite grasp the full extent of how this works but i think `BREAK` operator might be a good tool. so you could do stuff like:

``IF != % T X 0: BREAK``

I had initially placed `BRK` after the X increment, but then it was breaking the whole process every time I hit the first composite number. I’ve put it up for now, but I’ll try it again tomorrow. I appreciate any further suggestions.

Instead of generating a random value in script 4, you can add another layer of looping to generate potential candidates (it’s gonna be slow but I don’t think efficiency is the goal).

At a high level it could look like:

``````For each pattern 0-4:
- For each position 0-7:
Set X to last prime found
Y 1
- While Y:
J 1; Z 1
- While J AND Z squared less than X:
If Z goes into X: J 0
If J: P.PUSH X; Y 0``````

slippery slope to teletype crypto mining here

in seriousness though, if primes are musically interesting we could include a lookup table in the firmware (though i understand this is an exercise)

8 Likes

I’m not sure if they are with V/OCT (because I can’t get it to work ), but a couple of years ago I was writing an app in Swift as a proof-of-concept of a proposed module that generated various sine waves tuned, in Hz, to numbers such as prime, composite, triangular, square, Fibonacci, etc. I found the results surprising. I’ll see if I can find the old mock-ups on the Intellijel forums.1

I saw that @scanner_darkly @a773 is working on a Hz feature so this would come in handy, and now that I know of Teletype then there’s no need for a separate module because this one can do it all.

1 It looks like the post was deleted, but it is referenced by another user here: https://forum.intellijel.com/t/new-clock-quantiser-idea-request/1822/5. It was titled, 'Rissonance'; a portmanteau of dissonance and resonance. The 2 outputs were panned hard left and right to create binaural beats2.

2 I do not think these have a psychological benefit, per se, but they do sound interesting!

that would be @a773!

I’m not all too familiar with the various lookup tables yet, so I’m going to use `#`, just to denote a number function. The various lookup tables could include, but not be limited to :

`#.C` - Composite numbers
`#.F` - Fibonacci numbers
`#.H` - Hexagonal numbers
`#.O` - Octagonal numbers
`#.P` - Pentagonal numbers
`#.PR` - Prime numbers
`#.S` - Square numbers
`#.T` - Triangular numbers

`#.? x y z`

`x` being the nearest lower range, `y` being the nearest upper range, stop after `z` numbers.

The `z` could be useful because, with patterns, you may not want many changes, but with Hz then the case may be otherwise. Also, you probably wouldn’t have much use for 1,000 integers so the range would need to be something that wouldn’t bog down the CPU or memory. Maybe 1-100 or less?

So:

`#.C 5 99 5` would output five composite numbers: `{4, 6, 8, 9, 10}`.

`x` is 5. Since 5 falls equally between 4 and 6 the in lookup table of composite numbers,
`#.C`, then a `TOSS` function could determine which number it begins with. In this case, it chose to start on the left side of 5 and begin with 4. This is rudimentary when talking about composite numbers, but could be much more handy when you get into, say, Fibonacci numbers that could vary greatly in range.

2 Likes