parent
693ee1e4ae
commit
6ca700a8ef
@ -51,9 +51,6 @@ int factorial(int numb) {
|
|||||||
|
|
||||||
int main(int argc,char**argv) {
|
int main(int argc,char**argv) {
|
||||||
|
|
||||||
//For permutations, we know that there are a total of N! permutations.
|
|
||||||
// Which means each individual set starts at N!/N. So we can skip ahead some permutations.
|
|
||||||
// For 10 numbers, we get 362,880 values per permutation set. So 1000000/362880 = 2. So we'll start at permutation two.
|
|
||||||
int setPermutationCount=factorial(10)/10;
|
int setPermutationCount=factorial(10)/10;
|
||||||
int permutationCount=(1000000/setPermutationCount)*setPermutationCount;
|
int permutationCount=(1000000/setPermutationCount)*setPermutationCount;
|
||||||
int digits[10]={2,0,1,3,4,5,6,7,8,9};
|
int digits[10]={2,0,1,3,4,5,6,7,8,9};
|
||||||
@ -61,11 +58,11 @@ int main(int argc,char**argv) {
|
|||||||
incrementDigit(digits);
|
incrementDigit(digits);
|
||||||
if (allUniqueDigits(digits)) {
|
if (allUniqueDigits(digits)) {
|
||||||
permutationCount++;
|
permutationCount++;
|
||||||
printf("%d:",permutationCount);
|
/*printf("%d:",permutationCount);
|
||||||
for (int i=0;i<10;i++) {
|
for (int i=0;i<10;i++) {
|
||||||
printf(" %d ",digits[i]);
|
printf(" %d ",digits[i]);
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n");*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
BIN
archives/25/current
Executable file
BIN
archives/25/current
Executable file
Binary file not shown.
44
archives/25/src/main.c
Normal file
44
archives/25/src/main.c
Normal file
@ -0,0 +1,44 @@
|
|||||||
|
#include <stdio.h>
|
||||||
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
/*
|
||||||
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
|
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
||||||
|
Hence the first 12 terms will be:
|
||||||
|
|
||||||
|
F1 = 1
|
||||||
|
F2 = 1
|
||||||
|
F3 = 2
|
||||||
|
F4 = 3
|
||||||
|
F5 = 5
|
||||||
|
F6 = 8
|
||||||
|
F7 = 13
|
||||||
|
F8 = 21
|
||||||
|
F9 = 34
|
||||||
|
F10 = 55
|
||||||
|
F11 = 89
|
||||||
|
F12 = 144
|
||||||
|
The 12th term, F12, is the first term to contain three digits.
|
||||||
|
|
||||||
|
What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
||||||
|
|
||||||
|
https://projecteuler.net/problem=24
|
||||||
|
*/
|
||||||
|
|
||||||
|
int main(int argc,char**argv) {
|
||||||
|
struct String numb1 = BigNumber(1);
|
||||||
|
struct String numb2 = BigNumber(1);
|
||||||
|
int term=3;
|
||||||
|
while (numb2.length<1000) {
|
||||||
|
struct String oldString = numb1;
|
||||||
|
numb1 = numb2;
|
||||||
|
numb2 = add(oldString,numb1);
|
||||||
|
//free(oldString.str);
|
||||||
|
//printf("%d: %s\n",term,numb2.str);
|
||||||
|
term++;
|
||||||
|
}
|
||||||
|
printf("\n\nTerm %d has %d digits!",term,numb2.length);
|
||||||
|
return 0;
|
||||||
|
}
|
140
archives/25/src/utils.c
Normal file
140
archives/25/src/utils.c
Normal file
@ -0,0 +1,140 @@
|
|||||||
|
#include "utils.h"
|
||||||
|
#include <stdio.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct String mult(struct String numb1, struct String numb2) {
|
||||||
|
struct String n1 = numb1;
|
||||||
|
struct String n2 = numb2;
|
||||||
|
byte carryover = 0;
|
||||||
|
if (numb2.length>numb1.length) {
|
||||||
|
n1=numb2;
|
||||||
|
n2=numb1;
|
||||||
|
}
|
||||||
|
int addends[n2.length][n1.length+1];
|
||||||
|
for (int i=0;i<n2.length;i++) {
|
||||||
|
for (int j=0;j<n1.length+1;j++) {
|
||||||
|
addends[i][j]=0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
for (int i=n2.length-1;i>=0;i--) {
|
||||||
|
carryover=0;
|
||||||
|
for (int j=n1.length-1;j>=0;j--) {
|
||||||
|
int mult = (n1.str[j]-'0')*(n2.str[i]-'0')+((carryover!=0)?carryover:0);
|
||||||
|
//printf(" %d/%d\n",mult,carryover);
|
||||||
|
carryover=0;
|
||||||
|
if (mult>=10) {
|
||||||
|
carryover=mult/10;
|
||||||
|
mult=mult%10;
|
||||||
|
}
|
||||||
|
addends[(n2.length-1)-i][j+1]=mult;
|
||||||
|
}
|
||||||
|
if (carryover>0) {
|
||||||
|
addends[(n2.length-1)-i][0]=carryover;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
//printIntDoubleArr(n2.length,n1.length+1,addends);
|
||||||
|
struct String sum = {1,"0"};
|
||||||
|
for (int i=0;i<n2.length;i++) {
|
||||||
|
char val[n1.length+1+i];
|
||||||
|
for (int j=0;j<n1.length+1+i;j++) {
|
||||||
|
val[j]='0';
|
||||||
|
}
|
||||||
|
for (int j=0;j<n1.length+1;j++) {
|
||||||
|
val[j]=addends[i][j]+'0';
|
||||||
|
}
|
||||||
|
sum=add((struct String){n1.length+1+i,val},sum);
|
||||||
|
//printf("%s\n",sum.str);
|
||||||
|
}
|
||||||
|
if (sum.str[0]=='0') {
|
||||||
|
char*newStr=malloc(sum.length-1);
|
||||||
|
for (int i=1;i<sum.length;i++) {
|
||||||
|
newStr[i-1]=sum.str[i];
|
||||||
|
}
|
||||||
|
free(sum.str);
|
||||||
|
sum=(struct String){sum.length-1,newStr};
|
||||||
|
}
|
||||||
|
//printf("%s",sum.str);
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
struct String add(struct String numb1, struct String numb2){
|
||||||
|
//printf("%s %s\n",numb1.str,numb2.str);
|
||||||
|
byte carryover=0;
|
||||||
|
int digitCount=0;
|
||||||
|
char*str = malloc(1);
|
||||||
|
//str[0]='\0';
|
||||||
|
if (numb1.length>=numb2.length) {
|
||||||
|
for (int offset=0;offset<numb1.length;offset++) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
//printf("Digit count is now %d\n",digitCount);
|
||||||
|
if (numb2.length>offset) {
|
||||||
|
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
|
||||||
|
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
|
||||||
|
if (sum>=10) {
|
||||||
|
carryover=1;
|
||||||
|
sum-=10;
|
||||||
|
}
|
||||||
|
str[offset]=sum+'0';
|
||||||
|
} else {
|
||||||
|
str[offset]=numb1.str[numb1.length-offset-1]+((carryover>0)?carryover--:0);
|
||||||
|
}
|
||||||
|
//str[offset+1]='\0';
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
for (int offset=0;offset<numb2.length;offset++) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
//printf("Digit count is now %d\n",digitCount);
|
||||||
|
if (numb1.length>offset) {
|
||||||
|
//printf("%c %c\n",numb1.str[numb1.length-offset-1],numb2.str[numb2.length-offset-1]);
|
||||||
|
int sum=((numb1.str[numb1.length-offset-1]-'0')+(numb2.str[numb2.length-offset-1]-'0'))+((carryover>0)?carryover--:0);
|
||||||
|
if (sum>=10) {
|
||||||
|
carryover=1;
|
||||||
|
sum-=10;
|
||||||
|
}
|
||||||
|
str[offset]=sum+'0';
|
||||||
|
} else {
|
||||||
|
str[offset]=numb2.str[numb2.length-offset-1]+((carryover>0)?carryover--:0);
|
||||||
|
}
|
||||||
|
//str[offset+1]='\0';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (carryover>0) {
|
||||||
|
str = realloc(str,++digitCount);
|
||||||
|
str[digitCount-1]='1';
|
||||||
|
//str[digitCount]='\0';
|
||||||
|
}
|
||||||
|
for (int i=0;i<digitCount/2;i++) {
|
||||||
|
char c = str[i];
|
||||||
|
char c2 = str[digitCount-i-1];
|
||||||
|
str[digitCount-i-1]=c;
|
||||||
|
str[i]=c2;
|
||||||
|
}
|
||||||
|
str = realloc(str,digitCount+1);
|
||||||
|
str[digitCount]='\0';
|
||||||
|
struct String newStr = {digitCount,str};
|
||||||
|
return newStr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void printLongDoubleArr(int a,int b,long doubleArr[a][b]) {
|
||||||
|
for (int i=0;i<a;i++) {
|
||||||
|
for (int j=0;j<b;j++) {
|
||||||
|
printf("%ld\t",doubleArr[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void printIntDoubleArr(int a,int b,int doubleArr[a][b]) {
|
||||||
|
for (int i=0;i<a;i++) {
|
||||||
|
for (int j=0;j<b;j++) {
|
||||||
|
printf("%d\t",doubleArr[i][j]);
|
||||||
|
}
|
||||||
|
printf("\n");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
struct String createBigNumber(char*numb) {
|
||||||
|
int marker=0;
|
||||||
|
while (numb[marker++]!='\0');
|
||||||
|
return (struct String){marker-1,numb};
|
||||||
|
}
|
14
archives/25/src/utils.h
Normal file
14
archives/25/src/utils.h
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
#define true 1
|
||||||
|
#define false 0
|
||||||
|
#define boolean char
|
||||||
|
#define byte char
|
||||||
|
struct String{
|
||||||
|
int length;
|
||||||
|
char*str;
|
||||||
|
};
|
||||||
|
struct String add(struct String numb1, struct String numb2);
|
||||||
|
struct String mult(struct String numb1, struct String numb2);
|
||||||
|
void printLongDoubleArr(int a,int b,long doubleArr[a][b]);
|
||||||
|
void printIntDoubleArr(int a,int b,int doubleArr[a][b]);
|
||||||
|
struct String createBigNumber(char*numb);
|
||||||
|
#define BigNumber(X) createBigNumber(#X)
|
95
src/main.c
95
src/main.c
@ -1,79 +1,44 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include "utils.h"
|
#include "utils.h"
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
/*
|
/*
|
||||||
A permutation is an ordered arrangement of objects. For example, 3124 is one possible permutation of the digits 1, 2, 3 and 4. If all of the permutations are listed numerically or alphabetically, we call it lexicographic order. The lexicographic permutations of 0, 1 and 2 are:
|
The Fibonacci sequence is defined by the recurrence relation:
|
||||||
|
|
||||||
012 021 102 120 201 210
|
Fn = Fn−1 + Fn−2, where F1 = 1 and F2 = 1.
|
||||||
|
Hence the first 12 terms will be:
|
||||||
|
|
||||||
What is the millionth lexicographic permutation of the digits 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9?
|
F1 = 1
|
||||||
|
F2 = 1
|
||||||
|
F3 = 2
|
||||||
|
F4 = 3
|
||||||
|
F5 = 5
|
||||||
|
F6 = 8
|
||||||
|
F7 = 13
|
||||||
|
F8 = 21
|
||||||
|
F9 = 34
|
||||||
|
F10 = 55
|
||||||
|
F11 = 89
|
||||||
|
F12 = 144
|
||||||
|
The 12th term, F12, is the first term to contain three digits.
|
||||||
|
|
||||||
|
What is the index of the first term in the Fibonacci sequence to contain 1000 digits?
|
||||||
|
|
||||||
https://projecteuler.net/problem=24
|
https://projecteuler.net/problem=24
|
||||||
*/
|
*/
|
||||||
|
|
||||||
void carryover(int*arr,int digit) {
|
|
||||||
arr[digit]=0;
|
|
||||||
if (arr[digit-1]==9) {
|
|
||||||
carryover(arr,digit-1);
|
|
||||||
} else {
|
|
||||||
arr[digit-1]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void incrementDigit(int*arr) {
|
|
||||||
int val = arr[9];
|
|
||||||
if (arr[9]==9) {
|
|
||||||
carryover(arr,9);
|
|
||||||
} else {
|
|
||||||
arr[9]++;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
boolean allUniqueDigits(int*arr) {
|
|
||||||
boolean digits[10] = {};
|
|
||||||
for (int i=0;i<10;i++) {
|
|
||||||
if (!digits[arr[i]]) {
|
|
||||||
digits[arr[i]]=true;
|
|
||||||
} else {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
int factorial(int numb) {
|
|
||||||
int final=1;
|
|
||||||
for (int i=numb;i>=1;i--) {
|
|
||||||
final*=i;
|
|
||||||
}
|
|
||||||
return final;
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc,char**argv) {
|
int main(int argc,char**argv) {
|
||||||
|
struct String numb1 = BigNumber(1);
|
||||||
//For permutations, we know that there are a total of N! permutations.
|
struct String numb2 = BigNumber(1);
|
||||||
// Which means each individual set starts at N!/N. So we can skip ahead some permutations.
|
int term=3;
|
||||||
// For 10 numbers, we get 362,880 values per permutation set. So 1000000/362880 = 2. So we'll start at permutation two.
|
while (numb2.length<1000) {
|
||||||
int setPermutationCount=factorial(10)/10;
|
struct String oldString = numb1;
|
||||||
int permutationCount=(1000000/setPermutationCount)*setPermutationCount;
|
numb1 = numb2;
|
||||||
int digits[10]={2,0,1,3,4,5,6,7,8,9};
|
numb2 = add(oldString,numb1);
|
||||||
while ((digits[0]!=9||digits[1]!=8||digits[2]!=7||digits[3]!=6||digits[4]!=5||digits[5]!=4||digits[6]!=3||digits[7]!=2||digits[8]!=1||digits[9]!=0)&&permutationCount!=999999) {
|
//free(oldString.str);
|
||||||
incrementDigit(digits);
|
//printf("%d: %s\n",term,numb2.str);
|
||||||
if (allUniqueDigits(digits)) {
|
term++;
|
||||||
permutationCount++;
|
|
||||||
printf("%d:",permutationCount);
|
|
||||||
for (int i=0;i<10;i++) {
|
|
||||||
printf(" %d ",digits[i]);
|
|
||||||
}
|
}
|
||||||
printf("\n");
|
printf("\n\nTerm %d has %d digits!",term,numb2.length);
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
printf("\n\nThe one millionth lexicographic permutation is: \n\t");
|
|
||||||
for (int i=0;i<10;i++) {
|
|
||||||
printf("%d",digits[i]);
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user