Topic:   [Processing] Prime Numbers   (Read 5765 times)


0 Members and 1 Guest are viewing this topic.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
[Processing] Prime Numbers
« on: November 20, 2012, 06:33:31 PM »
This is a nice, compact program based on one I wrote in class my senior year in high school.  It finds all prime numbers between 1 and "cap". The assignment stated that we had to optimize the program as much as we could, and the instructor would tell us if he saw our program and knew there was something we could improve. We were only required to make it test one number; the optimization test was we were given a few very large numbers and we saw how long it took to determine that they were prime.

A few notes:
 - It does not bother checking even numbers.
 - The only way the computer can detect a prime number is by checking all possible factors.
 - Modulus returns the remainder of the division of two numbers, and is used to check whether a number is a factor.
 - sqrt() is used because there cannot be any factors larger than the square root of the number . This greatly optimizes the program for checking very large numbers.
 - Processing does not want to use sqrt() with a double, meaning I cannot currently speed-test this with particularly large prime number.

Code: [Select]
int n;
int i;
int cap = 100;
boolean prime;

void setup() {
  for (n = 3; n < cap; n += 2){
    prime = true;
    for (i = 3; i < sqrt(float(n+1)); i += 2){
      if (n % i == 0) {
        prime = false;
        break;
      }
    }
      if (prime == true)
        println(n);
  }
  println("Done.");
}
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: [Processing] Prime Numbers
« Reply #1 on: November 20, 2012, 07:40:32 PM »
Oooh fancy. The higher the number, the longer it takes to determine it's prime-ness.

Circuit


  • GMG-er

  • **


  • Posts: 299

  • blast from the past
Re: [Processing] Prime Numbers
« Reply #2 on: November 22, 2012, 09:26:08 PM »
Cool, man.  I recently made a similar program in C.  This method of detecting primes is really slow, and not well-suited for finding primes in sequence.  I left my program running for a couple of days and found all of the primes up to 2^31, and saved them to a file... unfortunately, when it was finally done, the file was so big that I couldn't open it.  ::)

Another method that you can use is the Sieve of Eratosthenes.  It behaves opposite of trial division: it gets faster the higher it goes, because it gradually eliminates non-primes by marking off all multiples of known primes.  This involves recording the status of every number (prime or not prime) beginning at 2, so it requires a lot of RAM.

Connors


  • ^ This guy is amazing.

  • ****


  • Posts: 2374

  • It's a secret to everyone...
Re: [Processing] Prime Numbers
« Reply #3 on: December 16, 2012, 07:31:03 PM »
That does sound interesting, how big does a file have to be that it won't open? :L
Warning: The above post may have been modified multiple times.

"In a great game, the character must never perfectly obey the user's command"
 - Tim Rogers

http://connorspuzzles.tumblr.com/

Gan


  • Administrator

  • ^ This guy is amazing.

  • *****


  • Posts: 4411
Re: [Processing] Prime Numbers
« Reply #4 on: December 16, 2012, 08:58:53 PM »
I'm pretty sure it depends on the text program. If the text program tries to buffer the whole text file, gonna fill up fast.

FrankJScott


  • GMG-er

  • **

  • no avatar

  • Posts: 431

  • Gan is so awesome.
Cool Product Info
« Reply #5 on: March 04, 2024, 11:28:48 AM »
Please try Google before asking about Great Product Site 1b4cbe0

FrankJScott


  • GMG-er

  • **

  • no avatar

  • Posts: 431

  • Gan is so awesome.
