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
#2
Well, you're trying to make a table with columns that are all 4 entries long. If you made a table of the list index of each number in your list of primes, it would look like this:
0   4   8
1   5   8
2   6   10
3   7   11
If you found a linear equation for the numbers in each resulting row, you could display them like that. For example, the 0, 4, 8... row has an equation y = 4*x, which you could use to get each number that will be in the top row.

Hope this helps!
Reply
#3
I'm actually trying to make a table way bigger than 4x4. I was just using that as an example to show that I want the list to print in order down the column(top to bottom, not across the row(left to right). I'm really confused on how to print in order vertically and this is all I could come up with.

def findPrimes(n):
    """ Adds the calculated prime numbers to a list. """
    prime_list = [1, 2]

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

    return prime_list

def makeTable(n, col = 15):
    """ Displays the prime_list through rows and columns. """
    prime_list = findPrimes(n)
    table_str = ""
    
    for index, item in enumerate(prime_list):
        table_str += ("%6d" % item) + "\t"
        if(index + 1) % col == 0:
            table_str += "\n"

    return table_str

# main program
findPrimes(4027)
makeTable(4027)

print(makeTable(4027))

# output statistics
print("\nThe number of prime numbers in the range is: " + str(len(findPrimes(4027))))
It lines up perfectly, but prints the list in order "left to right", and I'm trying to print it in order "vertically" a certain amount of rows before it moves all the way up to the next column, if you get me.

