Advertisement
dmilicev

numbers_1-9_triangle_sum_17_v1.c

Apr 19th, 2020
477
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.24 KB | None
  1. /*
  2.  
  3.     numbers_1-9_triangle_sum_17_v1.c
  4.  
  5.     How to fill the triangle with 1, 2, 3, 4, 5, 6, 7, 8 and 9
  6.     to get the sum of each side equal to 17 ?
  7.  
  8.                 x4
  9.             x3      x5
  10.         x2              x6
  11.     x1      x9      x8      x7
  12.  
  13.     https://web.facebook.com/photo.php?fbid=130470831905101&set=gm.2637750503211777&type=3&theater
  14.  
  15.     Brute force algorithm.
  16.  
  17.  
  18.     You can find all my C programs at Dragan Milicev's pastebin:
  19.  
  20.     https://pastebin.com/u/dmilicev
  21.  
  22. */
  23.  
  24. #include <stdio.h>
  25.  
  26. int main(void)
  27. {
  28.     int i, j, different, count=0, x[9];
  29.     unsigned number_of_attempts=0;
  30.  
  31.     for( x[0]=1; x[0]<=9; x[0]++)
  32.      for( x[1]=1; x[1]<=9; x[1]++)
  33.       for( x[2]=1; x[2]<=9; x[2]++)
  34.        for( x[3]=1; x[3]<=9; x[3]++)
  35.         for( x[4]=1; x[4]<=9; x[4]++)
  36.          for( x[5]=1; x[5]<=9; x[5]++)
  37.           for( x[6]=1; x[6]<=9; x[6]++)
  38.            for( x[7]=1; x[7]<=9; x[7]++)
  39.             for( x[8]=1; x[8]<=9; x[8]++)
  40.             {
  41.                 number_of_attempts++;
  42.                 different=1;
  43.  
  44.                 for( i=0; i<9; i++)     // Check that all numbers 1 to 9 are different
  45.                 {
  46.                     for( j=i+1; j<9; j++)
  47.                     {
  48.                         if( x[i] == x[j] )
  49.                             different=0;
  50.                     }
  51.                     if(different==0)
  52.                         break;
  53.                 }
  54.  
  55.                 if(different)           // if they are different
  56.                 {                       // and if they satisfy the condition
  57.                     if( x[0] + x[1] + x[2] + x[3] == 17 &&
  58.                         x[3] + x[4] + x[5] + x[6] == 17 &&
  59.                         x[6] + x[7] + x[8] + x[0] == 17     )
  60.                     {
  61.                             count++;    // count and display them
  62.                             printf("\n---%3d. solution: --------\n\n", count);
  63.                             printf("............%d\n", x[3]);
  64.                             printf("........%d.......%d\n", x[2],x[4]);
  65.                             printf("....%d...............%d\n", x[1],x[5]);
  66.                             printf("%d.......%d.......%d.......%d\n", x[0],x[8],x[7],x[6]);
  67.                     }
  68.                 } // if(different)
  69.             } // for( x[8]=1; x[8]<=9; x[8]++)
  70.  
  71.  
  72.     printf("\n Number of attempts is 9^9 = %ld \n", number_of_attempts);
  73.     printf("\n There are %d solutions. \n", count);
  74.  
  75.     return 0;
  76.  
  77. } // main()
  78.  
  79. /*
  80.  
  81. The computer program I created to accomplish this task says that there are 96 solutions, which are:
  82.  
  83. ---  1. solution: ------
  84.  
  85. ............3
  86. ........9......5
  87. ....4..............7
  88. 1......8......6......2
  89.  
  90. ---  2. solution: ------
  91.  
  92. ............3
  93. ........9......5
  94. ....4..............7
  95. 1......6......8......2
  96.  
  97. ---  3. solution: ------
  98.  
  99. ............3
  100. ........9......7
  101. ....4..............5
  102. 1......8......6......2
  103.  
  104. ---  4. solution: ------
  105.  
  106. ............3
  107. ........9......7
  108. ....4..............5
  109. 1......6......8......2
  110.  
  111. ---  5. solution: ------
  112.  
  113. ............2
  114. ........9......4
  115. ....5..............8
  116. 1......7......6......3
  117.  
  118. ---  6. solution: ------
  119.  
  120. ............2
  121. ........9......4
  122. ....5..............8
  123. 1......6......7......3
  124.  
  125. ---  7. solution: ------
  126.  
  127. ............2
  128. ........9......8
  129. ....5..............4
  130. 1......7......6......3
  131.  
  132. ---  8. solution: ------
  133.  
  134. ............2
  135. ........9......8
  136. ....5..............4
  137. 1......6......7......3
  138.  
  139. ---  9. solution: ------
  140.  
  141. ............3
  142. ........7......4
  143. ....6..............8
  144. 1......9......5......2
  145.  
  146. --- 10. solution: ------
  147.  
  148. ............3
  149. ........7......4
  150. ....6..............8
  151. 1......5......9......2
  152.  
  153. --- 11. solution: ------
  154.  
  155. ............3
  156. ........7......8
  157. ....6..............4
  158. 1......9......5......2
  159.  
  160. --- 12. solution: ------
  161.  
  162. ............3
  163. ........7......8
  164. ....6..............4
  165. 1......5......9......2
  166.  
  167. --- 13. solution: ------
  168.  
  169. ............2
  170. ........8......5
  171. ....6..............7
  172. 1......9......4......3
  173.  
  174. --- 14. solution: ------
  175.  
  176. ............2
  177. ........8......5
  178. ....6..............7
  179. 1......4......9......3
  180.  
  181. --- 15. solution: ------
  182.  
  183. ............2
  184. ........8......7
  185. ....6..............5
  186. 1......9......4......3
  187.  
  188. --- 16. solution: ------
  189.  
  190. ............2
  191. ........8......7
  192. ....6..............5
  193. 1......4......9......3
  194.  
  195. --- 17. solution: ------
  196.  
  197. ............3
  198. ........6......4
  199. ....7..............8
  200. 1......9......5......2
  201.  
  202. --- 18. solution: ------
  203.  
  204. ............3
  205. ........6......4
  206. ....7..............8
  207. 1......5......9......2
  208.  
  209. --- 19. solution: ------
  210.  
  211. ............3
  212. ........6......8
  213. ....7..............4
  214. 1......9......5......2
  215.  
  216. --- 20. solution: ------
  217.  
  218. ............3
  219. ........6......8
  220. ....7..............4
  221. 1......5......9......2
  222.  
  223. --- 21. solution: ------
  224.  
  225. ............2
  226. ........6......5
  227. ....8..............7
  228. 1......9......4......3
  229.  
  230. --- 22. solution: ------
  231.  
  232. ............2
  233. ........6......5
  234. ....8..............7
  235. 1......4......9......3
  236.  
  237. --- 23. solution: ------
  238.  
  239. ............2
  240. ........6......7
  241. ....8..............5
  242. 1......9......4......3
  243.  
  244. --- 24. solution: ------
  245.  
  246. ............2
  247. ........6......7
  248. ....8..............5
  249. 1......4......9......3
  250.  
  251. --- 25. solution: ------
  252.  
  253. ............3
  254. ........4......5
  255. ....9..............7
  256. 1......8......6......2
  257.  
  258. --- 26. solution: ------
  259.  
  260. ............3
  261. ........4......5
  262. ....9..............7
  263. 1......6......8......2
  264.  
  265. --- 27. solution: ------
  266.  
  267. ............3
  268. ........4......7
  269. ....9..............5
  270. 1......8......6......2
  271.  
  272. --- 28. solution: ------
  273.  
  274. ............3
  275. ........4......7
  276. ....9..............5
  277. 1......6......8......2
  278.  
  279. --- 29. solution: ------
  280.  
  281. ............2
  282. ........5......4
  283. ....9..............8
  284. 1......7......6......3
  285.  
  286. --- 30. solution: ------
  287.  
  288. ............2
  289. ........5......4
  290. ....9..............8
  291. 1......6......7......3
  292.  
  293. --- 31. solution: ------
  294.  
  295. ............2
  296. ........5......8
  297. ....9..............4
  298. 1......7......6......3
  299.  
  300. --- 32. solution: ------
  301.  
  302. ............2
  303. ........5......8
  304. ....9..............4
  305. 1......6......7......3
  306.  
  307. --- 33. solution: ------
  308.  
  309. ............3
  310. ........8......6
  311. ....4..............7
  312. 2......9......5......1
  313.  
  314. --- 34. solution: ------
  315.  
  316. ............3
  317. ........8......6
  318. ....4..............7
  319. 2......5......9......1
  320.  
  321. --- 35. solution: ------
  322.  
  323. ............3
  324. ........8......7
  325. ....4..............6
  326. 2......9......5......1
  327.  
  328. --- 36. solution: ------
  329.  
  330. ............3
  331. ........8......7
  332. ....4..............6
  333. 2......5......9......1
  334.  
  335. --- 37. solution: ------
  336.  
  337. ............3
  338. ........7......4
  339. ....5..............9
  340. 2......8......6......1
  341.  
  342. --- 38. solution: ------
  343.  
  344. ............3
  345. ........7......4
  346. ....5..............9
  347. 2......6......8......1
  348.  
  349. --- 39. solution: ------
  350.  
  351. ............3
  352. ........7......9
  353. ....5..............4
  354. 2......8......6......1
  355.  
  356. --- 40. solution: ------
  357.  
  358. ............3
  359. ........7......9
  360. ....5..............4
  361. 2......6......8......1
  362.  
  363. --- 41. solution: ------
  364.  
  365. ............1
  366. ........9......6
  367. ....5..............7
  368. 2......8......4......3
  369.  
  370. --- 42. solution: ------
  371.  
  372. ............1
  373. ........9......6
  374. ....5..............7
  375. 2......4......8......3
  376.  
  377. --- 43. solution: ------
  378.  
  379. ............1
  380. ........9......7
  381. ....5..............6
  382. 2......8......4......3
  383.  
  384. --- 44. solution: ------
  385.  
  386. ............1
  387. ........9......7
  388. ....5..............6
  389. 2......4......8......3
  390.  
  391. --- 45. solution: ------
  392.  
  393. ............1
  394. ........8......4
  395. ....6..............9
  396. 2......7......5......3
  397.  
  398. --- 46. solution: ------
  399.  
  400. ............1
  401. ........8......4
  402. ....6..............9
  403. 2......5......7......3
  404.  
  405. --- 47. solution: ------
  406.  
  407. ............1
  408. ........8......9
  409. ....6..............4
  410. 2......7......5......3
  411.  
  412. --- 48. solution: ------
  413.  
  414. ............1
  415. ........8......9
  416. ....6..............4
  417. 2......5......7......3
  418.  
  419. --- 49. solution: ------
  420.  
  421. ............3
  422. ........5......4
  423. ....7..............9
  424. 2......8......6......1
  425.  
  426. --- 50. solution: ------
  427.  
  428. ............3
  429. ........5......4
  430. ....7..............9
  431. 2......6......8......1
  432.  
  433. --- 51. solution: ------
  434.  
  435. ............3
  436. ........5......9
  437. ....7..............4
  438. 2......8......6......1
  439.  
  440. --- 52. solution: ------
  441.  
  442. ............3
  443. ........5......9
  444. ....7..............4
  445. 2......6......8......1
  446.  
  447. --- 53. solution: ------
  448.  
  449. ............3
  450. ........4......6
  451. ....8..............7
  452. 2......9......5......1
  453.  
  454. --- 54. solution: ------
  455.  
  456. ............3
  457. ........4......6
  458. ....8..............7
  459. 2......5......9......1
  460.  
  461. --- 55. solution: ------
  462.  
  463. ............3
  464. ........4......7
  465. ....8..............6
  466. 2......9......5......1
  467.  
  468. --- 56. solution: ------
  469.  
  470. ............3
  471. ........4......7
  472. ....8..............6
  473. 2......5......9......1
  474.  
  475. --- 57. solution: ------
  476.  
  477. ............1
  478. ........6......4
  479. ....8..............9
  480. 2......7......5......3
  481.  
  482. --- 58. solution: ------
  483.  
  484. ............1
  485. ........6......4
  486. ....8..............9
  487. 2......5......7......3
  488.  
  489. --- 59. solution: ------
  490.  
  491. ............1
  492. ........6......9
  493. ....8..............4
  494. 2......7......5......3
  495.  
  496. --- 60. solution: ------
  497.  
  498. ............1
  499. ........6......9
  500. ....8..............4
  501. 2......5......7......3
  502.  
  503. --- 61. solution: ------
  504.  
  505. ............1
  506. ........5......6
  507. ....9..............7
  508. 2......8......4......3
  509.  
  510. --- 62. solution: ------
  511.  
  512. ............1
  513. ........5......6
  514. ....9..............7
  515. 2......4......8......3
  516.  
  517. --- 63. solution: ------
  518.  
  519. ............1
  520. ........5......7
  521. ....9..............6
  522. 2......8......4......3
  523.  
  524. --- 64. solution: ------
  525.  
  526. ............1
  527. ........5......7
  528. ....9..............6
  529. 2......4......8......3
  530.  
  531. --- 65. solution: ------
  532.  
  533. ............2
  534. ........8......5
  535. ....4..............9
  536. 3......7......6......1
  537.  
  538. --- 66. solution: ------
  539.  
  540. ............2
  541. ........8......5
  542. ....4..............9
  543. 3......6......7......1
  544.  
  545. --- 67. solution: ------
  546.  
  547. ............2
  548. ........8......9
  549. ....4..............5
  550. 3......7......6......1
  551.  
  552. --- 68. solution: ------
  553.  
  554. ............2
  555. ........8......9
  556. ....4..............5
  557. 3......6......7......1
  558.  
  559. --- 69. solution: ------
  560.  
  561. ............1
  562. ........9......6
  563. ....4..............8
  564. 3......7......5......2
  565.  
  566. --- 70. solution: ------
  567.  
  568. ............1
  569. ........9......6
  570. ....4..............8
  571. 3......5......7......2
  572.  
  573. --- 71. solution: ------
  574.  
  575. ............1
  576. ........9......8
  577. ....4..............6
  578. 3......7......5......2
  579.  
  580. --- 72. solution: ------
  581.  
  582. ............1
  583. ........9......8
  584. ....4..............6
  585. 3......5......7......2
  586.  
  587. --- 73. solution: ------
  588.  
  589. ............2
  590. ........7......6
  591. ....5..............8
  592. 3......9......4......1
  593.  
  594. --- 74. solution: ------
  595.  
  596. ............2
  597. ........7......6
  598. ....5..............8
  599. 3......4......9......1
  600.  
  601. --- 75. solution: ------
  602.  
  603. ............2
  604. ........7......8
  605. ....5..............6
  606. 3......9......4......1
  607.  
  608. --- 76. solution: ------
  609.  
  610. ............2
  611. ........7......8
  612. ....5..............6
  613. 3......4......9......1
  614.  
  615. --- 77. solution: ------
  616.  
  617. ............1
  618. ........7......5
  619. ....6..............9
  620. 3......8......4......2
  621.  
  622. --- 78. solution: ------
  623.  
  624. ............1
  625. ........7......5
  626. ....6..............9
  627. 3......4......8......2
  628.  
  629. --- 79. solution: ------
  630.  
  631. ............1
  632. ........7......9
  633. ....6..............5
  634. 3......8......4......2
  635.  
  636. --- 80. solution: ------
  637.  
  638. ............1
  639. ........7......9
  640. ....6..............5
  641. 3......4......8......2
  642.  
  643. --- 81. solution: ------
  644.  
  645. ............2
  646. ........5......6
  647. ....7..............8
  648. 3......9......4......1
  649.  
  650. --- 82. solution: ------
  651.  
  652. ............2
  653. ........5......6
  654. ....7..............8
  655. 3......4......9......1
  656.  
  657. --- 83. solution: ------
  658.  
  659. ............2
  660. ........5......8
  661. ....7..............6
  662. 3......9......4......1
  663.  
  664. --- 84. solution: ------
  665.  
  666. ............2
  667. ........5......8
  668. ....7..............6
  669. 3......4......9......1
  670.  
  671. --- 85. solution: ------
  672.  
  673. ............1
  674. ........6......5
  675. ....7..............9
  676. 3......8......4......2
  677.  
  678. --- 86. solution: ------
  679.  
  680. ............1
  681. ........6......5
  682. ....7..............9
  683. 3......4......8......2
  684.  
  685. --- 87. solution: ------
  686.  
  687. ............1
  688. ........6......9
  689. ....7..............5
  690. 3......8......4......2
  691.  
  692. --- 88. solution: ------
  693.  
  694. ............1
  695. ........6......9
  696. ....7..............5
  697. 3......4......8......2
  698.  
  699. --- 89. solution: ------
  700.  
  701. ............2
  702. ........4......5
  703. ....8..............9
  704. 3......7......6......1
  705.  
  706. --- 90. solution: ------
  707.  
  708. ............2
  709. ........4......5
  710. ....8..............9
  711. 3......6......7......1
  712.  
  713. --- 91. solution: ------
  714.  
  715. ............2
  716. ........4......9
  717. ....8..............5
  718. 3......7......6......1
  719.  
  720. --- 92. solution: ------
  721.  
  722. ............2
  723. ........4......9
  724. ....8..............5
  725. 3......6......7......1
  726.  
  727. --- 93. solution: ------
  728.  
  729. ............1
  730. ........4......6
  731. ....9..............8
  732. 3......7......5......2
  733.  
  734. --- 94. solution: ------
  735.  
  736. ............1
  737. ........4......6
  738. ....9..............8
  739. 3......5......7......2
  740.  
  741. --- 95. solution: ------
  742.  
  743. ............1
  744. ........4......8
  745. ....9..............6
  746. 3......7......5......2
  747.  
  748. --- 96. solution: ------
  749.  
  750. ............1
  751. ........4......8
  752. ....9..............6
  753. 3......5......7......2
  754.  
  755. ------------------------
  756.  
  757. */
Advertisement
RAW Paste Data Copied
Advertisement