Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Question about Pascal's Triangle generator

I am a Python beginner and I have written a code for generating "Pascal’s Triangle"

from math import factorial
n = 50

for i in range(n):
    for j in range(n-i+1):

        print(end=" ")

    for j in range(i+1):

        print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

    print()

but, now I want to go bit further by changing the even Entries of the of the triangle to 0 and odd entries to 1, so

1

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

11

121

1331

14641

will become

1

11

101

1111

10001

so, what changes do I have to do in my code for this?

>Solution :

Just instead of printing value you need to print value & 1, as in following code.

value & 1 gives a lower bit of an integer, which is also the same as value % 2 (remainder of division by 2), this bit is equal 1 if number is odd and 0 if it is even.

Try it online!

from math import factorial
n = 50

for i in range(n):
    for j in range(n-i+1):

        print(end=" ")

    for j in range(i+1):

        print((factorial(i)//(factorial(j)*factorial(i-j))) & 1, end=" ")

    print()

Output:

                                                   1 
                                                  1 1 
                                                 1 0 1 
                                                1 1 1 1 
                                               1 0 0 0 1 
                                              1 1 0 0 1 1 
                                             1 0 1 0 1 0 1 
                                            1 1 1 1 1 1 1 1 
                                           1 0 0 0 0 0 0 0 1 
                                          1 1 0 0 0 0 0 0 1 1 
                                         1 0 1 0 0 0 0 0 1 0 1 
                                        1 1 1 1 0 0 0 0 1 1 1 1 
                                       1 0 0 0 1 0 0 0 1 0 0 0 1 
                                      1 1 0 0 1 1 0 0 1 1 0 0 1 1 
                                     1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
                                    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
                                   1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
                                  1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
                                 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 
                                1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 
                               1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 1 
                              1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 1 1 
                             1 0 1 0 1 0 1 0 0 0 0 0 0 0 0 0 1 0 1 0 1 0 1 
                            1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1 
                           1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 1 
                          1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1 1 
                         1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 0 0 0 0 0 1 0 1 
                        1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 0 0 0 0 1 1 1 1 
                       1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 
                      1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 0 0 1 1 
                     1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 0 1 
                    1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 
                   1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
                  1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
                 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 
                1 1 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1 
               1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0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 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 
  1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading