3 deleted 2 characters in body
source | link

So, I was curious about how much faster I could make the c++ from @Riley. Apparently about 1700 times faster (from 20.4s to 0.012s). And probably more if I had applied some memoization to gcd()

#include <iostream>
#include <stdlib.h>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  int found=0;
  for(int a = 1;   a < 299;     a++) { if(!found){
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ){
      cout << a << " " << b << " " << c << " " << d << endl;
      found=1;exit(0);
    }
  }}}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

So, I was curious about how much faster I could make the c++ from @Riley. Apparently about 1700 times faster (from 20.4s to 0.012s). And probably more if I had applied some memoization to gcd()

#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  int found=0;
  for(int a = 1;   a < 299;     a++) { if(!found){
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ){
      cout << a << " " << b << " " << c << " " << d << endl;
      found=1;
    }
  }}}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

So, I was curious about how much faster I could make the c++ from @Riley. Apparently about 1700 times faster (from 20.4s to 0.012s). And probably more if I had applied some memoization to gcd()

#include <iostream>
#include <stdlib.h>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  for(int a = 1;   a < 299;     a++) {
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ){
      cout << a << " " << b << " " << c << " " << d << endl;
      exit(0);
    }
  }}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

2 deleted 14 characters in body
source | link

So, I was curious about how much faster I could make the c++ from @Riley, apparently. Apparently about 7291700 times faster (from 20.4s to 0.028s012s). And probably more if I had applied some memoization to gcd()

#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  for(int a = 1;   a < 299;     a++) {
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ) {
      cout << a << " " << b << " " << c << " " << d << endl;
    }
  }}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */


#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  int found=0;
  for(int a = 1;   a < 299;     a++) { if(!found){
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ){
      cout << a << " " << b << " " << c << " " << d << endl;
      found=1;
    }
  }}}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

So, I was curious about how much faster I could make the c++ from @Riley, apparently about 729 times faster (from 20.4s to 0.028s). And probably more if I had applied some memoization to gcd()

#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  for(int a = 1;   a < 299;     a++) {
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ) {
      cout << a << " " << b << " " << c << " " << d << endl;
    }
  }}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

So, I was curious about how much faster I could make the c++ from @Riley. Apparently about 1700 times faster (from 20.4s to 0.012s). And probably more if I had applied some memoization to gcd()


#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  int found=0;
  for(int a = 1;   a < 299;     a++) { if(!found){
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ){
      cout << a << " " << b << " " << c << " " << d << endl;
      found=1;
    }
  }}}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */

1
source | link

So, I was curious about how much faster I could make the c++ from @Riley, apparently about 729 times faster (from 20.4s to 0.028s). And probably more if I had applied some memoization to gcd()

#include <iostream>
using namespace std;
int gcd(int a, int b) {
  for (;;) {
    if (a == 0) return b; b %= a;
    if (b == 0) return a; a %= b;
  }
}
int main() {
  for(int a = 1;   a < 299;     a++) {
  for(int b = a+2; b < 299-a;   b++) { if(gcd(a,b)!=1){
  for(int c = b+2; c < 299-a-b; c++) { if(gcd(b,c)!=1 && gcd(a,c)==1){
  for(int d = c+2; d < 299-a-b; d++) {
    if( a+b+c+d==299 && gcd(c,d)!=1 && gcd(d,a)!=1 && gcd(b,d)==1 ) {
      cout << a << " " << b << " " << c << " " << d << endl;
    }
  }}}}}}
}
/*run: g++ puzzle.cpp && time ./a.out */