mersenneforum.org  

Go Back   mersenneforum.org > Fun Stuff > Puzzles

Reply
 
Thread Tools
Old 2014-12-03, 14:23   #12
alpertron
 
alpertron's Avatar
 
Aug 2002
Buenos Aires, Argentina

2×683 Posts
Default

OK, so the program can be made a lot faster by only saving non-decreasing sums in order not to save permutations. Most of the time in the program is lost when sorting the intermediate file.

Using this information I changed the program to handle binary matrices of 4x8:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
  char oldstr[100];
  char newstr[100];
  int a,b,c,d,e,f,g,h,i,j,k,l,m;
  int count = 0;
  FILE *fpFile = fopen("test", "wt");
  for (a=0; a<2147483648; a+=2)
  {
    b =                   ((a >> 27) & 1) + ((a >> 23) & 1) + ((a >> 19) & 1) + ((a >> 15) & 1) + ((a >> 11) & 1) + ((a >> 7) & 1) + ((a >> 3) & 1);
    c = ((a >> 30) & 1) + ((a >> 26) & 1) + ((a >> 22) & 1) + ((a >> 18) & 1) + ((a >> 14) & 1) + ((a >> 10) & 1) + ((a >> 6) & 1) + ((a >> 2) & 1);
    d = ((a >> 29) & 1) + ((a >> 25) & 1) + ((a >> 21) & 1) + ((a >> 17) & 1) + ((a >> 13) & 1) + ((a >> 9) & 1) + ((a >> 5) & 1) + ((a >> 1) & 1);
    e = ((a >> 28) & 1) + ((a >> 24) & 1) + ((a >> 20) & 1) + ((a >> 16) & 1) + ((a >> 12) & 1) + ((a >> 8) & 1) + ((a >> 4) & 1);

    f =                   ((a >> 30) & 1) + ((a >> 29) & 1) + ((a >> 28) & 1);
    g = ((a >> 27) & 1) + ((a >> 26) & 1) + ((a >> 25) & 1) + ((a >> 24) & 1);
    h = ((a >> 23) & 1) + ((a >> 22) & 1) + ((a >> 21) & 1) + ((a >> 20) & 1);
    i = ((a >> 19) & 1) + ((a >> 18) & 1) + ((a >> 17) & 1) + ((a >> 16) & 1);
    j = ((a >> 15) & 1) + ((a >> 14) & 1) + ((a >> 13) & 1) + ((a >> 12) & 1);
    k = ((a >> 11) & 1) + ((a >> 10) & 1) + ((a >> 9) & 1) + ((a >> 8) & 1);
    l = ((a >> 7) & 1) + ((a >> 6) & 1) + ((a >> 5) & 1) + ((a >> 4) & 1);
    m = ((a >> 3) & 1) + ((a >> 2) & 1) + ((a >> 1) & 1);
    if (b<=c && c<=d && d<=e && f<=g && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m)
    {
      fprintf(fpFile, "[%d %d %d %d] [%d %d %d %d %d %d %d %d]\n",b,c,d,e,f,g,h,i,j,k,l,m);
    }
    if (b+1<=c && c<=d && d<=e && f+1<=g && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m)
    {
      fprintf(fpFile, "[%d %d %d %d] [%d %d %d %d %d %d %d %d]\n",b+1,c,d,e,f+1,g,h,i,j,k,l,m);
    }
    if (b<=c && c<=d && d<=e+1 && f<=g && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m+1)
    {
      fprintf(fpFile, "[%d %d %d %d] [%d %d %d %d %d %d %d %d]\n",b,c,d,e+1,f,g,h,i,j,k,l,m+1);
    }
    if (b+1<=c && c<=d && d<=e+1 && f+1<=g && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m+1)
    {
      fprintf(fpFile, "[%d %d %d %d] [%d %d %d %d %d %d %d %d]\n",b+1,c,d,e+1,f+1,g,h,i,j,k,l,m+1);
    }
  }
  fclose(fpFile);
  system("sort test /O test2");

  fpFile = fopen("test2", "rt");
  oldstr[0] = 0;
  while (!feof(fpFile))
  {
    fgets(newstr, 100, fpFile);
    if (strcmp(newstr, oldstr))
    {
      if (count == 29)
      {
        printf("%s", oldstr);
      }
      count = 0;
      strcpy(oldstr, newstr);
    }
    count++;
  }
}
The output is:

[1 2 3 5] [0 0 1 1 1 2 3 3]
[1 3 4 5] [0 0 1 1 2 3 3 3]
[2 3 4 6] [0 1 1 1 2 3 3 4]
[2 4 5 6] [0 1 1 2 3 3 3 4]
[3 4 5 7] [1 1 1 2 3 3 4 4]
[3 5 6 7] [1 1 2 3 3 3 4 4]

Last fiddled with by alpertron on 2014-12-03 at 14:53
alpertron is offline   Reply With Quote
Old 2014-12-03, 16:29   #13
alpertron
 
alpertron's Avatar
 
Aug 2002
Buenos Aires, Argentina

25268 Posts
Default

