ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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?
Indeed! It isn't really about the "circle", it is about the ordering the first 32 positive integers.
Questions that come to mind are:
Is that ordering unique? Can those same numbers be ordered differently and still have the paired square property? How many such re-orderings are there?
Is there anything special about the first 32 integers arranged in such an order? Can you extend it to 34? 64? Or use fewer numbers?
Can you remove a single number and retain the paired square property?
What series of integers cannot be so ordered? Is there some way to tell which can and which cannot?
@ danielbmartin: Am not denying that your program adequtely addressed the OP's original question in post #1. But it does not define a ring data type, without a logical beginning or end, per post #4.
As mentioned above, here is my simple implementation of a Ring data type which also tests the circular ring of data above:
Code:
/** SquareRing.cpp - define, load and test a ring of data
*/
#include <string> // for printf etc.
//#include <iostream> // for cout etc.
/* define static data per linuxquestions 'amazing circle' thread */
typedef char rType;
#define RingSize 32
rType LinearData[RingSize] = {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};
#define SquareCount 6
rType Squares[SquareCount] = {4, 9, 16, 25, 36, 49};
/** Ring class: define and process Ring (circular array) data type */
class Ring {
public:
int rSize;
int rHead; // arbitrary start point in ring
bool rClockwise; // true=Clockwise, false=Counter-clockwise
rType rElement[RingSize]; // store ring as linear array
bool rInit() { // do almost nothing (future: alloc dynamically)
srand(time(0));
Randomize();
return true;
};
void Randomize() {
rHead = rand()%RingSize; // randomize rHead offset
rClockwise = rand()%2; // and direction of flow
return;
};
bool LoadRingData() { // load ring from static array
int c0, c1;
rSize = 0;
c1 = rHead; // starting from rHead
for (c0 = 0; c0 < RingSize; c0++) {
rElement[c1] = LinearData[c0];
c1 = AdvanceIndex(c1);
rSize++;
}
return true;
};
int AdvanceIndex(int Index) {
// Step index clockwise or counter, wrapping as needed
if (rClockwise) { // clockwise (normal order)
Index++;
if (Index == RingSize)
Index = 0; // wrap to 0 in linear array
}
else { // counter-clockwise (descending order)
if (!Index)
Index = RingSize; // wrap to end of linear array
Index--;
}
return (Index);
}
bool TestRing() { // test each adjacent pair in ring
int sum;
int c0, c1, c2, c3;
rType inarray[RingSize];
memset(inarray,0,(sizeof(rType)*RingSize));
c1 = rHead; // starting from rHead
for (c0 = 0; c0 < RingSize; c0++) {
c2 = AdvanceIndex(c1); // point to next element
// validate both elements within range
if (rElement[c2] < 1 || rElement[c2] > RingSize) {
printf("%d is not between 1 and %d\n",rElement[c2],RingSize);
return false;
}
if (rElement[c1] < 1 || rElement[c1] > RingSize) {
printf("%d is not between 1 and %d\n",rElement[c1],RingSize);
return false;
}
// and they're not already in array
if (inarray[rElement[c1]-1]) {
printf("element '%d' occurs more than once\n",rElement[c1]);
return false;
}
if (inarray[rElement[c2]-1] && (c0 < (RingSize-1))) {
printf("element '%d' occurs more than once\n",rElement[c2]);
return false;
}
inarray[rElement[c1]-1] = rElement[c1];
sum = rElement[c1] + rElement[c2];
printf("%d + %d = %d", rElement[c1], rElement[c2], sum);
// and that the adjacent pair sums to a perfect square
for (c3 = 0; c3 < SquareCount; c3++)
if (sum == Squares[c3]) break;
if (c3 == SquareCount) { // square test failure
printf(": NOT A PERFECT SQUARE\n");
return false;
}
printf(": OK\n");
c1 = AdvanceIndex(c1);
}
return true;
};
bool DumpRing() {
int c0, c1;
c1 = rHead;
for (c0 = 0; c0 < RingSize; c0++) {
printf("%d ",rElement[c1]);
c1 = AdvanceIndex(c1);
}
printf("\n");
return true;
};
};
int main(void) {
Ring r0;
r0.rInit();
r0.LoadRingData();
r0.Randomize();
if (r0.TestRing())
printf("Ring tests OK\n");
else
printf("Ring doesn't test OK\n");
// r0.DumpRing();
exit(0);
}
May i point out that this methodology more adequately treats the data as a ring. Processing it in a random or arbitrary way both as to the point at which to begin, and the direction of flow, should always give satisfactory results, as suggested by the definition of a ring without defined beginning or end.
Last edited by dogpatch; 04-07-2021 at 12:20 PM.
Reason: add end note
Indeed! It isn't really about the "circle", it is about the ordering the first 32 positive integers.
Questions that come to mind are:
Is that ordering unique? Can those same numbers be ordered differently and still have the paired square property? How many such re-orderings are there?
Is there anything special about the first 32 integers arranged in such an order? Can you extend it to 34? 64? Or use fewer numbers?
Can you remove a single number and retain the paired square property?
What series of integers cannot be so ordered? Is there some way to tell which can and which cannot?
Lots of space to explore there!
Starting to remind me of the great 2019 puzzle / challenge.
Do you want to launch the 'Circle of Squares' programming challenge thread, or should i?
There maybe many "amazing circles". Consecutive three numbers. Think about something different than numbers. Say words - circle of words - and when jumping along it something funny happens. Simple idea to implement circle is shift code - it can be static or dynamic. Just two rings with letters - switching one of rings - say for three positions - gives a code a->d b->e ...So abba now reads deed.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.