Skip to content

Instantly share code, notes, and snippets.

@jamessa
Created October 19, 2012 02:47
Show Gist options
  • Select an option

  • Save jamessa/3915953 to your computer and use it in GitHub Desktop.

Select an option

Save jamessa/3915953 to your computer and use it in GitHub Desktop.
//
// main.c
// NumberPyramid
//
// Created by jamie on 12/10/18.
// Copyright (c) 2012年 jamie. All rights reserved.
//
#include <stdio.h>
static long epoch = 0;
void swap (long *x, long *y)
{
long temp;
temp = *x;
*x = *y;
*y = temp;
}
void print_bucket(long* bucket){
int i=0;
printf("%ld/%ld%ld+%ld/%ld%ld+%ld/%ld%ld\n", bucket[i++], bucket[i++], bucket[i++], bucket[i++], bucket[i++], bucket[i++], bucket[i++], bucket[i++], bucket[i++]);
}
void permute(long *a, int i, int n)
{
int j;
if (i == n){
epoch++;
long a1 = a[0];
long a4 = a[3];
long a7 = a[6];
long a23 = 10*a[1]+a[2];
long a56 = 10*a[4]+a[5];
long a89 = 10*a[7]+a[8];
if ( a1<a4 && a4<a7 && (a1*a56*a89 + a4*a23*a89 + a7*a23*a56)==a23*a56*a89 ){
print_bucket(a);
}
}
else
{
for (j = i; j <= n; j++)
{
swap((a+i), (a+j));
permute(a, i+1, n);
swap((a+i), (a+j)); //backtrack
}
}
}
int main(int argc, const char * argv[])
{
long bucket[9]={1,2,3,4,5,6,7,8,9};
permute(bucket, 0, 8);
printf("After %ld permutations", epoch);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment