📜 ⬆️ ⬇️

Sierpinski Triangle and Pascal's Triangle

What is it?


Sierpinski Triangle

The Sierpinski triangle is one of the most famous fractals, its construction is one of the first laboratory works for recursion in relevant disciplines in many universities. The fractal looks like this:
image

Pascal's Triangle

Pascal's triangle is an infinite table of binomial coefficients, which has a triangular shape. In this triangle on the top and sides are units. Each number is equal to the sum of the two numbers above it. The rows of the triangle are symmetrical about the vertical axis.
image

So what?


There is an interesting feature in Pascal's triangle. It displays the above fractal with its numbers. If you stare at the abyss for a long time , the abyss begins to peer at you values, then you can see that even and odd numbers are arranged in groups, because there is one unspoken known rule: even + odd = odd, even + even = even, odd + odd = even .
')
Well, less words, more work. We conclude a little clearer. People who are not interested in the software implementation will not be interested in the next paragraph.

I took the old algorithm for calculating the output of Pascal's triangle and transformed it in such a way that the remainder of its division by 2 is displayed instead of the value of the numbers. Therefore, the even ones now become zeros, the odd ones are units. I enclose the code below
#include <iostream> using namespace std; double Cnk(int N,int K) { return ((N<K)?0:((K==0)?1:((N-K+1)/double(K)*Cnk(N,K-1)))); } int main() { int n=10; for(int j=0; j<= n; j++) { for (int i=0; i <=j ; i++) cout<<(static_cast<int>(Cnk(j,i)))%2<<" "; cout<<"\n"; } return 0; } 

For clarity, I decorate the output in the following way: the output of the program is redirected to a file, from where, upon completion of the first, the pearl replaces the units with red letters O, the zeros with blue letters. Script code below:
 #! perl -w open (STREAM_IN, '1.txt');# || die "Can't open STREAM_IN\n"; open (STREAM_OUT, '>> 1.html');# || die "Can't open STREAM_OUT\n"; $ss='<br>'; while ($curr = <STREAM_IN>) { chomp($curr); $curr=~s/1/<font color="red">O<\/font>/g; $curr=~s/0/<font color="blue">O<\/font>/g; $curr=~s/-//g; $out = $curr.$ss; print (STREAM_OUT $out); }; close STREAM_IN; close STREAM_OUT; 

From the source you can see that we will look html. Why? For reasons of simplicity. Only the DOM tree is wrong. Fix it with a script on BASH and automate all of the above:
 #!/bin/bash g++ ~/serp.cpp; ~/a.out > ~/1.txt; echo ' <html> <head> <title>TRIANGLE</title> </head> <body> <center>' > ~/1.html; perl ~/s.pl; echo '</center> </body> </html>' >> ~/1.html 

So, we compile the source on the pluses, its output goes into the textbook, bash “echoes” in html to overwrite the beginning of the DOM tree, after which the textbook takes the pearl script, rewrites it into a multi-colored html-version, complements the htmlk, after which the dear BASH again completes the formation of the tree. We start, we look:

We emphasize and compare with the original

PROFIT

Source: https://habr.com/ru/post/167817/


All Articles