Interesting fractal graphics-solutions to recursion and Mathematics-poj 2083

Source: Internet
Author: User

Description

A fractal is an object or quantity that displays self-similarity, in a somewhat technical sense, on all scales. the object need not exhibit exactly the same structure at all scales, but the same "type" of structures must appear on all scales.
A box fractal is defined as below:
  • A box fractal of Degree 1 is simply
    X
  • A box fractal of degree 2 is
    X
    X
    X
  • If using B (n-1) to represent the box fractal of degree n-1, then a box fractal of degree n is defined recursively as following
    B(n - 1)        B(n - 1)        B(n - 1)B(n - 1)        B(n - 1)

Your task is to draw a box fractal of degree n.

Input

The input consists of several test cases. each line of the input contains a positive integer n which is no greater than 7. the last line of input is a negative integer −1 indicating the end of input.

Output

For each test case, output the box fractal using the 'X' notation. Please notice that 'X' is an uppercase letter. Print a line with only a single dash after each test case.

Sample Input

1234-1

Sample output

X-X X XX X-X X   X X X     XX X   X X   X X    X   X XX X   X X X     XX X   X X-X X   X X         X X   X X X     X           X     XX X   X X         X X   X X   X X               X X    X                 X   X X               X XX X   X X         X X   X X X     X           X     XX X   X X         X X   X X         X X   X X          X     X         X X   X X            X X             X            X X         X X   X X          X     X         X X   X XX X   X X         X X   X X X     X           X     XX X   X X         X X   X X   X X               X X    X                 X   X X               X XX X   X X         X X   X X X     X           X     XX X   X X         X X   X X


Recursion:

Let's take a look at the more common recursive solution:

Because the images are repeated, small images only output a part of the upper left corner of the large image, and only calculate the largest image. Table creation can speed up.

# Include <stdio. h> # include <string. h> int P [8] = {81,243,729, 730, 730}; char map [] []; // n current graph size, X, Y coordinate void print (int n, int X, int y) {If (n = 0) {map [x] [Y] = 'X '; // return;} print (n-1, x, y); // print (n-1, x + 2 * P [n-1], Y) on the left; // print (n-1, Y) on the Right, X + P [n-1], Y + P [n-1]); // intermediate print (n-1, X, Y + 2 * P [n-1]); print (n-1, X + 2 * P [n-1], Y + 2 * P [n-1]);} int N; int main () {for (INT I = 0; I <p [6]; I ++) memset (Map [I], 32, p [6]); print (6, 0, 0 ); // while (scanf ("% d", & N) & n --> = 0) {for (INT I = 0; I <p [N]; I ++) {map [I] [p [N] = 0; puts (Map [I]); map [I] [p [N] = '';} puts ("-");} return 0 ;}

2. Mathematical Methods

In discuss, we can see this short and concise program.

#include"stdio.h"#include"math.h"main(){int i,j,n,ii,jj,k;while(scanf("%d",&n)&&n--!=-1){for(i=0;i<pow(3,n);i++,printf("\n"))for(j=0;j<pow(3,n);j++){for(ii=i,jj=j,k=0;k<n&&(ii%3+jj%3)%2==0;ii/=3,jj/=3,k++);printf("%c",32+56*(k==n));}printf("-\n");} }

The following is an excerpt from the http://www.matrix67.com blog:

This image can also be used to prove

Find 1/5
+ 1/25 + 1/125 + .. = 1/4 graphic proofs

Some time ago, a large number of online graphic proofs about 1/4 + 1/16 + 1/64 +... = 1/3 emerged (1) (2 ). However, how many people have wondered why these figures prove that the base number is 1/4? It is also the sum of geometric series. Can we construct a graph to prove 1/5
+ 1/25 + 1/125 +... = 1/4?


