Notifications
Clear all

[Closed] Find a prime #?

So… i thought this would be relatively basic to do but turns out i’m a little incompetent.

This has no real practical purpose just for learning figured i should be doing something ot help expand my brain a bit.

But how would i write a script to find a prime # from 1 to whatever. I literally have nothing written i just can’t seem to grasp the concept of it. If someone could help point me in the right direction that would be great

5 Replies

You can maybe find inspiration on wikipedia, there are a lot of information on them there:

in particular this article about how to find prime number in a list:
http://en.wikipedia.org/wiki/Sieve_of_Eratosthenes

The best (and possibly only) way to find primes is to run through the numbers and see if they divisable by anything other than 1 and themselves. A good function to help with this in max is Mod. Mod returns the remainder of 2 numbers when they are divided. If the remainder is 0 then the second number is a multiple of the first. Here is a sample script, hope this is clear.

primeAr = #()  --empty array for storing primes
  for x = 2 to 100 do --count of the numbers to be checked for primality
  (
  	divisible = false --variable for storing whether or not a divisor is found
  	for y = 2 to (x-1) do --count of numbers to check against original number (1 and the number itself are not included) 
  	(
  		if (mod x y) == 0 then divisible = true --checks remainder and determines if x can be divided by y
  	)
  	if divisible == false then append PrimeAr x --if x can not be divided by y then it is a prime number
  )
  primeAr

Thanks Eric! That’s great haha, seems like a simple answer like i figured it would be.

I also found this page where someone talks about a python script to determine if a number is prime:

http://krenzel.info/?p=83

Cause it seems that trying to find if a number is prime is more useful than listing all the primes.

I also just found that if you want to print more than 20 elements of the array, you need to use this:

with printAllElements on primeAr as string

If you just write primeAr, you can only see the 20 first prime numbers.