0

Here is my drawing: CLICK

I need to write a program, that will find the number of squares(1x1), that we can draw into a circle of given radius.The squares can only by drawn fully and placed like lego blocks- one on another. In some cases, vertexes of squares can lie on the circle.

Examples: for 1- it makes 0, for 2- it gives four, for 3- 16 squares, for 4-32, for 5-52.

I have written something, but it doesn't work fine for 5+ (I mean- radius bigger than 5). Here it goes: CLICK. In my code- r is radius of the circle, sum is the sum of all squares and height is the height of triangles I try to "draw" into the circle (using Pythagorean theorem).

Now- any help? Is my algorithm even correct? Should I change something?

4

2 に答える 2

2

与えられた半径の円内の整数点を数える式を与えるガウスの円の問題があります。このロジックを使用して、円内にある正方形を数えることができます。

N = 4 * Sum[i=1..R] (Floor(Sqrt((R^2-i^2)))

例:

R = 3
i=1   n1 = Floor(Sqrt(9-1))~Floor(2.8)=2
i=2   n2 = Floor(Sqrt(9-4))~Floor(2.2)=2
i=3   n2 = Floor(Sqrt(9-9))=0
N=4*(n1+n2+n3)=16
于 2014-12-06T04:16:46.980 に答える
0

まず、半径 5 の円は、52 ではなく 60 の 1x1 の正方形に適合します。誰かがポイント {[3,4],[3,-4],[4,3],[4 ,-3],[-4,3],[-4,-3],[-3,4],[-3,-4]} それを紙に描いて手で数えてみると、円のすぐ上またはすぐ外側。彼らはまさに円の上にいます。

2番目-MBoの答えが私をここに連れてきました-誰かが新しい楽しいアルゴリズムを提案したかどうかを確認するために、時々StackOverflowでGauss Circle Problemを検索します。

3番目- コードは次のとおりです。

int     allSquares=0,
        squaredRadius=radius*radius,
        sideOfQuarterOfInscribedSquare=(int)(long)(radius/sqrt(2));
for(int x=sideOfQuarterOfInscribedSquare+1;
        x<radius;
        x++){
    allSquares+=(long)sqrt(squaredRadius-x*x);
}
allSquares= allSquares*8+4*sideOfQuarterOfInscribedSquare*sideOfQuarterOfInscribedSquare;
return allSquares;

円の 8 分の 1 の内側、内接正方形の外側にある正方形を数えるだけです。私の流行に敏感な書式設定と過度に冗長な変数名で申し訳ありません。

于 2017-11-15T13:26:30.310 に答える