Let's try it. The first idea of most people is to draw a positive Pentagon and divide it into five-level points. Next, color one of them, indicating 1/5 of the total area. What then? What should I do? We need a way to divide one of the remaining four parts into five parts again, and select one to represent 1/25, and recursively perform the operation. We seem to have discovered the problem: this "recursion" cannot be done. To recursively express 1/5, 1/25, and 1/125, you must divide a positive Pentagon into five small positive pentagons and divide the small positive Pentagon into five smaller positive pentagons, this does not seem to be possible. That is why the above figures can be used to prove the root cause of Σ (1/4) ^ n = 1/3: they can divide themselves into four smaller ones, so as to recursively represent (1/4) ^ n.
Now, we have a problem: in order to make a dashboard of Σ (1/5) ^ n = 1/4, we must look for such a graph, it divides itself into five identical parts, each of which is similar to itself.

Can we find this image? No! The root cause is that a plane image is 2D. The dimension of a plane image determines the similarity ratio and area ratio of two similar images. If the side length of a square is twice the length of the other side, the area of the former is four times that of the latter. The area of the circle must be in square relationship with the radius R, it can be multiplied by a constant coefficient at most. Similarly, if a curve is doubled, its length will be doubled. If a three-dimensional image is doubled, its size will become eight times the original size. Therefore, dividing a plane image into four similar images is quite harmonious-the area is changed to 1/4, and the side length should be changed to 1/2, the two 1/2 sides are exactly the same original side length. However, if you want to divide a plane image into five equal parts, the area of each part should be 1/5, then the corresponding edge should be changed to the original 1/SQRT (5 ), in this way, the edges of the original image cannot be filled without repetition or omission. It seems that it is impossible to prove that Σ (1/5) ^ n = 1/4.

When thinking about problems, we should be good at using imagination instead of giving up. It can be proved that Σ (1/4) ^ n = 1/3 in two-dimensional space. Similarly, in a one-dimensional space, we can easily prove that Σ (1/2) ^ n = 1. It would be nice to have something that is log (5)/log (2)-in this case, the corresponding edge is doubled to the original, the "area" is changed to 2 ^ (log (5)/log (2) times, that is, 5 times. That is to say, in the "space" of log (5)/log (2) dimension, a "Graph" can exactly contain five similar "graphs ", in addition, the new "side length" is able to remove the original "side length ". Someone will laugh here-is there such a dimension?
Yes. Many fragment graphs have some extremely weird properties. The changes in the similarity ratio and the changes in the occupied space do not have an integer power. For example, the Sierpinski triangle, a well-known fractal image, is like this-the similarity ratio is 1/2, and the Space proportion is 1/3. A single image looks like a two-dimensional image, but does not have the nature of a two-dimensional image. The Hausdorff dimension is specifically used to deal with such problems. We often use the Hausdorff dimension to describe a fractal image. For example, when the Hausdorff dimension of the Sierpinski triangle is log (3)/log (2)-the original three times the occupied area, the corresponding edge is doubled to the original one.

Hey! So can Sierpinski triangles prove Σ (1/3) ^ n = 1/2? Yes! A Sierpinski triangle consists of three small Sierpinski triangles similar to itself, so you can recursively Express (1/3) ^ n. Draw 1/3 of the entire Sierpinski triangle, and 1/3 of the other 1/3, and 1/3 of the 1/3, so that you can see it soon, the total area is exactly half of the original area.

Similarly, the Sierpinski Carpet is magnified to three times the original size, and the space occupied by the entire image is changed to eight times the original size. Its Hausdorff dimension is log (8)/log (3 ). So we can use it to prove that Σ (1/8) ^ n = 1/7.

Back to the original question in this article, in order to prove that Σ (1/5) ^ n = 1/4, we only need to find such a fragment, and Its Hausdorff dimension molecules are log (5 ). There are not only one such image, but also more than one. You can find various Hausdorff dimensions on this page. I found an interesting fractal image called vicsek snowflake. Its Hausdorff dimension is log (5)/log (3 ).

Continuously select the 1/5 in a smaller sense, you will see at a glance that the total of the selected part is 1/4 of the entire graph.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.