I changed the program to handle the 5x7 case.

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
  char oldstr[100];
  char newstr[100];
  int a,b,c,d,e,f,g,h,i,j,k,l,m,z;
  int count = 0;
  FILE *fpFile = fopen("test", "wt");
  for (z=0; z<32; z++)
  {
    g = ((z >> 4) & 1) + ((z >> 3) & 1) + ((z >> 2) & 1) + ((z >> 1) & 1) + (z & 1);
    for (a=0; a<1073741824; a+=2)
    {
      b = ((z >> 4) & 1) + ((a >> 29) & 1) + ((a >> 24) & 1) + ((a >> 19) & 1) + ((a >> 14) & 1) + ((a >> 9) & 1) + ((a >> 4) & 1);
      c = ((z >> 3) & 1) + ((a >> 28) & 1) + ((a >> 23) & 1) + ((a >> 18) & 1) + ((a >> 13) & 1) + ((a >> 8) & 1) + ((a >> 3) & 1);
      d = ((z >> 2) & 1) + ((a >> 27) & 1) + ((a >> 22) & 1) + ((a >> 17) & 1) + ((a >> 12) & 1) + ((a >> 7) & 1) + ((a >> 2) & 1);
      e = ((z >> 1) & 1) + ((a >> 26) & 1) + ((a >> 21) & 1) + ((a >> 16) & 1) + ((a >> 11) & 1) + ((a >> 6) & 1) + ((a >> 1) & 1);
      f = ( z       & 1) + ((a >> 25) & 1) + ((a >> 20) & 1) + ((a >> 15) & 1) + ((a >> 10) & 1) + ((a >> 5) & 1); 

      h = ((a >> 29) & 1) + ((a >> 28) & 1) + ((a >> 27) & 1) + ((a >> 26) & 1) + ((a >> 25) & 1);
      i = ((a >> 24) & 1) + ((a >> 23) & 1) + ((a >> 22) & 1) + ((a >> 21) & 1) + ((a >> 20) & 1);
      j = ((a >> 19) & 1) + ((a >> 18) & 1) + ((a >> 17) & 1) + ((a >> 16) & 1) + ((a >> 15) & 1);
      k = ((a >> 14) & 1) + ((a >> 13) & 1) + ((a >> 12) & 1) + ((a >> 11) & 1) + ((a >> 10) & 1);
      l = ((a >> 9) & 1) + ((a >> 8) & 1) + ((a >> 7) & 1) + ((a >> 6) & 1) + ((a >> 5) & 1);
      m = ((a >> 4) & 1) + ((a >> 3) & 1) + ((a >> 2) & 1) + ((a >> 1) & 1);
      if (b<=c && c<=d && d<=e && e<=f && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m)
      {
        fprintf(fpFile, "[%d %d %d %d %d] [%d %d %d %d %d %d %d]\n",b,c,d,e,f,g,h,i,j,k,l,m);
      }
      if (b<=c && c<=d && d<=e && e<=f+1 && g<=h && h<=i && i<=j && j<=k && k<=l && l<=m+1)
      {
        fprintf(fpFile, "[%d %d %d %d %d] [%d %d %d %d %d %d %d]\n",b,c,d,e,f+1,g,h,i,j,k,l,m+1);
      }
    }
  }
  fclose(fpFile);
  system("sort test /O test2");

  fpFile = fopen("test2", "rt");
  oldstr[0] = 0;
  while (!feof(fpFile))
  {
    fgets(newstr, 100, fpFile);
    if (strcmp(newstr, oldstr))
    {
      if (count == 29)
      {
        printf("%s", oldstr);
      }
      count = 0;
      strcpy(oldstr, newstr);
    }
    count++;
  }
}
After 13 minutes writing to the intermediate file (353 108 292 bytes) and 1 minute more for the sort process, the program found the following solutions:

[0 1 2 3 5] [0 1 1 1 2 3 3]
[0 1 3 4 5] [0 1 1 2 3 3 3]
[0 2 3 4 6] [1 1 1 2 3 3 4]
[0 2 4 5 6] [1 1 2 3 3 3 4]
[1 1 1 5 5] [0 1 2 2 2 2 4]
[1 1 3 3 4] [0 0 1 1 3 3 4]
[1 1 5 5 5] [0 1 3 3 3 3 4]
[1 2 2 4 4] [0 0 1 2 2 4 4]
[1 2 3 4 6] [1 1 1 2 3 3 5]
[1 2 3 5 6] [0 2 2 2 3 4 4]
[1 2 3 5 7] [1 2 2 2 3 4 4]
[1 2 4 5 6] [1 1 2 3 3 3 5]
[1 3 4 5 6] [0 2 2 3 4 4 4]
[1 3 4 5 7] [1 2 2 3 4 4 4]
[2 2 2 6 6] [1 2 2 2 2 4 5]
[2 2 4 4 5] [0 1 1 3 3 4 5]
[2 2 6 6 6] [1 3 3 3 3 4 5]
[2 3 3 5 5] [0 1 2 2 4 4 5]
[2 3 4 6 7] [2 2 2 3 4 4 5]
[2 4 5 6 7] [2 2 3 4 4 4 5]
[3 3 5 5 6] [1 1 3 3 4 5 5]
[3 4 4 6 6] [1 2 2 4 4 5 5]

I think this time there should be several independent solutions.
alpertron is offline   Reply With Quote
Old 2015-01-02, 19:41   #14
Xyzzy
 
Xyzzy's Avatar
 
"Mike"
Aug 2002

25·257 Posts
Default

http://domino.research.ibm.com/Comm/...ember2014.html
Xyzzy is offline   Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
December 2017 Batalov Puzzles 4 2018-01-04 04:33
December 2016 Xyzzy Puzzles 11 2017-01-24 12:27
December 2015 Xyzzy Puzzles 15 2016-01-06 10:23
Conference in Amsterdam 1-2 December fivemack Information & Answers 6 2011-12-12 13:13
Server update in December ltd Prime Sierpinski Project 4 2010-12-17 13:14

All times are UTC. The time now is 03:18.


Sat Jul 17 03:18:20 UTC 2021 up 50 days, 1:05, 1 user, load averages: 0.94, 1.29, 1.31

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2021, Jelsoft Enterprises Ltd.

This forum has received and complied with 0 (zero) government requests for information.

Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation.
A copy of the license is included in the FAQ.