Difference between revisions of "Primes"

From Bitnami MediaWiki
Jump to navigation Jump to search
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
−
Factor numbers in a given range:  
+
Factor numbers in a given range in a shell:  
 
<syntaxhighlight lang=bash>
 
<syntaxhighlight lang=bash>
 
for value in {2000..2021}
 
for value in {2000..2021}
 
do echo `factor $value`
 
do echo `factor $value`
 
done
 
done
 +
</syntaxhighlight>
 +
 +
Python simple prime finding program:
 +
<syntaxhighlight lang=python>
 +
import math
 +
 +
def primes(n):
 +
    count = 3
 +
    while count < n:
 +
        isprime = True
 +
        for x in range(2, int(math.sqrt(count) + 1)):
 +
            if count % x == 0:
 +
                isprime = False
 +
                break
 +
        if isprime:
 +
            print(count)
 +
        count += 1
 
</syntaxhighlight>
 
</syntaxhighlight>

Latest revision as of 04:34, 3 June 2021

Factor numbers in a given range in a shell:

for value in {2000..2021}
do echo `factor $value`
done

Python simple prime finding program:

import math

def primes(n):
    count = 3
    while count < n:
        isprime = True
        for x in range(2, int(math.sqrt(count) + 1)):
            if count % x == 0: 
                isprime = False
                break
        if isprime:
            print(count)
        count += 1