LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   amazing circle (https://www.linuxquestions.org/questions/programming-9/amazing-circle-4175693071/)

igadoter 04-03-2021 07:49 AM

amazing circle
 
1 Attachment(s)
Create programm to verify this circle is really amazing for perfect square just google

NevemTeve 04-04-2021 10:20 AM

That's amazing, indeed.

dogpatch 04-04-2021 11:47 AM

Cool.

What do you mean by "Create programm (sic) to verify..."? Might there be other sequences of integers 1 thru 32 that work? Might there be other limits other than 1 thru 32?

igadoter 04-04-2021 01:57 PM

Quote:

Originally Posted by dogpatch (Post 6237164)
Cool.

What do you mean by "Create programm (sic) to verify..."? Might there be other sequences of integers 1 thru 32 that work? Might there be other limits other than 1 thru 32?

Problem is to implement effectively "circle" as data structure - or ring. There is no "first" or "last" element in "circle".

boughtonp 04-04-2021 02:37 PM

Quote:

Originally Posted by igadoter (Post 6237203)
Problem is to implement effectively "circle" as data structure - or ring. There is no "first" or "last" element in "circle".

That's not a problem any more than drawing a circle on paper is a problem - just pick any arbitrary position to begin at, and connect back to that point.

What's the context of this? Is it homework, a learning exercise, just a curiosity, or something else?


danielbmartin 04-04-2021 02:40 PM

With this one-line InFile ...
Code:

1 8 28 21 4 32 17 19 30 6 3 13 12 24 25 11 5 31 18 7 29 20 16 9 27 22 14 2 23 26 10 15 1
... this awk ...
Code:

awk  \
  'BEGIN{SqTab=1 4 9 16 25 36 49 64 81 100}    # Build a table of perfect squares.
    {for (j=1;j<=32;j++)                      # 32 number pairs to be tested.
      {sum=$j+$(j+1)                          # Add an adjacent pair of numbers.
        print j,$j"+"$(j+1)"="sum              # Show result.
        if (index(SqTab,sum)==0) print "FAIL!" # Handle FAIL case.
    }}' $InFile >$OutFile
cat $OutFile

... produced this OutFile ...
Code:

1 1+8=9
2 8+28=36
3 28+21=49
4 21+4=25
5 4+32=36
6 32+17=49
7 17+19=36
8 19+30=49
9 30+6=36
10 6+3=9
11 3+13=16
12 13+12=25
13 12+24=36
14 24+25=49
15 25+11=36
16 11+5=16
17 5+31=36
18 31+18=49
19 18+7=25
20 7+29=36
21 29+20=49
22 20+16=36
23 16+9=25
24 9+27=36
25 27+22=49
26 22+14=36
27 14+2=16
28 2+23=25
29 23+26=49
30 26+10=36
31 10+15=25
32 15+1=16

Daniel B. Martin

.

dogpatch 04-04-2021 03:01 PM

Quote:

Originally Posted by igadoter (Post 6237203)
Problem is to implement effectively "circle" as data structure - or ring. There is no "first" or "last" element in "circle".

Perhaps i still don't understand your question, but it doesn't sound difficult. To operate on a 'ring' of data, you would have to start at some (random?) point on the ring, and proceed either clockwise or counter-clockwise until you returned to the starting point.

Programmatically, this could be achieved by defining the ring as a simple array, and then, starting from a random offset, process in a linear manner, logically wrapping around to the array's first element as needed, continuing until you return to the starting offset. For counter-clockwise, process in reverse order.

But here's an interesting programming challenge: Given the requirements that the numbers 1 thru 32 must exist exactly once, and that the sum of each adjacent pair of numbers must be a perfect square, write a program to generate a ring of data. The above methodology could be employed to do so, and to compare the sequence(s) generated against the ring of data shown in the jpg image.

dogpatch 04-04-2021 03:05 PM

While i was typing the above, danielbmartin supplied a program. My previous post tells how to treat the data as a 'ring'. Start at a random offest, and wrap as needed.

Note: You'd have to increase the for loop by one step, to connect the first and last element in the 'ring'.

danielbmartin 04-04-2021 03:44 PM

Quote:

Originally Posted by dogpatch (Post 6237216)
... Note: You'd have to increase the for loop by one step, to connect the first and last element in the 'ring'.

The published solution does close the ring.

Daniel B. Martin

.

dogpatch 04-05-2021 08:10 AM

Quote:

Originally Posted by danielbmartin (Post 6237225)
The published solution does close the ring.

Daniel B. Martin

.

Well. . . not exactly. It merely adds an element to the linear array, with the added last element equal to the first. Same result, but not quite the same as wrapping around to form a logical ring.

danielbmartin 04-05-2021 09:43 AM

Quote:

Originally Posted by dogpatch (Post 6237410)
Well. . . not exactly. It merely adds an element to the linear array, with the added last element equal to the first. Same result, but not quite the same as wrapping around to form a logical ring.

I fail to see the distinction. Please explain.

Daniel B. Martin

.

dogpatch 04-05-2021 12:00 PM

In post #4, OP was asking
Quote:

Originally Posted by igadoter (Post 6237203)
Problem is to implement effectively "circle" as data structure - or ring. There is no "first" or "last" element in "circle".

Your program works, but treats the data as a linear array. The better way to answer the OP would be to define a class 'Ring', in which processing could begin at any arbitrary point in the ring, and wrap around in a circular manner. In a non-oop program, you could still use a linear array, but allow processing to begin at any point in the array, and wrap around to the beginning as needed.

dogpatch 04-05-2021 12:08 PM

I may post a small C++ program to illustrate what i mean. But am out of connection minutes, so will wait until another day.

danielbmartin 04-06-2021 08:48 AM

Post #1 in this thread asked for a program to test the validity of a Magic Circle. Quoting from the image in that post, "the sum of any two adjacent numbers is a perfect square."

Testing this assertion does not require any specific starting point, nor does it demand a direction of travel (clockwise or counterclockwise). If all number pairs sum to a perfect square the number sequence is validated.

Post #7 introduces the word random. That was not stipulated in post #1 nor does it strengthen the validation demonstrated by the solution offered in post #6. If every number pair sums to a perfect square, that is true regardless of travel direction or starting point.

A favorite maxim: "Don't make the job harder than it has to be."

Daniel B. Martin

.

pan64 04-06-2021 10:33 AM

verifying that circle is not an issue. You can do it without computer, calculator or any other tool in minutes (entering the numbers into any device will take longer than the check).
The "real" problem is to generate a circle like this. How many different amazing circle exist?


All times are GMT -5. The time now is 11:14 AM.