pythonpascals-triangle

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

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 0 0 0 0 0 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 0 0 0 0 0 0 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 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 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 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 
    

    Just for fun, if one prints # instead of 1 and space instead of 0, as in this code, then you'll get a nice picture of fractal:

                                                       # 
                                                      # # 
                                                     #   # 
                                                    # # # # 
                                                   #       # 
                                                  # #     # # 
                                                 #   #   #   # 
                                                # # # # # # # # 
                                               #               # 
                                              # #             # # 
                                             #   #           #   # 
                                            # # # #         # # # # 
                                           #       #       #       # 
                                          # #     # #     # #     # # 
                                         #   #   #   #   #   #   #   # 
                                        # # # # # # # # # # # # # # # # 
                                       #                               # 
                                      # #                             # # 
                                     #   #                           #   # 
                                    # # # #                         # # # # 
                                   #       #                       #       # 
                                  # #     # #                     # #     # # 
                                 #   #   #   #                   #   #   #   # 
                                # # # # # # # #                 # # # # # # # # 
                               #               #               #               # 
                              # #             # #             # #             # # 
                             #   #           #   #           #   #           #   # 
                            # # # #         # # # #         # # # #         # # # # 
                           #       #       #       #       #       #       #       # 
                          # #     # #     # #     # #     # #     # #     # #     # # 
                         #   #   #   #   #   #   #   #   #   #   #   #   #   #   #   # 
                        # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
                       #                                                               # 
                      # #                                                             # # 
                     #   #                                                           #   # 
                    # # # #                                                         # # # # 
                   #       #                                                       #       # 
                  # #     # #                                                     # #     # # 
                 #   #   #   #                                                   #   #   #   # 
                # # # # # # # #                                                 # # # # # # # # 
               #               #                                               #               # 
              # #             # #                                             # #             # # 
             #   #           #   #                                           #   #           #   # 
            # # # #         # # # #                                         # # # #         # # # # 
           #       #       #       #                                       #       #       #       # 
          # #     # #     # #     # #                                     # #     # #     # #     # # 
         #   #   #   #   #   #   #   #                                   #   #   #   #   #   #   #   # 
        # # # # # # # # # # # # # # # #                                 # # # # # # # # # # # # # # # # 
       #                               #                               #                               # 
      # #                             # #                             # #                             # #