Updated Sylfirm X Sarasota Florida Site
« Reply #6 on: March 11, 2024, 10:11:41 PM »
For the lady asking about softwave therapy for face near me, radio frequency skin treatment near me, thermage flx near me, promax lipo near me, profound skin treatment near me, ultrasound therapy for wrinkles near me, j plasma renuvion near me, thermage flx treatment near me,  I highly recommend this <a href=https://dominic5e11lsz9.wiki-cms.com/user>moved here for Stomach Tightening Sarasota link</a> or face tightening near me, ultracel q+ near me, tempsure near me, laser neck tightening near me, rf microneedling near me cost, profound rf microneedling near me, rf therapy near me, lutronic genius rf microneedling near me, and don't forget this <a href=https://aiden2n75ywx0.life3dblog.com/profile>high rated Downtown Sarasota RadioFrequency Microneedling info</a> and don't forget non surgical skin tightening stomach near me, skin tightening laser treatment near me, tempsure firm near me, fibroblast plasma skin tightening near me, ultherapy skin tightening near me, rf microneedling near me cost, fibroblast plasma near me, thermage skin tightening near me, alongside all this <a href=https://gabriel3a00hos6.blogvivi.com/profile>read what he said on RF Microneedling Sarasota forum</a> which is also great. Also, have a look at this <a href=https://cameron9q88cjq7.national-wiki.com/user>go here on Radiofrequency microneedling near me forum</a> alongside all endymed skin tightening near me, thermage cpt near me, radio frequency skin tightening treatment near me, fibroblast therapy near me, infini rf microneedling near me, sofwave near me, plasma face lift near me, ultracel q+ near me, not to mention this <a href=http://vb.lo0o0d.com/member.php?u=44989>high rated Radiofrequency microneedling near me url</a> with morpheus8 rf microneedling near me, morpheus radiofrequency microneedling near me, plasma neck lift near me, thermage flx treatment near me, radio frequency facial treatment near me, ultherapy cost near me, fractionated rf near me, necktite near me,  for good measure. Check more @ <a href=https://ekvall.co/showthread.php?tid=1607&pid=157312#pid157312>Top Rated Digital Marketing Site</a> 1960b21

FrankJScott


  • GMG-er

  • **

  • no avatar

  • Posts: 431

  • Gan is so awesome.
Useful Product Tips
« Reply #7 on: March 21, 2024, 01:47:23 PM »
Please try Google before asking about Excellent Product Info 41960b2

FrankJScott


  • GMG-er

  • **

  • no avatar

  • Posts: 431

  • Gan is so awesome.
Useful Natural Pet Supplement Website
« Reply #8 on: March 25, 2024, 11:36:21 AM »
In response to the lady asking about Pet herbal supplements for drug detection cats, Pet herbal supplements for dogs with performance issues, Pet herbal supplements for drug detection dogs, Holistic supplements for dogs and cats, Pet allergy relief support supplements, Holistic pet herbs, Pet memory support supplements, Herbal health for dogs and cats,  I highly recommend this advice about natural pet supplement forum or Pet herbal supplements for drug detection dogs, Pet herbal supplements for dogs with digestive issues, Pet herbal supplements for cats, Pet herbal supplements for weaning kittens, Pet herbal supplements for dogs with athletic injuries, Pet herbal supplements for cats with kidney problems, Pet bone supplements, Pet herbal supplements for cats with athletic injuries, alongside all this recommended you read on natural pet supplement info not to mention Pet herbal supplements for sled dogs, Pet ear support supplements, Pet anxiety support supplements, Pet herb supplements, yeast infections in dogs, Pet herbal supplements for fussy eaters, Pet athletic support supplements, Pet herbal supplements for cats with athletic injuries, and don't forget this continued on natural pet supplement link which is also great. Also, have a look at this new natural pet supplement blog alongside all Pet vitality support supplements, Pet herbal supplements for tracking dogs, Pet herbal supplements for cats with liver problems, Pet herbal remedies, Pet herbal supplements for cats with performance issues, Pet immune system supplements, pet wellbeing, Pet herbal supplements for dogs with stress, as well as this going here on natural pet supplement link with Pet joint supplements, Pet herbal supplements for cats with mobility issues, Organic pet herbs, Pet herbal medicine, ear infections in dogs, Pet wellness with herbs, Pet herb supplements, Herbal medicine for dogs,  for good measure. Check more @ Excellent Natural Pet Supplement Blog 3ef90e7