Skip to main content
replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

This is a follow up on my previous questionquestion.

JS1's answerJS1's answer suggested that I should use a precomputed table containing all permutations of the lowest valid number for each number between 1 and MAX. It was well described how to implement the precomputed table but somewhere I seem to have miserably failed since the code runs much slower than my previous versions of the code.

I don't believe there's anything wrong with my lookup table but if that's for any use here is the code I wrote to obtain all permutations for numbers between 1 and MAX.

Something with my code seems off because for larger calculations (the example found in my previous question) the code runs terribly slow.

#include <stdio.h>

int lookup[] = {
#include "table.h"
};

#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define MAX 1000000

int main(void) {
  int n[100];
  int x, i, j, k, l, m, N;

  scanf("%d", &N);
  for(i=0;i<N;i++) scanf("%d", &n[i]);
  for(x=1;x<MAX;x++) {
    j=x-1;
    i=-x;
    while((unsigned int)(j) < LENGTH(lookup) && lookup[j] != i) ++j;
    for(l=0;l<N;l++) {
      m = j;
      while((k = lookup[++m]) > 0) {
        if(k%n[l]==0)break;
      }
      if(k<0) goto outer;
    }
    break;
    outer:
    continue;
  }
  fprintf(stdout, "%d\n", x);
}

Perhaps the access time on the array is affecting the speed?

This is a follow up on my previous question.

JS1's answer suggested that I should use a precomputed table containing all permutations of the lowest valid number for each number between 1 and MAX. It was well described how to implement the precomputed table but somewhere I seem to have miserably failed since the code runs much slower than my previous versions of the code.

I don't believe there's anything wrong with my lookup table but if that's for any use here is the code I wrote to obtain all permutations for numbers between 1 and MAX.

Something with my code seems off because for larger calculations (the example found in my previous question) the code runs terribly slow.

#include <stdio.h>

int lookup[] = {
#include "table.h"
};

#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define MAX 1000000

int main(void) {
  int n[100];
  int x, i, j, k, l, m, N;

  scanf("%d", &N);
  for(i=0;i<N;i++) scanf("%d", &n[i]);
  for(x=1;x<MAX;x++) {
    j=x-1;
    i=-x;
    while((unsigned int)(j) < LENGTH(lookup) && lookup[j] != i) ++j;
    for(l=0;l<N;l++) {
      m = j;
      while((k = lookup[++m]) > 0) {
        if(k%n[l]==0)break;
      }
      if(k<0) goto outer;
    }
    break;
    outer:
    continue;
  }
  fprintf(stdout, "%d\n", x);
}

Perhaps the access time on the array is affecting the speed?

This is a follow up on my previous question.

JS1's answer suggested that I should use a precomputed table containing all permutations of the lowest valid number for each number between 1 and MAX. It was well described how to implement the precomputed table but somewhere I seem to have miserably failed since the code runs much slower than my previous versions of the code.

I don't believe there's anything wrong with my lookup table but if that's for any use here is the code I wrote to obtain all permutations for numbers between 1 and MAX.

Something with my code seems off because for larger calculations (the example found in my previous question) the code runs terribly slow.

#include <stdio.h>

int lookup[] = {
#include "table.h"
};

#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define MAX 1000000

int main(void) {
  int n[100];
  int x, i, j, k, l, m, N;

  scanf("%d", &N);
  for(i=0;i<N;i++) scanf("%d", &n[i]);
  for(x=1;x<MAX;x++) {
    j=x-1;
    i=-x;
    while((unsigned int)(j) < LENGTH(lookup) && lookup[j] != i) ++j;
    for(l=0;l<N;l++) {
      m = j;
      while((k = lookup[++m]) > 0) {
        if(k%n[l]==0)break;
      }
      if(k<0) goto outer;
    }
    break;
    outer:
    continue;
  }
  fprintf(stdout, "%d\n", x);
}

Perhaps the access time on the array is affecting the speed?

Source Link
Linus
  • 927
  • 5
  • 18

Optimize program to test for divisibility of numbers 3.0

This is a follow up on my previous question.

JS1's answer suggested that I should use a precomputed table containing all permutations of the lowest valid number for each number between 1 and MAX. It was well described how to implement the precomputed table but somewhere I seem to have miserably failed since the code runs much slower than my previous versions of the code.

I don't believe there's anything wrong with my lookup table but if that's for any use here is the code I wrote to obtain all permutations for numbers between 1 and MAX.

Something with my code seems off because for larger calculations (the example found in my previous question) the code runs terribly slow.

#include <stdio.h>

int lookup[] = {
#include "table.h"
};

#define LENGTH(x) (sizeof(x) / sizeof((x)[0]))
#define MAX 1000000

int main(void) {
  int n[100];
  int x, i, j, k, l, m, N;

  scanf("%d", &N);
  for(i=0;i<N;i++) scanf("%d", &n[i]);
  for(x=1;x<MAX;x++) {
    j=x-1;
    i=-x;
    while((unsigned int)(j) < LENGTH(lookup) && lookup[j] != i) ++j;
    for(l=0;l<N;l++) {
      m = j;
      while((k = lookup[++m]) > 0) {
        if(k%n[l]==0)break;
      }
      if(k<0) goto outer;
    }
    break;
    outer:
    continue;
  }
  fprintf(stdout, "%d\n", x);
}

Perhaps the access time on the array is affecting the speed?