SOLUTION: Write a program to print the following series 2 5 11 17 23 31 41 47 59 ...

Algebra ->  Sequences-and-series -> SOLUTION: Write a program to print the following series 2 5 11 17 23 31 41 47 59 ...       Log On


   



Question 1062750: Write a program to print the following series
2 5 11 17 23 31 41 47 59 ...

Found 2 solutions by rothauserc, Edwin McCravy:
Answer by rothauserc(4718) About Me  (Show Source):
You can put this solution on YOUR website!
This is a list of every second prime beginning with 2.
:
The index into the list of primes would be (2n-1) where n = 1, 2, ......k
:
You would have to set k to some integer, for example 100
:
There are plenty of examples of c or java code on the internet to generate prime numbers, store them in a matrix and then use the index (2n-1) into the matrix to print out the every second prime number list
:

Answer by Edwin McCravy(20056) About Me  (Show Source):
You can put this solution on YOUR website!
The sequence is the 1st prime, the 3rd prime, the 5th prime, etc., i.e.
every other prime.

The only language I know is LibertyBasic. Here is the program in that
language. The ' indicates a comment line.
--------------------------------------------------------------------------

           'We begin by printing the first term. The only term that's even.
           'and setting the prime count = 1

print 2;" ";
count = 1

          'Now we need try only odd numbers n from 5 to 100. That's why the
          'step 2" in the next executable line. If we want numbers higher
          'than 100, then we change the 100 to a higher number.

for n = 3 to 100 step 2

pr = 1   
          'We test each n to see if it is prime. We use 1 or 0
          'values of "pr" to indicate whether n is prime or not
          'if pr=1 it is prime, and if pr=0 it is not prime
          'We started above by setting pr=1, for we change pr to 0
          'if n is not prime. Since n is odd, we only
          'need to see if n is divisible by odd numbers,
          'which is why the "step 2" in the next line.

for k = 3 to n-1 step 2

          'the next line makes pr=0 if n is divisible by k

if n mod k = 0 then pr=0

next k

          'if n is not prime the next line skips to 1 for the next n value

if pr = 0 then 1

          'if n is prime, we increase the prime count by 1

count = count+1

          'the next line determines whether the count is even or
          'odd.  If it's odd, we print n; if even we skip to 1

if count mod 2 = 0 then 1 

print n;" ";

1 next n

print "..."

-----------------------------------------

Edwin