Guest User

Untitled

a guest
Mar 19th, 2018
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.17 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <time.h>
  4. #include <string.h>
  5. #include <math.h>
  6. #include <ctype.h>
  7. #define MINSIZESEARCH 1
  8. #define DIMFIBSTART 100000000
  9. #define DIMFIBSTARTL 100000000000000
  10. #define NDIGIT 9
  11. #define NDIGITL 15
  12. #define MINFINDE 2749
  13.  
  14. /* this function returns the first fib of the dim = dimFib and also it returns
  15. by reference the index k. If there was a problem it returns -1 */
  16. int calcFibOfSize (int dimFib, int *k, int *fnMinus1P);
  17.  
  18. /* this function makes the calculation of the dimension of the counter and then
  19. it returns the number of digits of the counter, it returns the dimension or -1
  20. if there was a problem in the function */
  21. int dimCalc (int counter, int *nDigit);
  22.  
  23. /* this function creates and fills the array with the digits of the number
  24. counter, ckowing that dim and nDigits. It returns the pointer of the array or
  25. NULL if there was a problem in the function */
  26. int *preencheVectorDigit (int counter, int dim, int nDigit);
  27.  
  28. /* this function creates and fills the array with the digits of the number
  29. counter, ckowing that dim and nDigits. It returns the pointer of the array or
  30. NULL if there was a problem in the function */
  31. int *preencheVectorDigitL (long long int counter, long long int dim, int nDigit);
  32.  
  33. /* this functions makes the confirmation the this nAlg digits are in fact a
  34. Pandigital number, if yes returns 1 if not it returns 0, or -1 if there was a
  35. problem in the function */
  36. int pandigitalCheck (int *vector, int nAlg);
  37.  
  38. int main () {
  39.  
  40. clock_t inicio, fim;
  41. double tempo;
  42. inicio = clock();
  43. /* work to verify */
  44. int minSearch = MINSIZESEARCH, fnD, dim;
  45. int fnMinus1D, fnMinus2D, dimFibStart = DIMFIBSTART, divider = dimFibStart*10;
  46. int k, *v, booleano1 = 0, booleano2 = 0, res, nDigit = NDIGIT, answer, found = 0;
  47. long long int fnL, fnMinus1L, fnMinus2L, dimFibStartL = DIMFIBSTARTL;
  48. int nDigitL = NDIGITL, booleanoDigit = 0, minFinde = MINFINDE;
  49.  
  50. fnD = calcFibOfSize (dimFibStart, &k, &fnMinus1D);
  51. /* preparations for enter cycle */
  52. /* fn-2 */
  53. fnMinus2L = fnMinus1D;
  54. fnMinus2D = fnMinus1D;
  55. /* fn-1 */
  56. fnMinus1L = fnD;
  57. fnMinus1D = fnD;
  58. dim = dimFibStart;
  59. while (!found) {
  60. /* right calc */
  61. fnD = fnMinus1D + fnMinus2D;
  62. k++;
  63. /* resize of fnD */
  64. fnD%=divider;
  65. /* nDigit = 9 and dim = 10⁹ */
  66. if (k > minSearch) {
  67. v = preencheVectorDigit (fnD, dim, nDigit);
  68. if (v == NULL) {
  69. printf("\nThere was an error in the function v.");
  70. return 0;
  71. }
  72.  
  73. booleano1 = pandigitalCheck (v, nDigit);
  74. if (booleano1 == -1) {
  75. printf ("\nThere was a problem in the function.");
  76. }
  77. /* free memory */
  78. free (v);
  79. v = NULL;
  80. }
  81. /* next menber */
  82. fnMinus2D = fnMinus1D;
  83. fnMinus1D = fnD;
  84. /* left calc */
  85. fnL = fnMinus1L + fnMinus2L;
  86. /* resize of fnD */
  87. res = fnL / dimFibStartL;
  88. if (res > 9) {
  89. /* overflow, downsize */
  90. fnL/=10;
  91. fnMinus1L/=10;
  92. }
  93. /* next menber */
  94. fnMinus2L = fnMinus1L;
  95. fnMinus1L = fnL;
  96. /* testing conditions if right size is pandigital */
  97. if (booleano1 == 1 && k > minFinde) {
  98. /* nDigitL = 15 and dim = pow(10,15) */
  99. v = preencheVectorDigitL (fnL, dimFibStartL, nDigit);
  100. if (v == NULL) {
  101. printf("\nThere was an error in the function v.");
  102. return 0;
  103. }
  104. booleano2 = pandigitalCheck (v, nDigit);
  105. /* free memory */
  106. free (v);
  107. v = NULL;
  108. if (booleano2 == -1) {
  109. printf ("\nThere was a problem in the function.");
  110. } else if (booleano2 == 1) {
  111. answer = k;
  112. found = 1;
  113. }
  114. }
  115. }
  116. /* end of the work */
  117. fim = clock();
  118. tempo = (double)(fim - inicio) / CLOCKS_PER_SEC;
  119. printf("\nTime in seconds: %lf", tempo);
  120. printf ("\nThe answer is %d.", answer);
  121. printf("\n");
  122. return 0;
  123. }
  124. /******************************************************************************/
  125. int pandigitalCheck (int *vector, int nAlg) {
  126. /* this functions makes the confirmation the this nAlg digits are in fact a
  127. Pandigital number, if yes returns 1 if not it returns 0, or -1 if there was a
  128. problem in the function */
  129. if (vector == NULL) {
  130. return -1;
  131. }
  132. int *vectorCheck;
  133. int i, j;
  134. vectorCheck = (int*) calloc (nAlg, sizeof (int));
  135. if (vectorCheck == NULL) {
  136. printf ("\nError in the memory allocation of vectorCheck.");
  137. return -1;
  138. }
  139. /*fulling of vector check*/
  140. for (i = 0; i < nAlg; i++) {
  141. vectorCheck [i] = i+1;
  142. }
  143. /* check to see if exist the numbers from 1 until nAlg */
  144. for (i = 0; i < nAlg; i++) {
  145. for (j = 0; j < nAlg; j++) {
  146. if (vector[i] == vectorCheck[j]) {
  147. vectorCheck[j] = -1;
  148. }
  149. }
  150. }
  151. for (i = 0; i < nAlg; i++) {
  152. /* no pandigital found */
  153. if (vectorCheck[i] != -1) {
  154. free(vectorCheck);
  155. return 0;
  156. }
  157. }
  158. /* if it arrives here then it is a pandigital number */
  159. free(vectorCheck);
  160. return 1;
  161. }
  162. /******************************************************************************/
  163. int dimCalc (int counter, int *nDigit) {
  164. /* this function makes the calculation of the dimension of the counter and then
  165. it returns the number of digits of the counter, it returns the dimension or -1
  166. if there was a problem in the function */
  167. if (nDigit == NULL || counter < 1) {
  168. return -1;
  169. }
  170. int dim = 1;
  171. int res, nDigitsNumber = 1;
  172. res = counter / dim;
  173. while (res > 9) {
  174. dim = dim * 10;
  175. nDigitsNumber++;
  176. res = counter / dim;
  177. }
  178. *nDigit = nDigitsNumber;
  179. return dim;
  180. }
  181. /******************************************************************************/
  182. int *preencheVectorDigit (int counter, int dim, int nDigit) {
  183. /* this function creates and fills the array with the digits of the number
  184. counter, ckowing that dim and nDigits. It returns the pointer of the array or
  185. NULL if there was a problem in the function */
  186. int *vectorDigit;
  187. int i;
  188. vectorDigit = (int*) calloc (nDigit, sizeof (int));
  189. if (vectorDigit == NULL) {
  190. printf ("\nError in the creation of the array.");
  191. return NULL;
  192. }
  193. for (i = 0; i < nDigit; i++) {
  194. vectorDigit[i] = counter / dim;
  195. counter = counter - vectorDigit[i]*dim;
  196. dim = dim / 10;
  197. }
  198. return vectorDigit;
  199. }
  200. /******************************************************************************/
  201. int calcFibOfSize (int dimFib, int *k, int *fnMinus1P) {
  202. /* this function returns the first fib of the dim = dimFib and also it returns
  203. by reference the index k. If there was a problem it returns -1 */
  204. if (k == NULL || dimFib < 1 || fnMinus1P == NULL) {
  205. return -1;
  206. }
  207. int fn, fnMinus1 = 1, fnMinus2 = 1, kcalc = 2, dim, nDigit;
  208. while (1) {
  209. fn = fnMinus1 + fnMinus2;
  210. kcalc++;
  211. dim = dimCalc (fn, &nDigit);
  212. if (dim == dimFib) {
  213. break;
  214. }
  215. /* next number preparations */
  216. fnMinus2 = fnMinus1;
  217. fnMinus1 = fn;
  218. }
  219. *k = kcalc;
  220. *fnMinus1P = fnMinus1;
  221. return fn;
  222. }
  223. /******************************************************************************/
  224. int *preencheVectorDigitL (long long int counter, long long int dim, int nDigit) {
  225. /* this function creates and fills the array with the digits of the number
  226. counter, ckowing that dim and nDigits. It returns the pointer of the array or
  227. NULL if there was a problem in the function */
  228. int *vectorDigit;
  229. int i;
  230. vectorDigit = (int*) calloc (nDigit, sizeof (int));
  231. if (vectorDigit == NULL) {
  232. printf ("\nError in the creation of the array.");
  233. return NULL;
  234. }
  235. for (i = 0; i < nDigit; i++) {
  236. vectorDigit[i] = counter / dim;
  237. counter = counter - vectorDigit[i]*dim;
  238. dim = dim / 10;
  239. }
  240. return vectorDigit;
  241. }
  242. /******************************************************************************/
Add Comment
Please, Sign In to add comment