-3

I've come across a problem that asks me to print a table to visualize all factors of each integer ranging from 1 to limit. Then it specifies that a given position i, starting from 1 in a row n, then a * indicates that i is a factor of n, and - indicates that it is not. Below is an example of the output.

I know that I've to make use modulus operator to test the factors and a for loop, but I'm so confused with constructing the code.

Maximum number to factorise: 20
* - - - - - - - - - - - - - - - - - - - 
* * - - - - - - - - - - - - - - - - - - 
* - * - - - - - - - - - - - - - - - - - 
* * - * - - - - - - - - - - - - - - - - 
* - - - * - - - - - - - - - - - - - - - 
* * * - - * - - - - - - - - - - - - - - 
* - - - - - * - - - - - - - - - - - - - 
* * - * - - - * - - - - - - - - - - - - 
* - * - - - - - * - - - - - - - - - - - 
* * - - * - - - - * - - - - - - - - - - 
* - - - - - - - - - * - - - - - - - - - 
* * * * - * - - - - - * - - - - - - - - 
* - - - - - - - - - - - * - - - - - - - 
* * - - - - * - - - - - - * - - - - - - 
* - * - * - - - - - - - - - * - - - - - 
* * - * - - - * - - - - - - - * - - - - 
* - - - - - - - - - - - - - - - * - - - 
* * * - - * - - * - - - - - - - - * - - 
* - - - - - - - - - - - - - - - - - * - 
* * - * * - - - - * - - - - - - - - - * 
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
  • 3
    Post what have you tried and where you got stuck. – Ahsanul Haque Sep 20 '15 at 13:40
  • What @AhsanulHaque said. The point is that this is a relatively basic Python question, and the only recommendation we could give you is: *try* and *read*. *Try* out your approach, find out where it goes wrong, and before you do that *read* a python tutorial, and do the exercises there. This will make you 1000x faster at solving problems yourself. – Marcus Müller Sep 20 '15 at 15:28
  • Look into http://stackoverflow.com/questions/32691193/looped-nesting-factorising/32691449#32691449. – wenzul Sep 21 '15 at 09:50

1 Answers1

2

I am not going to give the complete code, since this is clearly an homework assignment.

But here is a pseudo algorithm that can get you started -

  1. You would need two for loops, one nested inside the other. The first for loop for going over the rows, second inner for loop for going over the columns.

  2. Inside the nested for loop, you would need to check whether the counter for inner for loop is divisible by the counter variable of the outer for loop, if it is you need to print * without giving a new line (To do this you can use end='' argument in Python 3 , or use , after what you want to print in Python 2), if not divisible print -.

  3. Finally after completing the inner loop, you would need to print another newline, so that the next row starts at next line.

martineau
  • 119,623
  • 25
  • 170
  • 301
Sharon Dwilif K
  • 1,218
  • 9
  • 17
  • What's a pseudo algorithm? `;-)` – martineau Sep 20 '15 at 14:43
  • @martineau: this :) No, seriously, the formal requirement for an algorithm is that it's a) deterministic (Sharon implies this here), b) terminating (this is), but also c) clear at every point of application -- which this is not, because Sarah Young doesn't seem to be fit enough to translate each of these steps into instructions. Hence, it's an algorithm for us who can already write python, and a pseudo algorithm for Sarah, with which she can much easier learn python. – Marcus Müller Sep 20 '15 at 15:30
  • @Marcus: I've heard of pseudo-code, but never a pseudo-algorithm before — learn something new everyday... – martineau Sep 20 '15 at 15:45
  • @martineau Obviously, Sharon invented that word, but you can say anything as long as you have a good explanation for it! – Marcus Müller Sep 20 '15 at 15:46