Difference between revisions of "Rocky Python Prime"

From NIMBioS
(Created page with "= Python Code = '''prime.py''' <pre> MIN = 2 MAX = 100000 def is_prime(num): if num <= 1: return False else: for i in range(2, num): if (num % i) == 0: return False return True for i in range(MIN, MAX+1): if is_prime(i): print(i) </pre> = Batch Script = '''python-prime.run''' <pre> #SBATCH --job-name=PYTHON_PRIME #SBATCH --output=python_prime_%j.out module load Python python3 prime.py </pre> = Running Job = <pre> [test_user@rocky7 prime]$ pw...")
 
m
 
(4 intermediate revisions by the same user not shown)
Line 8: Line 8:


def is_prime(num):
def is_prime(num):
if num <= 1:
    if num <= 1:
return False
        return False
else:
    else:
for i in range(2, num):
        for i in range(2, num):
if (num % i) == 0:
            if (num % i) == 0:
return False
                return False
return True
    return True




for i in range(MIN, MAX+1):
for i in range(MIN, MAX+1):
if is_prime(i):
    if is_prime(i):
print(i)
        print(i)
</pre>
</pre>


Line 27: Line 27:
'''python-prime.run'''
'''python-prime.run'''
<pre>
<pre>
#!/bin/bash
#SBATCH --job-name=PYTHON_PRIME
#SBATCH --job-name=PYTHON_PRIME
#SBATCH --output=python_prime_%j.out
#SBATCH --output=python_prime_%j.out
Line 41: Line 42:
[test_user@rocky7 prime]$ pwd
[test_user@rocky7 prime]$ pwd
/home/test_user/projects/python/prime
/home/test_user/projects/python/prime
</pre>
 
<pre>
[test_user@rocky7 prime]$ ls
[test_user@rocky7 prime]$ ls
prime.py  python-prime.run
prime.py  python-prime.run
</pre>
 
<pre>
[test_user@rocky7 prime]$ sbatch python-prime.run  
[test_user@rocky7 prime]$ sbatch python-prime.run  
Submitted batch job 3877
Submitted batch job 3877

Latest revision as of 02:15, 29 April 2023

Python Code

prime.py

MIN = 2
MAX = 100000


def is_prime(num):
    if num <= 1:
        return False
    else:
        for i in range(2, num):
            if (num % i) == 0:
                return False
    return True


for i in range(MIN, MAX+1):
    if is_prime(i):
        print(i)


Batch Script

python-prime.run

#!/bin/bash
#SBATCH --job-name=PYTHON_PRIME
#SBATCH --output=python_prime_%j.out

module load Python

python3 prime.py


Running Job

[test_user@rocky7 prime]$ pwd
/home/test_user/projects/python/prime

[test_user@rocky7 prime]$ ls
prime.py  python-prime.run

[test_user@rocky7 prime]$ sbatch python-prime.run 
Submitted batch job 3877


Here we can see the job was assigned to moose1.

[test_user@rocky7 prime]$ squeue
             JOBID PARTITION       NAME      USER ST       TIME  NODES NODELIST(REASON)
              3877 compute_all PYTHON_P test_user  R       0:02      1 moose1


Once job is no longer listed in the queue:

[test_user@rocky7 prime]$ ls
prime.py  python-prime.run  python_prime_3877.out
[test_user@rocky7 prime]$ cat python_prime_3877.out 
2
3
5
7
11
...<truncated>...
99881
99901
99907
99923
99929
99961
99971
99989
99991