Skip to content

Commit

Permalink
Merge pull request #592 from laraPPr/update_python_example_scripts
Browse files Browse the repository at this point in the history
HPC tutorial: update python scripts
  • Loading branch information
boegel authored Jan 5, 2024
2 parents a910b69 + 9f11040 commit 69617b2
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 23 deletions.
14 changes: 7 additions & 7 deletions intro-HPC/examples/Program-examples/01_Python/file3.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
#
def primes(top):
result = []
for n in range(2, top):
for x in range(2, n/2+1):
for n in range(2, top+1):
for x in range(2, n):
if n % x == 0:
break
else:
Expand All @@ -33,11 +33,11 @@ def primes(top):
#
scratch_dir = os.environ.get('VSC_SCRATCH')
filename_p1 = scratch_dir + "/primes_1.txt"
print "Output File: ", filename_p1
print("Output File: ", filename_p1)
f_out = open(filename_p1, 'w+')
for i in range(1, 30000):
# We take a random integer between 1 and 2000
top = random.randrange(2000)
top = random.randrange(2,2000)
# and we calculate all primes up to that limit
l = primes(top)
f_out.write('TOP=')
Expand All @@ -60,8 +60,8 @@ def primes(top):
filename_p2 = scratch_dir + "/primes_2.txt"
f_in=open(filename_p1, "r")
f_out=open(filename_p2, "w")
print "Input File: ", filename_p1
print "Output File: ", filename_p2
print("Input File: ", filename_p1)
print("Output File: ", filename_p2)
in_lines=f_in.readlines() #reads it line by line
for line in in_lines:
delim = line.find("[")
Expand All @@ -82,5 +82,5 @@ def primes(top):

end_time = int(time.time())
duration = end_time - start_time
print "Duration = " + str(duration) + " seconds."
print("Duration = " + str(duration) + " seconds.")

14 changes: 7 additions & 7 deletions intro-HPC/examples/Program-examples/01_Python/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ def ask_int(prompt, retries=3):
if num > 1:
return num
else:
print "Please enter an integer which is bigger as one."
print('Please enter an integer which is bigger as one.')
retries = retries - 1
if retries <= 0:
print('Too many attempts, try again later.')
Expand All @@ -27,23 +27,23 @@ def ask_int(prompt, retries=3):
#
# PRIMES
#
print "This program calculates all primes between 1 and your upper limit."
print('This program calculates all primes between 1 and your upper limit.')
num = ask_int('Enter your upper limit (>1): ')
start_time = print_time("Start Time: ")
print "[Prime#1] = 1"
print('[Prime#1] = 1')
ctr = 1
for n in range(2, int(num)):
for x in range(2, n/2+1):
for n in range(2, int(num)+1):
for x in range(2, n):
if n % x == 0:
break
else:
# loop fell through without finding a factor
ctr += 1
print "[Prime#%i] = %i" % (ctr,n)
print('[Prime#%i] = %i' % (ctr,n))
end_time = print_time("End Time: ")
duration = end_time - start_time
s = "Duration: " + str(duration) + " seconds."
print s
print(s)



2 changes: 1 addition & 1 deletion intro-HPC/examples/Running-interactive-jobs/message.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def print_cowsay(cowsay):
||----w | \n\
|| ||\
"
print cowtxt
print(cowtxt)

# X-Message
cmd = "xmessage -buttons yes:1,no:0 -center -timeout 60 \" Do you want to see a cow? \""
Expand Down
16 changes: 8 additions & 8 deletions intro-HPC/examples/Running-interactive-jobs/primes.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
def print_time(str):
now = time.time()
st = datetime.datetime.fromtimestamp(now).strftime('%Y-%m-%d %H:%M:%S')
print str, st
print(str, st)
return int(now)

def ask_int(prompt, retries=3):
Expand All @@ -21,27 +21,27 @@ def ask_int(prompt, retries=3):
if num > 1:
return num
else:
print "Please enter an integer which is bigger as one."
print("Please enter an integer which is bigger as one.")
retries = retries - 1
if retries <= 0:
print('Too many attempts, try again later.')
sys.exit()

# PRIMES
print "This program calculates all primes between 1 and your upper limit."
print("This program calculates all primes between 1 and your upper limit.")
num = ask_int('Enter your upper limit (>1): ')
start_time = print_time("Start Time: ")
print "[Prime#1] = 1"
print("[Prime#1] = 1")
ctr = 1
for n in range(2, int(num)):
for x in range(2, n/2+1):
for n in range(2, int(num)+1):
for x in range(2, n):
if n % x == 0:
break
else:
# loop fell through without finding a factor
ctr += 1
print "[Prime#%i] = %i" % (ctr,n)
print("[Prime#%i] = %i" % (ctr,n))
end_time = print_time("End Time: ")
duration = end_time - start_time
s = "Duration: " + str(duration) + " seconds."
print s
print(s)

0 comments on commit 69617b2

Please sign in to comment.