I'm sorry, but I'm confused on what you were trying to say above. I guess I'm dumb ':(
Reply
#4
Quote:
# main program
findPrimes(4027)

Why do you call that at all, if you don't do anything with the result? makeTable() calls it all by itself to get the data, so calling it before makeTable() serves no purpose other than to slow your testing down.

Also, to make your testing even faster, focus on just the part you need help with... the table.
# 4 columns, 5 rows
items = list(range(5 * 4))

print(items)
pretty_print(items) # FIXME
Focusing on just the table means that it'll run lightning fast, while also it'll be easier for random people to help out.
Reply
#5
Quote:Why do you call that at all, if you don't do anything with the result? makeTable() calls it all by itself to get the data, so calling it before makeTable() serves no purpose other than to slow your testing down.

Sorry, I'm fairly new to programming.

Here's what I have so far and it works 100%, but this is from reading online about printing rows and columns and a lot of it is too advanced for what I'm aloud to use for my homework. I modified it to match my program, but again, it's too advanced.

I have no idea how to write the displayPrimes() function in the simplest of terms.

def findPrimes(n):
    """ Return a list of prime numbers between 1 to n. """
    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(number, rows = 50):
    table_list = [[] for _ in range(rows)]
    primes = findPrimes(number)
    
    for index, item in enumerate(primes):
        row_index = index % rows
        table_list[row_index].append("%6d" % item)

    table_str = "\n".join(["\t".join(i) for i in table_list])
    
    return table_str

# main program
print(displayPrimes(4027))

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

     2     233     547     877    1229    1597    1993    2371    2749    3187    3581    4001
     3     239     557     881    1231    1601    1997    2377    2753    3191    3583    4003
     5     241     563     883    1237    1607    1999    2381    2767    3203    3593    4007
     7     251     569     887    1249    1609    2003    2383    2777    3209    3607    4013
    11     257     571     907    1259    1613    2011    2389    2789    3217    3613    4019
    13     263     577     911    1277    1619    2017    2393    2791    3221    3617    4021
    17     269     587     919    1279    1621    2027    2399    2797    3229    3623
    19     271     593     929    1283    1627    2029    2411    2801    3251    3631
    23     277     599     937    1289    1637    2039    2417    2803    3253    3637
    29     281     601     941    1291    1657    2053    2423    2819    3257    3643
    31     283     607     947    1297    1663    2063    2437    2833    3259    3659
    37     293     613     953    1301    1667    2069    2441    2837    3271    3671
    41     307     617     967    1303    1669    2081    2447    2843    3299    3673
    43     311     619     971    1307    1693    2083    2459    2851    3301    3677
    47     313     631     977    1319    1697    2087    2467    2857    3307    3691
    53     317     641     983    1321    1699    2089    2473    2861    3313    3697
    59     331     643     991    1327    1709    2099    2477    2879    3319    3701
    61     337     647     997    1361    1721    2111    2503    2887    3323    3709
    67     347     653    1009    1367    1723    2113    2521    2897    3329    3719
    71     349     659    1013    1373    1733    2129    2531    2903    3331    3727
    73     353     661    1019    1381    1741    2131    2539    2909    3343    3733
    79     359     673    1021    1399    1747    2137    2543    2917    3347    3739
    83     367     677    1031    1409    1753    2141    2549    2927    3359    3761
    89     373     683    1033    1423    1759    2143    2551    2939    3361    3767
    97     379     691    1039    1427    1777    2153    2557    2953    3371    3769
   101     383     701    1049    1429    1783    2161    2579    2957    3373    3779
   103     389     709    1051    1433    1787    2179    2591    2963    3389    3793
   107     397     719    1061    1439    1789    2203    2593    2969    3391    3797
   109     401     727    1063    1447    1801    2207    2609    2971    3407    3803
   113     409     733    1069    1451    1811    2213    2617    2999    3413    3821
   127     419     739    1087    1453    1823    2221    2621    3001    3433    3823
   131     421     743    1091    1459    1831    2237    2633    3011    3449    3833
   137     431     751    1093    1471    1847    2239    2647    3019    3457    3847
   139     433     757    1097    1481    1861    2243    2657    3023    3461    3851
   149     439     761    1103    1483    1867    2251    2659    3037    3463    3853
   151     443     769    1109    1487    1871    2267    2663    3041    3467    3863
   157     449     773    1117    1489    1873    2269    2671    3049    3469    3877
   163     457     787    1123    1493    1877    2273    2677    3061    3491    3881
   167     461     797    1129    1499    1879    2281    2683    3067    3499    3889
   173     463     809    1151    1511    1889    2287    2687    3079    3511    3907
   179     467     811    1153    1523    1901    2293    2689    3083    3517    3911
   181     479     821    1163    1531    1907    2297    2693    3089    3527    3917
   191     487     823    1171    1543    1913    2309    2699    3109    3529    3919
   193     491     827    1181    1549    1931    2311    2707    3119    3533    3923
   197     499     829    1187    1553    1933    2333    2711    3121    3539    3929
   199     503     839    1193    1559    1949    2339    2713    3137    3541    3931
   211     509     853    1201    1567    1951    2341    2719    3163    3547    3943
   223     521     857    1213    1571    1973    2347    2729    3167    3557    3947
   227     523     859    1217    1579    1979    2351    2731    3169    3559    3967
   229     541     863    1223    1583    1987    2357    2741    3181    3571    3989

The number of prime numbers in the range is: 556
This is the output from the above code. I have no idea how to do this in simpler terms.
Reply
#6
Actually, I just figured it out, but need one thing. The last column isn't full going down the rows. I need an if statement before I print the numbers to make sure I still print out the rest of the numbers even if the column won't be full.

def findPrimes(n):
    """ Return a list of prime numbers between 1 to n. """
    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 displayTable(n):
    """ Makes a table of rows and columns to display a list """
    primes = findPrimes(n)
    rows = 56
    columns = 10

    for row in range(rows):
        for col in range(columns):
            print(primes[row + 56 * col], "\t", end = "")
        print()

displayTable(4027)

# statistics
print("\nThe number of primes in the given range is: " + str(len(findPrimes(4027))))
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Displaying list correspond to the column number danlopek14q 9 3,936 Aug-27-2021, 04:32 AM
Last Post: naughtyCat
  sorted list not displaying Crackity 6 5,036 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