-1

I am trying to write a program that takes a user input, limit and prints a table to visualise all the factors of each integer ranging from 1 to the limit. Each row represents an integer between 1 and limit, whilst the first row represents the number 1, the second number 2 etc. For a given position i (starting from 1) in a row n, if the i is a factor of the limit then a '*' is there, if not then '-'.

For example for the input of 20 it should produce:

Maximum number to factorise: 20
* - - - - - - - - - - - - - - - - - - - 
* * - - - - - - - - - - - - - - - - - - 
* - * - - - - - - - - - - - - - - - - - 
* * - * - - - - - - - - - - - - - - - - 
* - - - * - - - - - - - - - - - - - - - 
* * * - - * - - - - - - - - - - - - - - 
* - - - - - * - - - - - - - - - - - - - 
* * - * - - - * - - - - - - - - - - - - 
* - * - - - - - * - - - - - - - - - - - 
* * - - * - - - - * - - - - - - - - - - 
* - - - - - - - - - * - - - - - - - - - 
* * * * - * - - - - - * - - - - - - - - 
* - - - - - - - - - - - * - - - - - - - 
* * - - - - * - - - - - - * - - - - - - 
* - * - * - - - - - - - - - * - - - - - 
* * - * - - - * - - - - - - - * - - - - 
* - - - - - - - - - - - - - - - * - - - 
* * * - - * - - * - - - - - - - - * - - 
* - - - - - - - - - - - - - - - - - * - 
* * - * * - - - - * - - - - - - - - - * 

I currently have:

limit = input('Maximum number to factorise: ')
for i in range(1,int(limit)):
    line = '{:2}:'.format(i)
    for j in range (1,11):
        line += "{:4}".format(i % j)
    print(line)

But it gives:

Maximum number to factorise: 20
 1:   0   1   1   1   1   1   1   1   1   1
 2:   0   0   2   2   2   2   2   2   2   2
 3:   0   1   0   3   3   3   3   3   3   3
 4:   0   0   1   0   4   4   4   4   4   4
 5:   0   1   2   1   0   5   5   5   5   5
 6:   0   0   0   2   1   0   6   6   6   6
 7:   0   1   1   3   2   1   0   7   7   7
 8:   0   0   2   0   3   2   1   0   8   8
 9:   0   1   0   1   4   3   2   1   0   9
10:   0   0   1   2   0   4   3   2   1   0
11:   0   1   2   3   1   5   4   3   2   1
12:   0   0   0   0   2   0   5   4   3   2
13:   0   1   1   1   3   1   6   5   4   3
14:   0   0   2   2   4   2   0   6   5   4
15:   0   1   0   3   0   3   1   7   6   5
16:   0   0   1   0   1   4   2   0   7   6
17:   0   1   2   1   2   5   3   1   8   7
18:   0   0   0   2   3   0   4   2   0   8
19:   0   1   1   3   4   1   5   3   1   9

So I have the square but how do I replace the 0s with "*" and everything else with "-"? And how do I get my program to do as asked?

Curious
  • 3
  • 2

2 Answers2

1

You should use the variable i or limit in your inner loop as well. So you will generate a limit * limit output. Also you have to iterate until limit + 1 to get full limit rows.

A detailed version:

# get a maximim number and cast it to int
limit = int(input('Maximum number to factorise: '))

# prints a header [1; limit]
print " " + "".join(["%3d" % i for i in xrange(1, limit + 1)])
# outer loop: [1; limit]
for i in range(1, limit + 1):
    line = '%2d ' % i  # format string row number
    # inner loop: [1; limit]!
    for j in range (1, i + 1):  # you can also set limit + 1
        if i % j:  # if i % != 0
            line += '-  '
        else:
            line += '*  '
    print(line)

Generates:

   1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20
 1 *
 2 *  *
 3 *  -  *
 4 *  *  -  *
 5 *  -  -  -  *
 6 *  *  *  -  -  *
 7 *  -  -  -  -  -  *
 8 *  *  -  *  -  -  -  *
 9 *  -  *  -  -  -  -  -  *
10 *  *  -  -  *  -  -  -  -  *
11 *  -  -  -  -  -  -  -  -  -  *
12 *  *  *  *  -  *  -  -  -  -  -  *
13 *  -  -  -  -  -  -  -  -  -  -  -  *
14 *  *  -  -  -  -  *  -  -  -  -  -  -  *
15 *  -  *  -  *  -  -  -  -  -  -  -  -  -  *
16 *  *  -  *  -  -  -  *  -  -  -  -  -  -  -  *
17 *  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
18 *  *  *  -  -  *  -  -  *  -  -  -  -  -  -  -  -  *
19 *  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  *
20 *  *  -  *  *  -  -  -  -  *  -  -  -  -  -  -  -  -  -  *
wenzul
  • 3,948
  • 2
  • 21
  • 33
0

You want something like:

limit = input('Maximum number to factorise: ')
for i in range(1,int(limit)):
    line = '{:2}:'.format(i)
    for j in range (1,11):
        if i % j == 0:
            line += "{:4}".format('0')
        else:
            line += "{:4}".format('-')
    print(line)
Paul Evans
  • 27,315
  • 3
  • 37
  • 54