I am trying to get the output of the following code:
for x in range(1,100):
if x==2:
print(x)
else:
for i in range (2,x):
if x%i==0:
break
elif x%i!=0:
if i==(x-1):
print(x)
with an output as following:
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
If you have a prime, you could use '%4s' % prime
to right-align the prime in 4 characters (you might choose another width, or adapt it depending on your range) :
primes = [2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41,
43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97]
width = 4
cell_format = '%'+str(width)+'s'
cells = 10
for i,p in enumerate(primes):
if i % 10 == 0:
print
print cell_format % p,
It outputs :
2 3 5 7 11 13 17 19 23 29
31 37 41 43 47 53 59 61 67 71
73 79 83 89 97
count = 0
cells = 10
for x in range(1,100):
if x==2:
print('%4s' % x),
else:
for i in range (2,x):
if x%i==0:
break
elif x%i!=0:
if i==(x-1):
count += 1
if count % cells == 0:
print("")
print('%4s' % x),
count = 0
cells = 10
for x in range(1, 100):
if x == 2:
print('%4s' % x, end='')
else:
for i in range(2, x):
if x % i == 0:
break
elif x % i != 0:
if i == (x - 1):
count += 1
if count % cells == 0:
print("")
print('%4s' % x, end='')
print("")