Home > front end >  How to print prime numbers in a tabular format
How to print prime numbers in a tabular format

Time:10-18

my goal is to print prime numbers in a tabular format, instead of printing one value each line. so far all my attempts have ended in either lines, or misprinted tables.

start = int(input("Start number: "))
end = int(input("End number: "))

if start < 0 or end < 0:
    print("Start and End must be positive.")
    start = int(input("Start number: "))
    end = int(input("End number: "))
if end < start:
    print("End must be greater than Start number: ")
    start = int(input("Start number: "))
    end = int(input("End number: "))

prime = True
for num in range(start,end 1):
    if num > 1:
        for i in range(2,num):
            if num % i == 0:
                break
        else:
            num = print(num)

the one i have here can only print it line by line

#start number: 1
#end number: 100
 # 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 

CodePudding user response:

This can be done with str.rjust or its friends

>>> "2".rjust(3)
'  2'
>>> 

first we gather the numbers we want to print and calculate how many characters it take the biggest of them and add one to that value, that result is the one we will use for the rjust

>>> nums=[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]
>>> j = len(str(max(nums)))   1
>>> 

now we pick how many we want to print per line

>>> linesize = 10
>>> 

and finally we make use of print keyword-only arguments end to control when to print in the same line or not and enumerate to control how many we have already printed

>>> for i,p in enumerate(nums,1):
        print( str(p).rjust(j), end="" )
        if i%linesize==0:
            print() #to go to the next line

        
  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
>>> 

CodePudding user response:

You could use str.format and implement a reusable solution using a generator:

from math import floor

def tabular(records, line_width=42, sep_space=3):
    
    width = len(str(max(records)))   sep_space
    columns = floor(line_width/width)
    
    for i in range(0, len(records), columns):

        row_records = records[i:i columns]
        row_format = ("{:>"   str(width)   "}") * len(row_records)

        yield row_format.format(*row_records)
# test data / prime numbers
numbers = [
    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
]

for row in tabular(numbers):
    print(row)

#    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

Example with some other numbers:

for row in tabular(list(range(0, 1600, 50)), 79, 2):
    print(row)

#     0    50   100   150   200   250   300   350   400   450   500   550   600
#   650   700   750   800   850   900   950  1000  1050  1100  1150  1200  1250
#  1300  1350  1400  1450  1500  1550

Example with str.format but without using a generator:

# test data / prime numbers
numbers = [
    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 = len(str(max(numbers)))   3

for i in range(0, len(numbers), 10):

    row_records = numbers[i:i 10]
    row_format = ("{:>"   width   "}") * len(row_records)
    print(row_format.format(*row_records))

#    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
  • Related