Forums > MaxMSP

# object for deciding wether input is odd or prime number

September 23, 2010 | 7:08 pm

Is there an objetc that sends out 0 or 1 wether input is odd or prime?

September 23, 2010 | 7:26 pm

Do you mean odd or even?

– Pasted Max Patch, click to expand. –

September 23, 2010 | 7:32 pm

odd/even is dead easy: mask low order bit is one way, or do an integer divide by two and multiply back to see if you get the same number. Tim’s presumably using one of these (haven’t looked at his patch yet).

Prime isn’t quite so easy, but there are a couple of objects to help. If you check out MaxObjects.com (try < http://www.maxobjects.com/?request=prime>) you’ll find some objects that ought to suit your purposes. You may need to patch a bit around them to get what you need.

September 23, 2010 | 8:07 pm

Never mind, my answer is the same as the patch above:

[% 2] gives 0 for odd, 1 for even numbers.

September 23, 2010 | 8:17 pm

Yeah, modulo is in my post. Here’s a pointless way of doing it manually because I’m bored :)

expr ((((\$i1+1)/2)*2)-\$i1) – pretty much what Peter said

0 even
1 odd

September 23, 2010 | 9:13 pm

found this .js on the forum, can’t recall who. brute-force way of determining primes (spits out factors), still plenty fast:

function msg_int(v)
{
while (v%2==0)
{
outlet(0,2);
v/=2;
}
var i=3;
while (i*i
{
if (v%i==0)
{
v/=i;
outlet(0,i);
}
else i+=2;
}
if (v>1) outlet(0,v);
}

Viewing 6 posts - 1 through 6 (of 6 total)