LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-06-2021, 12:53 PM   #16
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,263
Blog Entries: 24

Rep: Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194Reputation: 4194

Quote:
Originally Posted by pan64 View Post
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?

Lots of space to explore there!
 
1 members found this post helpful.
Old 04-07-2021, 12:05 PM   #17
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
@ 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
 
Old 04-07-2021, 12:10 PM   #18
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
Quote:
Originally Posted by astrogeek View Post
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?
 
2 members found this post helpful.
Old 04-07-2021, 01:54 PM   #19
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 21,804

Rep: Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306Reputation: 7306
Quote:
Originally Posted by dogpatch View Post
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?
why not? in a generalized form: we have n numbers (usually {1..n}) and need to create a circle, where we have a condition on the neighbors.
 
1 members found this post helpful.
Old 04-07-2021, 02:05 PM   #20
igadoter
Senior Member
 
Registered: Sep 2006
Location: wroclaw, poland
Distribution: many, primary Slackware
Posts: 2,717

Original Poster
Blog Entries: 1

Rep: Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625Reputation: 625
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.
 
Old 04-07-2021, 06:57 PM   #21
dogpatch
Member
 
Registered: Nov 2005
Location: Central America
Distribution: Mepis, Android
Posts: 490
Blog Entries: 4

Rep: Reputation: 238Reputation: 238Reputation: 238
You've got it. The challenge is hereby issued.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
RPM - dependencies going round in circle! The_JinJ Linux - Newbie 6 11-23-2004 04:09 AM
NDISWrapper and the circle of Hate ryanroler Linux - Wireless Networking 3 11-06-2004 08:55 PM
going in circle's with fstab and cd-rom chopp Linux - Newbie 32 10-08-2003 07:08 AM
Confusion with RPM's - vicious circle mykyl Linux - Software 13 09-18-2003 02:48 PM
Draw Circle Gerardoj Programming 11 08-24-2003 05:32 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:06 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration