#include #include #include void division(int num, int num1, char array[], int count); int finbonacci(int input); int main(){ //Declare Variables int fibNum1, fibNum2, fibNum3, loop, loop1, loop2, error, winner, val; char strInputString[100], c, result1[100], result2[100], FloatingPointArray[100]; //initalise variables error = 0; //take input while (error != 1){ error = 0; printf("Number of floating digits required for the Golden section number?\n"); printf("Input: "); for (loop = 0; (c = getchar()) != EOF && c != '\n' && loop < 100; loop++){ if (c >= '0' && c <= '9'){ strInputString[loop] = c; } else { error = 1; } } if (error == 1){ printf("Output: Wrong Input, Please Re-enter\n"); error = 0; for (loop1 = 0; loop < 100; loop++){ strInputString[loop] = '0'; } } else { break; } } //for loop for (loop = 3; loop < 30; loop++){ winner = 0; //get number 1 fibNum2 = fibonacci(loop); //get number 2 fibNum1 = fibonacci(loop-1); int temp = atoi(strInputString); //number of floating points to match upto //do division division(fibNum2, fibNum1, FloatingPointArray, temp+1); for (loop2 = 0; loop2 < temp; loop2++){ result1[loop2] = FloatingPointArray[loop2]; } fibNum3 = fibonacci(loop+1); division(fibNum3, fibNum2, FloatingPointArray, temp+1); for (loop2 = 0; loop2 < temp; loop2++){ result2[loop2] = FloatingPointArray[loop2]; } for (loop2 = 1; loop2 < temp+1; loop2++){ if (result1[loop2] == result2[loop2]){ winner = 1; } else { winner = 0; break; } } if (winner == 1){ printf("hehe"); break; } } if (winner == 1){ printf("Output: Golden section number is: %d (Fibonacci Rank: %d)",atoi(result2) ,loop); } printf("\n"); return 0; } void division(int intInputDividend, int intInputDivider, char FloatingPointArray[], int count){ double remainder = 0; double result; // standard variable initalisation - pretty self explanatory double divider; double dividend; int loop; int count1 = 0; dividend = intInputDividend; divider = intInputDivider; count = count + 1; while ((result = dividend - divider) != 0 && count != 0 ){ //checks to see if the remainder isn't equal to zero, and it has not reached the length of required doubleing points if (result < 0){ FloatingPointArray[count1] = 0; while (count != 0){ for (loop = 1; (result >= divider); loop++){ result = result - divider; // calculates the actual remainder from each time round the loop printf("result = %f\n",result); } FloatingPointArray[count1+1] = loop; count = count -1; ++count1; } return; } for (loop = 1; (result >= divider); loop++){ result = result - divider; // calculates the actual remainder from each time round the loop } remainder = result; FloatingPointArray[count1] = loop; // stores the result into the array count = count - 1; // decrement count ++count1; // increment second counter if (remainder == 0){ while (count != 0){ FloatingPointArray[count1] = 0 ; count = count -1; ++count1; } break; } if (remainder < 1){ dividend = remainder; } if (remainder > 100 && remainder < 1000){ dividend = remainder * 100; } if (remainder > 0 && remainder < 100){ dividend = remainder * 10; //prepare the dividend for the next time around the loop } if (remainder > 1000 && remainder < 10000){ dividend = remainder * 1000; } } } int fibonacci(int input){ int num1, num2, num0, fibLoop; num0 = 0; num1 = 1; for (fibLoop = 1; fibLoop < input; fibLoop++) { num2 = num0 + num1; num0 = num1; num1 = num2; } return(num2); }