Skip to content

First part creates an array, "prime", of boolean variables, such that running "prime[n]" will return true if n is prime, for any n less than a pre-specified maximum. Second part of code creates function to find prime factorization of any n less than the pre-specified maximum.

Notifications You must be signed in to change notification settings

LiuJoyceC/prime-numbers-array-javascript

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 

Repository files navigation

prime-numbers-array-javascript

Description: First part creates an array, "prime", of boolean variables, such that "prime[n]" will return true if n is prime, for any n less than a pre-specified maximum. Second part of code creates function to find prime factorization of any n less than the pre-specified maximum. The advantage of creating an array (instead of a function that just determines if one input number is prime) is that you now have a stored database of prime numbers and will not need to call the function for each new n. This array comes in handy for the prime factorization function.

Background: I wrote this program on my second day of learning JavaScript after learning the syntax of a for loop from the intro course on Codecademy. I came up with this method of creating a list of prime numbers when I was in middle school (using pencil and paper and crossing off numbers that were not prime), but this is the first time I have implemented it in a computer program. It was exciting to watch my computer find all the prime numbers under a 1,000,000 in just a fifth of a second, compared to my childhood pencil-and-paper method that took several minutes to find all the prime numbers under just 500! For this code, I did have to google up a few functions that weren't covered in the Codecademy intro course. The second part of the code uses a recursive function, which is not covered in the Codecademy course, either, so if you are interested in learning recursion, I recommend the Intro to Computer Science Class on Udacity (taught in Python, but don't worry, no prior knowledge is assumed).

Method: This was a method I thought up in middle school long before I had taken any Number Theory courses that use modular mathematics to more efficiently determine if a number is prime, so bear with me if it seems a bit rudimentary :) The idea behind this method is that you can determine if a number n is prime as long as you have a list of all the prime numbers that are less than or equal to the square root of n (you do not need to know any prime numbers greater than the square root, because if n is divisible by any prime number less than itself but greater than its square root, then it must be divisble by some prime number that is less than its square root). So then to create a list of prime numbers up to a specified maximum, you get a list of numbers from 2 to that max, and you circle the first number, 2, which is your first prime. Then starting at the square of 2, you start crossing off all numbers divisble by 2 until you reach the max. Then circle the next number that has not been crossed off (3), which is your next prime. Again, start at the square of 3 and cross off all numbers divisble by 3. After that, the next number that has not yet been crossed off is 5, so you repeat the process starting at the square of 5. Starting the cross-off at the square saves a little time because we already know any non-prime between 5 and 25 have already been crossed off when we went through 2 and 3. Note that we also don't have to go through and cross off multiples of any number that isn't prime, because they'll have already been crossed off by the prime factors of that number. The process gets faster as the prime numbers get bigger, since larger prime numbers are spaced farther apart from each other than smaller prime numbers. You can actually stop this process once you've circled any prime number greater than the square root of your pre-determined maximum, because you already know that any number between the max and the square root of the max that hasn't been crossed off yet is prime.

About

First part creates an array, "prime", of boolean variables, such that running "prime[n]" will return true if n is prime, for any n less than a pre-specified maximum. Second part of code creates function to find prime factorization of any n less than the pre-specified maximum.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published