Python Forum
Displaying a long list with Rows and Columns
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Displaying a long list with Rows and Columns
#1
Description:
"Find all the prime numbers between 1 and 4,027 and print them in a table which
"reads down", using as few rows as possible, and using as few sheets of paper
as possible. All numbers should be right-justified in their column. The height
of the columns should all be the same, except for perhaps the last column,
which might have a few blank entries towards its bottom row. After printing
the table, re-print in a second table of twin primes found among the original
prime numbers, again reading down.
Finally, print the following statistics: number of primes found and the number
of twin prime pairs found. Recall, all tables require titles."

I got the program to calculate all of the prime numbers in the range, but don't know how to display it. The numbers need to be printed in order, not from left to right, but from top to bottom, if that makes sense. I'm just printing to the console and the copy and pasting it to a Microsoft Word Document.

Like this:
1   7   19  37

2   11  23  41

3   13  29  43

5   17  31  47
Not like this:
1   2   3   5

7   11  13  17

19  23  29  31

37  41  43  47
I know I need a nested for loop, but have never done anything like this. This is my Attempt:
def findPrimes(n):
    """ Adds the calculated prime numbers to an empty list. """
    prime_list = [2]

    for number in range(3, n, 2):
        if all(number % i != 0 for i in range(2, int(number ** .5) + 1)):
            prime_list.append(number)

    return prime_list

def displayPrimes():
    for row in findPrimes(4027):
        for col in findPrimes(4027):
            display = row * col
            print(display, end = "\t")
            break

# main program
findPrimes(4027)
displayPrimes()

# output statistics
print("\n\nThe number of prime numbers in the range is: " + str(len(findPrimes(4027))))
Reply


Messages In This Thread
Displaying a long list with Rows and Columns - by ngr33n - Sep-20-2017, 10:33 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying list correspond to the column number danlopek14q 9 4,228 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  sorted list not displaying Crackity 6 5,356 Jul-18-2017, 12:50 PM
Last Post: sparkz_alot

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020