python - Looped nesting -- Factorising -
this question has answer here:
- visualising factors 1 answer
i trying write program takes user input, limit , prints table visualise factors of each integer ranging 1 limit. each row represents integer between 1 , limit, whilst first row represents number 1, second number 2 etc. given position (starting 1) in row n, if factor of limit '*' there, if not '-'.
for example input of 20 should produce:
maximum number factorise: 20 * - - - - - - - - - - - - - - - - - - - * * - - - - - - - - - - - - - - - - - - * - * - - - - - - - - - - - - - - - - - * * - * - - - - - - - - - - - - - - - - * - - - * - - - - - - - - - - - - - - - * * * - - * - - - - - - - - - - - - - - * - - - - - * - - - - - - - - - - - - - * * - * - - - * - - - - - - - - - - - - * - * - - - - - * - - - - - - - - - - - * * - - * - - - - * - - - - - - - - - - * - - - - - - - - - * - - - - - - - - - * * * * - * - - - - - * - - - - - - - - * - - - - - - - - - - - * - - - - - - - * * - - - - * - - - - - - * - - - - - - * - * - * - - - - - - - - - * - - - - - * * - * - - - * - - - - - - - * - - - - * - - - - - - - - - - - - - - - * - - - * * * - - * - - * - - - - - - - - * - - * - - - - - - - - - - - - - - - - - * - * * - * * - - - - * - - - - - - - - - *
i have:
limit = input('maximum number factorise: ') in range(1,int(limit)): line = '{:2}:'.format(i) j in range (1,11): line += "{:4}".format(i % j) print(line)
but gives:
maximum number 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 have square how replace 0s "*" , else "-"? , how program asked?
you want like:
limit = input('maximum number factorise: ') in range(1,int(limit)): line = '{:2}:'.format(i) j in range (1,11): if % j == 0: line += "{:4}".format('0') else: line += "{:4}".format('-') print(line)
Comments
Post a Comment