Prime numbers code

Posted By on Feb 22, 2018 | 0 comments


Here is some Python code that uses Emily’s approach to find all the primes up to a specified number.

  • Set ‘max’ to an integer up to which you want to find prime numbers.
  • Click run.
  • The code returns: a list of primes; the number of primes calculated from each oddish sequence; and the number primes up to ‘max’.

The method is similar to the Sieve of Eratosthenes, but it only marks each non-prime once and applies just the minimum number of primes.

The second sequence returned by the code (the number of primes calculated from each oddish sequence) has the OEIS reference A050216 and contains the numbers behind Brocard’s conjecture (as yet unproven).

An explanation of each line of the code:

1.    ‘max’ is the integer up to which the primes will be calculated.
2.   ‘number_line’ is an array to represent the number line.
3.   ‘primes_list’ is an array of the primes seeded with the first prime (2).
4.   ‘primes_counted’ is an array of the number of primes calculated from each oddish sequence.
5.   ‘p_count’ is a counter for marking positions of non-primes.
6.   A loop to find all positions in number line with 2 as a factor:
7.    – Marking each as non-prime;
8.    – Incrementing the prime counter by the prime (2).
9.   ‘0_count’ is a counter for the oddish sequence being worked on.
10.  ‘nl_count’ is a counter for the position in the number line.
11.  A loop to use the next oddish sequence until ‘max’ is reached.
12.  ‘cp-count’ is a counter for the number of primes that are calculated.
13.  A loop to move along the number line until end of oddish sequence (P x P).
14.  Break from loop before max is reached.
15.   [break]
16.  Increment the number line counter.
17.  If current position in the number line is prime:
18.   – Add number to the list of primes;
19.   – Increment the calculated primes counter.
20.  Reset the counter for marking positions of non-primes.
21.  A loop to find all positions in the number line with the prime as a factor:
22.   – Marking each as non-prime;
23.   – Incrementing the prime counter by the prime;
24.   – Record the number of primes calculated by the oddish sequence;
25.   – Increment the oddish sequence counter.
26.  Add a count for the first prime (2) as it was seeded separately.
27.  Print the prime numbers calculated.
28.  Print the number of primes calculated by each oddish sequence.
29.  Print total number of primes calculated.

 

Submit a Comment

Your e-mail address will not be published. Required fields are marked *