[SOLVED] 'Circle of Squares' programming challenge
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.
@GazL: Is this the Armstrong numbers thread to which you referred?. That was resurrected after 11 years? And is now 4 years older? I missed it the first two times, but may take a look at it sometime myself. Thanks for the tip.
It should be the latest. To confirm, If you have the one tagged "version 2.5" on line 1, then it's the latest.
The statement you highlight replaced a for loop that did a sequential search of an array in earlier versions, It's now a single memory access using a pointer and offset so I don't know how one could make it any faster. If you have a faster way of keeping track of what values have been used I'm all ears.
so it is not as difficult to find sequence but starting point seems to be important. It is not difficult cause must be a lot of such sequences - deterministic algorithm is just pure lack. If there are many such sequences there is great chance to find one.
Edit: My small addition to is_square test
Code:
// is_square logical table
char *p;
p = (char *) malloc((MAXSIZE*MAXSIZE+1)*sizeof(char));
for (int i = 1; i < MAXSIZE + 1; i++)
*(p+i*i) = 'A';
it test all squares up to MAXSIZE*MAXSIZE. For n test is satisfied if *(p+n) == 'A', fails if
*(p+n) == '\0'. The last this dependency from starting point gives me following idea
Code:
% a.out N $(($RANDOM % N))
say with N = 76 the third run gave almost immediate answer. So put time limit execution up to 1 minut and execute that command.
Miracle happens.
...
so it is not as difficult to find sequence but starting point seems to be important. It is not difficult cause must be a lot of such sequences - deterministic algorithm is just pure lack. If there are many such sequences there is great chance to find one.
I was thinking about an algorithm to find out the "best" starting point.
Right now I think we can calculate the number of pairs for every number in the circle and I think we need to find the one with the lowest number of pairs (and probably we can go further to find the number of second/third/... neighbors). But probably this is not really useful.
With randomization and timeout 60 seconds it took me 6 min to find sequence of 97 numbers (1 .. 97). But it seems some numbers are more lucky. Timeout 60 sec means that program found sequence in period of time shorter than 60 sec.
I think that improvement will be if program would accept starting sequence - you can think about first number as 0-level randomization Providing longer starting sequence is higher randomization. Just pick starting sequence randomly.
Code:
% amazing_circle 127 1 3 13 23
Edit: I ma^T uploading amazing circles for 97, 98, 99 - they can be computed relatively fast - below 6 min.
Here at last is something to upload; have included the latest DataRing.h.txt include file as a separate upload, as more general use code. The other WonderRing.cpp.txt contains several pieces specific to this Ring of Squares challenge, as noted within the txt file. You may have to change some include file specs; my gcc compiler is pretty old.
A few notes on what i've been doing off-line. Forgive me if i'm treating of things that you folks have long since conquered.
1. From the beginning, i've treated the sequences as rings, with indeterminate start, end, and flow direction. In fact, rings of data strike me as a concept with practical applications elsewhere. Probably it's already been done, and probably better than my amateurish C++ code. See DataRing.h.txt and comments.
2. Rather than code functions to calculate squares and square roots, i always simply used a static array of squares. It then occurred to me that this array could be any series of target numbers, say Fibonacci or Armstrong. To try this idea, i made an array of prime numbers and was able to easily generate rings of primes with pretty much the same code. (Caveat: you can't generate a ring of primes with an odd number of elements. Think about it.) The output of rings of primes was very fast but grew in number VERY quickly when the -a (all_rings) option was employed. For example, the command
Code:
PrimeRing 16 -a
displays 81024 valid rings of 16 elements, while recursing through RecursFind() for only 1016063 cycles (depending upon the starting, or seed number - see below).
3. With rings of data, it was a simple matter to include an option (-o) to order the elements of the rings before dumping to the screen. This did not involve any sorting or rearranging of the elements; simply alter the head, tail, and flow direction to give the desired order. This, along with the -a option, allowed the program to identify and discard any mirror (duplicate) rings. So, for example
Code:
SquareRing 34 6 -a
yields
Code:
22 valid rings generated
670991 cycles of RecursFind()
while
Code:
SquareRing 34 6 -ao
gives
Code:
11 valid rings generated and ordered
670991 cycles of RecursFind()
Note that no processing time is saved, since all rings are still generated, just that not all are accepted and displayed.
4. Speaking of processing time, most of my computer time since my last LQ visit was spent trying to get my program to run more efficiently and faster. That deserves a separate post, below.
Caveat: Am still getting an occasional floating point error; haven't yet tracked it down.
Last edited by dogpatch; 04-23-2021 at 02:35 PM.
Reason: add caveat (also update 2nd attachment)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.