Change Contents of the Bubble
Welcome to CS1315. Click on the python to add comments.

This page removed for FERPA compliance
View this PageEdit this Page (locked)Uploads to this PageHistory of this PageHomeRecent ChangesSearchHelp Guide

PrimePalindromes


import os

def primepalindromes(upper):
samplesfile=open("primes.txt","wt")
primestring="Palindromic Primes between 1 and "+str(upper)+"\n"
for n in range(1,upper,2):
printNow(n)
if ((len(str(n)))%2)>0 and palindromecheck(n)==1 and primecheck(n)==1:
primestring=primestring+str(n)+"\n"
samplesfile.write(primestring)
samplesfile.close()

def primecheck(x):
for n in range (2,(int(sqrt(x)))+1):
if x%n==0:
return 0
return 1

def palindromecheck(x):
numstring=str(x)
if numstring==mirrorstring(x):
return 1
return 0

def mirrorstring(string):
string=str(string)
mirrorpoint= int(len(string)/2)
mstring= string[0:mirrorpoint]
reversestring= string[mirrorpoint-1::-1]
return mstring +string[mirrorpoint]+ reversestring



Output:

Palindromic Primes between 1 and 100000
101
131
151
181
191
313
353
373
383
727
757
787
797
919
929
10301
10501
10601
11311
11411
12421
12721
12821
13331
13831
13931
14341
14741
15451
15551
16061
16361
16561
16661
17471
17971
18181
18481
19391
19891
19991
30103
30203
30403
30703
30803
31013
31513
32323
32423
33533
34543
34843
35053
35153
35353
35753
36263
36563
37273
37573
38083
38183
38783
39293
70207
70507
70607
71317
71917
72227
72727
73037
73237
73637
74047
74747
75557
76367
76667
77377
77477
77977
78487
78787
78887
79397
79697
79997
90709
91019
93139
93239
93739
94049
94349
94649
94849
94949
95959
96269
96469
96769
97379
97579
97879
98389
98689

Student549

Link to this Page