If I gave you the numbers 2, 5, 11, ___ could you guess the next number? The human brain starts looking for patterns between the numbers. Most people find that the easiest pattern is the previous number times 2 plus 1.
(2 x 2) + 1 = 5
(5 x 2) + 1 = 11
(11 x 2) + 1 = 23
If you give the same problem to a computer, it finds the next number, but is a very unexpected efficient way. You can alternatively take the number and add a 5 and divide by 5.
25/5 = 5
55/5 = 11
115/5 = 23
As humans, we are restricted by the complexity of the math inside the pattern. We do not try every possible math equation, but instead rely on our amazing ability to spot patterns. Computers however, can calculate complex math fast and efficiently. My goal was to write a program that can find the pattern in any pattern. My first idea was simply to “brute force” the solution by simply trying every possibility. This technique requires no complex logic or equations. Instead of mimicking humans, it uses its own strengths to solve the problem. The following program was made in c# and the full source code is available for download at the bottom of this article.
Step 1: Understanding the problem
The code starts with a bank of numbers and math operations like below:
1 |
const string stringbank = "123456789^+-/*"; |
The program will take the characters in the stringbank and try every single combination/permutation starting with one character, then two, then three etc. For example it would try calculating:
One Character: 1, 2, 3, 4, 5, 6, 7, 8, 9, ^, +, -, /, *
Two Characters: 11, 12, 13, 14, 15, 16, 17, 18, 19, 1^, 1+, 1-, 1/, 1*, 21, 22, 23, 24, 25, 26, 27, 28, 29, 2^, 2+, 2-, 2/, 2*, 31, 32, 33, 34, 35, 36, 37, 38, 39, 3^, 3+, 3-, 3/, 3*, 41, 42, 43, 44, 45, 46, 47, 48, 49, 4^, 4+, 4-, 4/, 4*, 51, 52, 53, 54, 55, 56, 57, 58, 59, 5^, 5+, 5-, 5/, 5*, 61, 62, 63, 64, 65, 66, 67, 68, 69, 6^, 6+, 6-, 6/, 6*, 71, 72, 73, 74, 75, 76, 77, 78, 79, 7^, 7+, 7-, 7/, 7*, 81, 82, 83, 84, 85, 86, 87, 88, 89, 8^, 8+, 8-, 8/, 8*, 91, 92, 93, 94, 95, 96, 97, 98, 99, 9^, 9+, 9-, 9/, 9*, ^1, ^2, ^3, ^4, ^5, ^6, ^7, ^8, ^9, ^^, ^+, ^-, ^/, ^*, +1, +2, +3, +4, +5, +6, +7, +8, +9, +^, ++, +-, +/, +*, -1, -2, -3, -4, -5, -6, -7, -8, -9, -^, -+, –, -/, -*, /1, /2, /3, /4, /5, /6, /7, /8, /9, /^, /+, /-, //, /*, *1, *2, *3, *4, *5, *6, *7, *8, *9, *^, *+, *-, */, **
Three Characters: 111, 112, 113, 114, 115, 116, 117, 118, 119, 11^, 11+, 11-, 11/, 11*, 121, 122, 123, 124, 125, 126, 127, 128, 129, 12^, 12+, 12-, 12/, 12*, 131, 132, 133, 134, 135, 136, 137, 138, 139, 13^, 13+, 13-, 13/, 13*, 141, 142, 143, 144, 145, 146, 147, 148, 149, 14^, 14+, 14-, 14/, 14*, 151, 152, 153, 154, 155, 156, 157, 158, 159, 15^, 15+, 15-, 15/, 15*, 161, 162, 163, 164, 165, 166, 167, 168, 169, 16^, 16+, 16-, 16/, 16*, 171, 172, 173, 174, 175, 176, 177, 178, 179, 17^, 17+, 17-, 17/, 17*, 181, 182, 183, 184, 185, 186, 187, 188, 189, 18^, 18+, 18-, 18/, 18*, 191, 192, 193, 194, 195, 196, 197, 198, 199, 19^, 19+, 19-, 19/, 19*, 1^1, 1^2, 1^3, 1^4, 1^5, 1^6, 1^7, 1^8, 1^9, 1^^, 1^+, 1^-, 1^/, 1^*, 1+1, 1+2, 1+3, 1+4, 1+5, 1+6, 1+7, 1+8, 1+9, 1+^, 1++, 1+-, 1+/, 1+*, 1-1, 1-2, 1-3, 1-4, 1-5, 1-6, 1-7, 1-8, 1-9, 1-^, 1-+, 1–, 1-/, 1-*, 1/1, 1/2, 1/3, 1/4, 1/5, 1/6, 1/7, 1/8, 1/9, 1/^, 1/+, 1/-, 1//, 1/*, 1*1, 1*2, 1*3, 1*4, 1*5, 1*6, 1*7, 1*8, 1*9, 1*^, 1*+, 1*-, 1*/, 1**, 211, 212, 213, 214, 215, 216, 217, 218, 219, 21^, 21+, 21-, 21/, 21*, 221, 222, 223, 224, 225, 226, 227, 228, 229, 22^, 22+, 22-, 22/, 22*, 231, 232, 233, 234, 235, 236, 237, 238, 239, 23^, 23+, 23-, 23/, 23*, 241, 242, 243, 244, 245, 246, 247, 248, 249, 24^, 24+, 24-, 24/, 24*, 251, 252, 253, 254, 255, 256, 257, 258, 259, 25^, 25+, 25-, 25/, 25*, 261, 262, 263, 264, 265, 266, 267, 268, 269, 26^, 26+, 26-, 26/, 26*, 271, 272, 273, 274, 275, 276, 277, 278, 279, 27^, 27+, 27-, 27/, 27*, 281, 282, 283, 284, 285, 286, 287, 288, 289, 28^, 28+, 28-, 28/, 28*, 291, 292, 293, 294, 295, 296, 297, 298, 299, 29^, 29+, 29-, 29/, 29*, 2^1, 2^2, 2^3, 2^4, 2^5, 2^6, 2^7, 2^8, 2^9, 2^^, 2^+, 2^-, 2^/, 2^*, 2+1, 2+2, 2+3, 2+4, 2+5, 2+6, 2+7, 2+8, 2+9, 2+^, 2++, 2+-, 2+/, 2+*, 2-1, 2-2, 2-3, 2-4, 2-5, 2-6, 2-7, 2-8, 2-9, 2-^, 2-+, 2–, 2-/, 2-*, 2/1, 2/2, 2/3, 2/4, 2/5, 2/6, 2/7, 2/8, 2/9, 2/^, 2/+, 2/-, 2//, 2/*, 2*1, 2*2, 2*3, 2*4, 2*5, 2*6, 2*7, 2*8, 2*9, 2*^, 2*+, 2*-, 2*/, 2**, 311, 312, 313, 314, 315, 316, 317, 318, 319, 31^, 31+, 31-, 31/, 31*, 321, 322, 323, 324, 325, 326, 327, 328, 329, 32^, 32+, 32-, 32/, 32*, 331, 332, 333, 334, 335, 336, 337, 338, 339, 33^, 33+, 33-, 33/, 33*, 341, 342, 343, 344, 345, 346, 347, 348, 349, 34^, 34+, 34-, 34/, 34*, 351, 352, 353, 354, 355, 356, 357, 358, 359, 35^, 35+, 35-, 35/, 35*, 361, 362, 363, 364, 365, 366, 367, 368, 369, 36^, 36+, 36-, 36/, 36*, 371, 372, 373, 374, 375, 376, 377, 378, 379, 37^, 37+, 37-, 37/, 37*, 381, 382, 383, 384, 385, 386, 387, 388, 389, 38^, 38+, 38-, 38/, 38*, 391, 392, 393, 394, 395, 396, 397, 398, 399, 39^, 39+, 39-, 39/, 39*, 3^1, 3^2, 3^3, 3^4, 3^5, 3^6, 3^7, 3^8, 3^9, 3^^, 3^+, 3^-, 3^/, 3^*, 3+1, 3+2, 3+3, 3+4, 3+5, 3+6, 3+7, 3+8, 3+9, 3+^, 3++, 3+-, 3+/, 3+*, 3-1, 3-2, 3-3, 3-4, 3-5, 3-6, 3-7, 3-8, 3-9, 3-^, 3-+, 3–, 3-/, 3-*, 3/1, 3/2, 3/3, 3/4, 3/5, 3/6, 3/7, 3/8, 3/9, 3/^, 3/+, 3/-, 3//, 3/*, 3*1, 3*2, 3*3, 3*4, 3*5, 3*6, 3*7, 3*8, 3*9, 3*^, 3*+, 3*-, 3*/, 3**, 411, 412, 413, 414, 415, 416, 417, 418, 419, 41^, 41+, 41-, 41/, 41*, 421, 422, 423, 424, 425, 426, 427, 428, 429, 42^, 42+, 42-, 42/, 42*, 431, 432, 433, 434, 435, 436, 437, 438, 439, 43^, 43+, 43-, 43/, 43*, 441, 442, 443, 444, 445, 446, 447, 448, 449, 44^, 44+, 44-, 44/, 44*, 451, 452, 453, 454, 455, 456, 457, 458, 459, 45^, 45+, 45-, 45/, 45*, 461, 462, 463, 464, 465, 466, 467, 468, 469, 46^, 46+, 46-, 46/, 46*, 471, 472, 473, 474, 475, 476, 477, 478, 479, 47^, 47+, 47-, 47/, 47*, 481, 482, 483, 484, 485, 486, 487, 488, 489, 48^, 48+, 48-, 48/, 48*, 491, 492, 493, 494, 495, 496, 497, 498, 499, 49^, 49+, 49-, 49/, 49*, 4^1, 4^2, 4^3, 4^4, 4^5, 4^6, 4^7, 4^8, 4^9, 4^^, 4^+, 4^-, 4^/, 4^*, 4+1, 4+2, 4+3, 4+4, 4+5, 4+6, 4+7, 4+8, 4+9, 4+^, 4++, 4+-, 4+/, 4+*, 4-1, 4-2, 4-3, 4-4, 4-5, 4-6, 4-7, 4-8, 4-9, 4-^, 4-+, 4–, 4-/, 4-*, 4/1, 4/2, 4/3, 4/4, 4/5, 4/6, 4/7, 4/8, 4/9, 4/^, 4/+, 4/-, 4//, 4/*, 4*1, 4*2, 4*3, 4*4, 4*5, 4*6, 4*7, 4*8, 4*9, 4*^, 4*+, 4*-, 4*/, 4**, 511, 512, 513, 514, 515, 516, 517, 518, 519, 51^, 51+, 51-, 51/, 51*, 521, 522, 523, 524, 525, 526, 527, 528, 529, 52^, 52+, 52-, 52/, 52*, 531, 532, 533, 534, 535, 536, 537, 538, 539, 53^, 53+, 53-, 53/, 53*, 541, 542, 543, 544, 545, 546, 547, 548, 549, 54^, 54+, 54-, 54/, 54*, 551, 552, 553, 554, 555, 556, 557, 558, 559, 55^, 55+, 55-, 55/, 55*, 561, 562, 563, 564, 565, 566, 567, 568, 569, 56^, 56+, 56-, 56/, 56*, 571, 572, 573, 574, 575, 576, 577, 578, 579, 57^, 57+, 57-, 57/, 57*, 581, 582, 583, 584, 585, 586, 587, 588, 589, 58^, 58+, 58-, 58/, 58*, 591, 592, 593, 594, 595, 596, 597, 598, 599, 59^, 59+, 59-, 59/, 59*, 5^1, 5^2, 5^3, 5^4, 5^5, 5^6, 5^7, 5^8, 5^9, 5^^, 5^+, 5^-, 5^/, 5^*, 5+1, 5+2, 5+3, 5+4, 5+5, 5+6, 5+7, 5+8, 5+9, 5+^, 5++, 5+-, 5+/, 5+*, 5-1, 5-2, 5-3, 5-4, 5-5, 5-6, 5-7, 5-8, 5-9, 5-^, 5-+, 5–, 5-/, 5-*, 5/1, 5/2, 5/3, 5/4, 5/5, 5/6, 5/7, 5/8, 5/9, 5/^, 5/+, 5/-, 5//, 5/*, 5*1, 5*2, 5*3, 5*4, 5*5, 5*6, 5*7, 5*8, 5*9, 5*^, 5*+, 5*-, 5*/, 5**, 611, 612, 613, 614, 615, 616, 617, 618, 619, 61^, 61+, 61-, 61/, 61*, 621, 622, 623, 624, 625, 626, 627, 628, 629, 62^, 62+, 62-, 62/, 62*, 631, 632, 633, 634, 635, 636, 637, 638, 639, 63^, 63+, 63-, 63/, 63*, 641, 642, 643, 644, 645, 646, 647, 648, 649, 64^, 64+, 64-, 64/, 64*, 651, 652, 653, 654, 655, 656, 657, 658, 659, 65^, 65+, 65-, 65/, 65*, 661, 662, 663, 664, 665, 666, 667, 668, 669, 66^, 66+, 66-, 66/, 66*, 671, 672, 673, 674, 675, 676, 677, 678, 679, 67^, 67+, 67-, 67/, 67*, 681, 682, 683, 684, 685, 686, 687, 688, 689, 68^, 68+, 68-, 68/, 68*, 691, 692, 693, 694, 695, 696, 697, 698, 699, 69^, 69+, 69-, 69/, 69*, 6^1, 6^2, 6^3, 6^4, 6^5, 6^6, 6^7, 6^8, 6^9, 6^^, 6^+, 6^-, 6^/, 6^*, 6+1, 6+2, 6+3, 6+4, 6+5, 6+6, 6+7, 6+8, 6+9, 6+^, 6++, 6+-, 6+/, 6+*, 6-1, 6-2, 6-3, 6-4, 6-5, 6-6, 6-7, 6-8, 6-9, 6-^, 6-+, 6–, 6-/, 6-*, 6/1, 6/2, 6/3, 6/4, 6/5, 6/6, 6/7, 6/8, 6/9, 6/^, 6/+, 6/-, 6//, 6/*, 6*1, 6*2, 6*3, 6*4, 6*5, 6*6, 6*7, 6*8, 6*9, 6*^, 6*+, 6*-, 6*/, 6**, 711, 712, 713, 714, 715, 716, 717, 718, 719, 71^, 71+, 71-, 71/, 71*, 721, 722, 723, 724, 725, 726, 727, 728, 729, 72^, 72+, 72-, 72/, 72*, 731, 732, 733, 734, 735, 736, 737, 738, 739, 73^, 73+, 73-, 73/, 73*, 741, 742, 743, 744, 745, 746, 747, 748, 749, 74^, 74+, 74-, 74/, 74*, 751, 752, 753, 754, 755, 756, 757, 758, 759, 75^, 75+, 75-, 75/, 75*, 761, 762, 763, 764, 765, 766, 767, 768, 769, 76^, 76+, 76-, 76/, 76*, 771, 772, 773, 774, 775, 776, 777, 778, 779, 77^, 77+, 77-, 77/, 77*, 781, 782, 783, 784, 785, 786, 787, 788, 789, 78^, 78+, 78-, 78/, 78*, 791, 792, 793, 794, 795, 796, 797, 798, 799, 79^, 79+, 79-, 79/, 79*, 7^1, 7^2, 7^3, 7^4, 7^5, 7^6, 7^7, 7^8, 7^9, 7^^, 7^+, 7^-, 7^/, 7^*, 7+1, 7+2, 7+3, 7+4, 7+5, 7+6, 7+7, 7+8, 7+9, 7+^, 7++, 7+-, 7+/, 7+*, 7-1, 7-2, 7-3, 7-4, 7-5, 7-6, 7-7, 7-8, 7-9, 7-^, 7-+, 7–, 7-/, 7-*, 7/1, 7/2, 7/3, 7/4, 7/5, 7/6, 7/7, 7/8, 7/9, 7/^, 7/+, 7/-, 7//, 7/*, 7*1, 7*2, 7*3, 7*4, 7*5, 7*6, 7*7, 7*8, 7*9, 7*^, 7*+, 7*-, 7*/, 7**, 811, 812, 813, 814, 815, 816, 817, 818, 819, 81^, 81+, 81-, 81/, 81*, 821, 822, 823, 824, 825, 826, 827, 828, 829, 82^, 82+, 82-, 82/, 82*, 831, 832, 833, 834, 835, 836, 837, 838, 839, 83^, 83+, 83-, 83/, 83*, 841, 842, 843, 844, 845, 846, 847, 848, 849, 84^, 84+, 84-, 84/, 84*, 851, 852, 853, 854, 855, 856, 857, 858, 859, 85^, 85+, 85-, 85/, 85*, 861, 862, 863, 864, 865, 866, 867, 868, 869, 86^, 86+, 86-, 86/, 86*, 871, 872, 873, 874, 875, 876, 877, 878, 879, 87^, 87+, 87-, 87/, 87*, 881, 882, 883, 884, 885, 886, 887, 888, 889, 88^, 88+, 88-, 88/, 88*, 891, 892, 893, 894, 895, 896, 897, 898, 899, 89^, 89+, 89-, 89/, 89*, 8^1, 8^2, 8^3, 8^4, 8^5, 8^6, 8^7, 8^8, 8^9, 8^^, 8^+, 8^-, 8^/, 8^*, 8+1, 8+2, 8+3, 8+4, 8+5, 8+6, 8+7, 8+8, 8+9, 8+^, 8++, 8+-, 8+/, 8+*, 8-1, 8-2, 8-3, 8-4, 8-5, 8-6, 8-7, 8-8, 8-9, 8-^, 8-+, 8–, 8-/, 8-*, 8/1, 8/2, 8/3, 8/4, 8/5, 8/6, 8/7, 8/8, 8/9, 8/^, 8/+, 8/-, 8//, 8/*, 8*1, 8*2, 8*3, 8*4, 8*5, 8*6, 8*7, 8*8, 8*9, 8*^, 8*+, 8*-, 8*/, 8**, 911, 912, 913, 914, 915, 916, 917, 918, 919, 91^, 91+, 91-, 91/, 91*, 921, 922, 923, 924, 925, 926, 927, 928, 929, 92^, 92+, 92-, 92/, 92*, 931, 932, 933, 934, 935, 936, 937, 938, 939, 93^, 93+, 93-, 93/, 93*, 941, 942, 943, 944, 945, 946, 947, 948, 949, 94^, 94+, 94-, 94/, 94*, 951, 952, 953, 954, 955, 956, 957, 958, 959, 95^, 95+, 95-, 95/, 95*, 961, 962, 963, 964, 965, 966, 967, 968, 969, 96^, 96+, 96-, 96/, 96*, 971, 972, 973, 974, 975, 976, 977, 978, 979, 97^, 97+, 97-, 97/, 97*, 981, 982, 983, 984, 985, 986, 987, 988, 989, 98^, 98+, 98-, 98/, 98*, 991, 992, 993, 994, 995, 996, 997, 998, 999, 99^, 99+, 99-, 99/, 99*, 9^1, 9^2, 9^3, 9^4, 9^5, 9^6, 9^7, 9^8, 9^9, 9^^, 9^+, 9^-, 9^/, 9^*, 9+1, 9+2, 9+3, 9+4, 9+5, 9+6, 9+7, 9+8, 9+9, 9+^, 9++, 9+-, 9+/, 9+*, 9-1, 9-2, 9-3, 9-4, 9-5, 9-6, 9-7, 9-8, 9-9, 9-^, 9-+, 9–, 9-/, 9-*, 9/1, 9/2, 9/3, 9/4, 9/5, 9/6, 9/7, 9/8, 9/9, 9/^, 9/+, 9/-, 9//, 9/*, 9*1, 9*2, 9*3, 9*4, 9*5, 9*6, 9*7, 9*8, 9*9, 9*^, 9*+, 9*-, 9*/, 9**, ^11, ^12, ^13, ^14, ^15, ^16, ^17, ^18, ^19, ^1^, ^1+, ^1-, ^1/, ^1*, ^21, ^22, ^23, ^24, ^25, ^26, ^27, ^28, ^29, ^2^, ^2+, ^2-, ^2/, ^2*, ^31, ^32, ^33, ^34, ^35, ^36, ^37, ^38, ^39, ^3^, ^3+, ^3-, ^3/, ^3*, ^41, ^42, ^43, ^44, ^45, ^46, ^47, ^48, ^49, ^4^, ^4+, ^4-, ^4/, ^4*, ^51, ^52, ^53, ^54, ^55, ^56, ^57, ^58, ^59, ^5^, ^5+, ^5-, ^5/, ^5*, ^61, ^62, ^63, ^64, ^65, ^66, ^67, ^68, ^69, ^6^, ^6+, ^6-, ^6/, ^6*, ^71, ^72, ^73, ^74, ^75, ^76, ^77, ^78, ^79, ^7^, ^7+, ^7-, ^7/, ^7*, ^81, ^82, ^83, ^84, ^85, ^86, ^87, ^88, ^89, ^8^, ^8+, ^8-, ^8/, ^8*, ^91, ^92, ^93, ^94, ^95, ^96, ^97, ^98, ^99, ^9^, ^9+, ^9-, ^9/, ^9*, ^^1, ^^2, ^^3, ^^4, ^^5, ^^6, ^^7, ^^8, ^^9, ^^^, ^^+, ^^-, ^^/, ^^*, ^+1, ^+2, ^+3, ^+4, ^+5, ^+6, ^+7, ^+8, ^+9, ^+^, ^++, ^+-, ^+/, ^+*, ^-1, ^-2, ^-3, ^-4, ^-5, ^-6, ^-7, ^-8, ^-9, ^-^, ^-+, ^–, ^-/, ^-*, ^/1, ^/2, ^/3, ^/4, ^/5, ^/6, ^/7, ^/8, ^/9, ^/^, ^/+, ^/-, ^//, ^/*, ^*1, ^*2, ^*3, ^*4, ^*5, ^*6, ^*7, ^*8, ^*9, ^*^, ^*+, ^*-, ^*/, ^**, +11, +12, +13, +14, +15, +16, +17, +18, +19, +1^, +1+, +1-, +1/, +1*, +21, +22, +23, +24, +25, +26, +27, +28, +29, +2^, +2+, +2-, +2/, +2*, +31, +32, +33, +34, +35, +36, +37, +38, +39, +3^, +3+, +3-, +3/, +3*, +41, +42, +43, +44, +45, +46, +47, +48, +49, +4^, +4+, +4-, +4/, +4*, +51, +52, +53, +54, +55, +56, +57, +58, +59, +5^, +5+, +5-, +5/, +5*, +61, +62, +63, +64, +65, +66, +67, +68, +69, +6^, +6+, +6-, +6/, +6*, +71, +72, +73, +74, +75, +76, +77, +78, +79, +7^, +7+, +7-, +7/, +7*, +81, +82, +83, +84, +85, +86, +87, +88, +89, +8^, +8+, +8-, +8/, +8*, +91, +92, +93, +94, +95, +96, +97, +98, +99, +9^, +9+, +9-, +9/, +9*, +^1, +^2, +^3, +^4, +^5, +^6, +^7, +^8, +^9, +^^, +^+, +^-, +^/, +^*, ++1, ++2, ++3, ++4, ++5, ++6, ++7, ++8, ++9, ++^, +++, ++-, ++/, ++*, +-1, +-2, +-3, +-4, +-5, +-6, +-7, +-8, +-9, +-^, +-+, +–, +-/, +-*, +/1, +/2, +/3, +/4, +/5, +/6, +/7, +/8, +/9, +/^, +/+, +/-, +//, +/*, +*1, +*2, +*3, +*4, +*5, +*6, +*7, +*8, +*9, +*^, +*+, +*-, +*/, +**, -11, -12, -13, -14, -15, -16, -17, -18, -19, -1^, -1+, -1-, -1/, -1*, -21, -22, -23, -24, -25, -26, -27, -28, -29, -2^, -2+, -2-, -2/, -2*, -31, -32, -33, -34, -35, -36, -37, -38, -39, -3^, -3+, -3-, -3/, -3*, -41, -42, -43, -44, -45, -46, -47, -48, -49, -4^, -4+, -4-, -4/, -4*, -51, -52, -53, -54, -55, -56, -57, -58, -59, -5^, -5+, -5-, -5/, -5*, -61, -62, -63, -64, -65, -66, -67, -68, -69, -6^, -6+, -6-, -6/, -6*, -71, -72, -73, -74, -75, -76, -77, -78, -79, -7^, -7+, -7-, -7/, -7*, -81, -82, -83, -84, -85, -86, -87, -88, -89, -8^, -8+, -8-, -8/, -8*, -91, -92, -93, -94, -95, -96, -97, -98, -99, -9^, -9+, -9-, -9/, -9*, -^1, -^2, -^3, -^4, -^5, -^6, -^7, -^8, -^9, -^^, -^+, -^-, -^/, -^*, -+1, -+2, -+3, -+4, -+5, -+6, -+7, -+8, -+9, -+^, -++, -+-, -+/, -+*, –1, –2, –3, –4, –5, –6, –7, –8, –9, –^, –+, —, –/, –*, -/1, -/2, -/3, -/4, -/5, -/6, -/7, -/8, -/9, -/^, -/+, -/-, -//, -/*, -*1, -*2, -*3, -*4, -*5, -*6, -*7, -*8, -*9, -*^, -*+, -*-
… ,***
Three thing can be observed by this.
- The number of possibilities increases exponentially with each character (ie 1 character = 14^1 possibilities, 2 characters = 14^2 possibilities….)
- The majority of the possibilities are invalid and must be dismissed. (+8^ or +8* are not valid equations)
- Even if it does calculate an answer there is no way to input any of the values into the math equation. In other words imagine the pattern was 2, 5, 11, __. The formula to find the next number is the (previous number x 2) + 1 —> (11 x 2) + 1 = 23 however our program currently does not have the ability to input any numbers (in this example the 11).
The solution is quite simple. We add a “variable” character to the string bank like below and replace the “variable” character with our input number right before the program calculates the solution.
1 |
const string stringbank = "a123456789^+-/*"; //Variable 'a' |
Now the program theoretically can get a*2+1 as a possible solution.
The general code logic
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
//a*2+1 is just an example of one of the many possible permutations. string trialEquation = "a*2+1"; //Insert function here that return every permutation. // Step 1 - Take the first, second and third number in the pattern and store it as input1, input2 and input3. string input1 = "2"; string input2 = "5"; string input3 = "11"; // Step 3 - Generate three test trials by replacing the 'a' with input1, input2 & input3. string trial1 = trialEquation.Replace("a", input1); //"a*2+1" --> "2*2+1" string trial2 = trialEquation.Replace("a", input2); //"a*2+1" --> "5*2+1" string trial3 = trialEquation.Replace("a", input3); //"a*2+1" --> "11*2+1" //Step 4. Try to calculate trial and if it passes both tests then we assume we found a functioning equation. try { if (*calculate trial1 answer here* == input2) if (*calculate trial2 answer here* == input3) { Console.WriteLine("Success! Solution found! {0}", trialEquation); Console.WriteLine("---------------------------"); } } catch { // ignored } |
Step 2: Coding the code
A function that returns all permutations of the stringbank
Here is the finished code.
Download finished project here.
Download compiled exe here.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 |
/* This brute force algorithm was originally written (by me) back in 1998, and has been collecting dust since then. However, for the purpose of testing Gist on GitHub I decided to rewrite the algorithm from VB6 to C#, make some improvements and release this fast, compact, non-recursive, brute force algorithm under the MIT license: http://opensource.org/licenses/MIT Notes: - Do a run with testLetters = "0123456789" and testLength = 3, to see what happens - Remember to keep the callback testCalback as fast as possible - Tweet some love to @fredrikdev :) Have fun! Fredrik https://github.com/johanssonrobotics */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Data;//import this namespace using MathParserTK; using System.Globalization; using System.Threading; using System.Diagnostics; using System.Threading.Tasks; namespace FJR { public static class Algorithm { // callback that is called for every combination to test public delegate bool BruteForceTest(ref char[] test, int testIndex, int testMax); // fast, compact, non-recursive, brute force algorithm public static bool BruteForce(string testLetters, int testLength, BruteForceTest testCallback) { // get the number of combinations we need to try (just for statistic purposes) int testIndex = 0, testMax = (int)Math.Pow(testLetters.Length, testLength); // initialize & perform first test var test = new char[testLength]; for (int x = 0; x < testLength; x++) test[x] = testLetters[0]; if (testCallback(ref test, ++testIndex, testMax)) return true; // start rotating chars from the back and forward for (int ti = testLength - 1, li = 0; ti > -1; ti--) { for (li = testLetters.IndexOf(test[ti]) + 1; li < testLetters.Length; li++) { // test test[ti] = testLetters[li]; if (testCallback(ref test, ++testIndex, testMax)) return true; // rotate to the right for (int z = ti + 1; z < testLength; z++) if (test[z] != testLetters[testLetters.Length - 1]) { ti = testLength; goto outerBreak; } } outerBreak: if (li == testLetters.Length) test[ti] = testLetters[0]; } // no match return false; } public static void Main(string[] args) { int i2 = 1; Console.WriteLine("Please enter var A:"); string varA = Console.ReadLine(); Console.WriteLine("Please enter var B:"); string varB = Console.ReadLine(); Console.WriteLine("Please enter var C "); string varC = Console.ReadLine(); //Console.WriteLine("Please enter var D "); //string varD = Console.ReadLine(); MathParser parser = new MathParser(); // example usage Console.WriteLine("Searching for combination..."); Stopwatch stopWatch = new Stopwatch(); stopWatch.Start(); // our 'test' routine that gets called for every combination to test BruteForceTest testCallback = delegate(ref char[] test, int testIndex, int testMax) { var str = new string(test); // Console.Title = Math.Round(100 * (testIndex / (double)testMax), 0) + "%" + " - " + testIndex + " - " + str; i2++; try { string newstr = str.Replace("a", varA); double answer = parser.Parse(newstr); //Console.WriteLine(answer); if (answer.ToString() == varB) { //Console.WriteLine("{0} = {1}", str, answer); string checkstr = str.Replace("a", varB); double check = parser.Parse(checkstr); if (check.ToString() == varC) { Console.WriteLine("--------------------------------------------------"); Console.WriteLine("Success! Solution found! ({0})=b", str); string nextstr = str.Replace("a", varC); double next = parser.Parse(nextstr); Console.WriteLine("next number is({0})", next); stopWatch.Stop(); TimeSpan ts = stopWatch.Elapsed; // Format and display the TimeSpan value. string elapsedTime = String.Format("{0:00}:{1:00}:{2:00}.{3:00}", ts.Hours, ts.Minutes, ts.Seconds, ts.Milliseconds / 10); Console.WriteLine("RunTime " + elapsedTime); Console.WriteLine("testIndex " + testIndex); stopWatch.Start(); } } } catch{} return(1==2); // Returns a false }; // test all combinations of the letters "abc" with the result length 3 // todo: you may want to add a for-loop here to test e.g. length 1 to 8 const string stringbank = "a123456789^+-/*"; for (int i = 0; i < 100; i++) { BruteForce(stringbank, i, testCallback); } Console.ReadLine(); } } } namespace MathParserTK { #region License agreement // This is light, fast and simple to understand mathematical parser // designed in one class, which receives as input a mathematical // expression (System.String) and returns the output value (System.Double). // For example, if you input string is "√(625)+25*(3/3)" then parser return double value — 50. // Copyright (C) 2012-2013 Yerzhan Kalzhani, kirnbas@gmail.com // This program is free software: you can redistribute it and/or modify // it under the terms of the GNU General Public License as published by // the Free Software Foundation, either version 3 of the License, or // (at your option) any later version. // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program. If not, see <http://www.gnu.org/licenses/> #endregion #region Example usage // public static void Main() // { // MathParser parser = new MathParser(); // string s1 = "pi+5*5+5*3-5*5-5*3+1E1"; // string s2 = "sin(cos(tg(sh(ch(th(100))))))"; // bool isRadians = false; // double d1 = parser.Parse(s1, isRadians); // double d2 = parser.Parse(s2, isRadians); // // Console.WriteLine(d1); // 13.141592... // Console.WriteLine(d2); // 0.0174524023974442 // Console.ReadKey(true); // } #endregion #region Common tips to extend this math parser // If you want to add new operator, then add new item to supportedOperators // dictionary and check next methods: // (1) IsRightAssociated, (2) GetPriority, (3) SyntaxAnalysisRPN, (4) NumberOfArguments // // If you want to add new function, then add new item to supportedFunctions // dictionary and check next above methods: (2), (3), (4). // // if you want to add new constant, then add new item to supportedConstants #endregion public class MathParser { #region Fields #region Markers (each marker should have length equals to 1) private const string NumberMaker = "#"; private const string OperatorMarker = "$"; private const string FunctionMarker = "@"; #endregion #region Internal tokens private const string Plus = OperatorMarker + "+"; private const string UnPlus = OperatorMarker + "un+"; private const string Minus = OperatorMarker + "-"; private const string UnMinus = OperatorMarker + "un-"; private const string Multiply = OperatorMarker + "*"; private const string Divide = OperatorMarker + "/"; private const string Degree = OperatorMarker + "^"; private const string LeftParent = OperatorMarker + "("; private const string RightParent = OperatorMarker + ")"; private const string Sqrt = FunctionMarker + "sqrt"; private const string Sin = FunctionMarker + "sin"; private const string Cos = FunctionMarker + "cos"; private const string Tg = FunctionMarker + "tg"; private const string Ctg = FunctionMarker + "ctg"; private const string Sh = FunctionMarker + "sh"; private const string Ch = FunctionMarker + "ch"; private const string Th = FunctionMarker + "th"; private const string Log = FunctionMarker + "log"; private const string Ln = FunctionMarker + "ln"; private const string Exp = FunctionMarker + "exp"; private const string Abs = FunctionMarker + "abs"; #endregion #region Dictionaries (containts supported input tokens, exclude number) // Key -> supported input token, Value -> internal token or number /// /// Contains supported operators /// private readonly Dictionary<string, string> supportedOperators = new Dictionary<string, string> { { "+", Plus }, { "-", Minus }, { "*", Multiply }, { "/", Divide }, { "^", Degree }, { "(", LeftParent }, { ")", RightParent } }; /// /// Contains supported functions /// private readonly Dictionary<string, string> supportedFunctions = new Dictionary<string, string> { { "sqrt", Sqrt }, { "√", Sqrt }, { "sin", Sin }, { "cos", Cos }, { "tg", Tg }, { "ctg", Ctg }, { "sh", Sh }, { "ch", Ch }, { "th", Th }, { "log", Log }, { "exp", Exp }, { "abs", Abs } }; private readonly Dictionary<string, string> supportedConstants = new Dictionary<string, string> { {"pi", NumberMaker + Math.PI.ToString() }, {"e", NumberMaker + Math.E.ToString() } }; #endregion #endregion private readonly char decimalSeparator; private bool isRadians; #region Constructors /// /// Initialize new instance of MathParser /// (symbol of decimal separator is read /// from regional settings in system) /// public MathParser() { try { decimalSeparator = Char.Parse(System.Globalization.CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator); } catch (FormatException ex) { throw new FormatException("Error: can't read char decimal separator from system, check your regional settings.", ex); } } /// /// Initialize new instance of MathParser /// ///Set decimal separator public MathParser(char decimalSeparator) { this.decimalSeparator = decimalSeparator; } #endregion /// /// Produce result of the given math expression /// ///Math expression (infix/standard notation) /// Result public double Parse(string expression, bool isRadians = true) { this.isRadians = isRadians; try { return Calculate(ConvertToRPN(FormatString(expression))); } catch (DivideByZeroException e) { throw e; } catch (FormatException e) { throw e; } catch (InvalidOperationException e) { throw e; } catch (ArgumentOutOfRangeException e) { throw e; } catch (ArgumentException e) { throw e; } catch (Exception e) { throw e; } } /// /// Produce formatted string by the given string /// ///Unformatted math expression /// Formatted math expression private string FormatString(string expression) { if (string.IsNullOrEmpty(expression)) { throw new ArgumentNullException("Expression is null or empty"); } StringBuilder formattedString = new StringBuilder(); int balanceOfParenth = 0; // Check number of parenthesis // Format string in one iteration and check number of parenthesis // (this function do 2 tasks because performance priority) for (int i = 0; i < expression.Length; i++) { char ch = expression[i]; if (ch == '(') { balanceOfParenth++; } else if (ch == ')') { balanceOfParenth--; } if (Char.IsWhiteSpace(ch)) { continue; } else if (Char.IsUpper(ch)) { formattedString.Append(Char.ToLower(ch)); } else { formattedString.Append(ch); } } if (balanceOfParenth != 0) { throw new FormatException("Number of left and right parenthesis is not equal"); } return formattedString.ToString(); } #region Convert to Reverse-Polish Notation /// /// Produce math expression in reverse polish notation /// by the given string /// ///Math expression in infix notation /// Math expression in postfix notation (RPN) private string ConvertToRPN(string expression) { int pos = 0; // Current position of lexical analysis StringBuilder outputString = new StringBuilder(); Stack stack = new Stack(); // While there is unhandled char in expression while (pos < expression.Length) { string token = LexicalAnalysisInfixNotation(expression, ref pos); outputString = SyntaxAnalysisInfixNotation(token, outputString, stack); } // Pop all elements from stack to output string while (stack.Count > 0) { // There should be only operators if (stack.Peek()[0] == OperatorMarker[0]) { outputString.Append(stack.Pop()); } else { throw new FormatException("Format exception," + " there is function without parenthesis"); } } return outputString.ToString(); } /// /// Produce token by the given math expression /// ///Math expression in infix notation ///Current position in string for lexical analysis /// Token private string LexicalAnalysisInfixNotation(string expression, ref int pos) { // Receive first char StringBuilder token = new StringBuilder(); token.Append(expression[pos]); // If it is a operator if (supportedOperators.ContainsKey(token.ToString())) { // Determine it is unary or binary operator bool isUnary = pos == 0 || expression[pos - 1] == '('; pos++; switch (token.ToString()) { case "+": return isUnary ? UnPlus : Plus; case "-": return isUnary ? UnMinus : Minus; default: return supportedOperators[token.ToString()]; } } else if (Char.IsLetter(token[0]) || supportedFunctions.ContainsKey(token.ToString()) || supportedConstants.ContainsKey(token.ToString())) { // Read function or constant name while (++pos < expression.Length && Char.IsLetter(expression[pos])) { token.Append(expression[pos]); } if (supportedFunctions.ContainsKey(token.ToString())) { return supportedFunctions[token.ToString()]; } else if (supportedConstants.ContainsKey(token.ToString())) { return supportedConstants[token.ToString()]; } else { throw new ArgumentException("Unknown token"); } } else if (Char.IsDigit(token[0]) || token[0] == decimalSeparator) { // Read number // Read the whole part of number if (Char.IsDigit(token[0])) { while (++pos < expression.Length && Char.IsDigit(expression[pos])) { token.Append(expression[pos]); } } else { // Because system decimal separator // will be added below token.Clear(); } // Read the fractional part of number if (pos < expression.Length && expression[pos] == decimalSeparator) { // Add current system specific decimal separator token.Append(CultureInfo.CurrentCulture .NumberFormat.NumberDecimalSeparator); while (++pos < expression.Length && Char.IsDigit(expression[pos])) { token.Append(expression[pos]); } } // Read scientific notation (suffix) if (pos + 1 < expression.Length && expression[pos] == 'e' && (Char.IsDigit(expression[pos + 1]) || (pos + 2 < expression.Length && (expression[pos + 1] == '+' || expression[pos + 1] == '-') && Char.IsDigit(expression[pos + 2])))) { token.Append(expression[pos++]); // e if (expression[pos] == '+' || expression[pos] == '-') token.Append(expression[pos++]); // sign while (pos < expression.Length && Char.IsDigit(expression[pos])) { token.Append(expression[pos++]); // power } // Convert number from scientific notation to decimal notation return NumberMaker + Convert.ToDouble(token.ToString()); } return NumberMaker + token.ToString(); } else { throw new ArgumentException("Unknown token in expression"); } } /// /// Syntax analysis of infix notation /// ///Token ///Output string (math expression in RPN) ///Stack which contains operators (or functions) /// Output string (math expression in RPN) private StringBuilder SyntaxAnalysisInfixNotation(string token, StringBuilder outputString, Stack stack) { // If it's a number just put to string if (token[0] == NumberMaker[0]) { outputString.Append(token); } else if (token[0] == FunctionMarker[0]) { // if it's a function push to stack stack.Push(token); } else if (token == LeftParent) { // If its '(' push to stack stack.Push(token); } else if (token == RightParent) { // If its ')' pop elements from stack to output string // until find the ')' string elem; while ((elem = stack.Pop()) != LeftParent) { outputString.Append(elem); } // if after this a function is in the peek of stack then put it to string if (stack.Count > 0 && stack.Peek()[0] == FunctionMarker[0]) { outputString.Append(stack.Pop()); } } else { // While priority of elements at peek of stack >= (>) token's priority // put these elements to output string while (stack.Count > 0 && Priority(token, stack.Peek())) { outputString.Append(stack.Pop()); } stack.Push(token); } return outputString; } /// /// Is priority of token less (or equal) to priority of p /// private bool Priority(string token, string p) { return IsRightAssociated(token) ? GetPriority(token) < GetPriority(p) : GetPriority(token) <= GetPriority(p); } /// /// Is right associated operator /// private bool IsRightAssociated(string token) { return token == Degree; } /// /// Get priority of operator /// private int GetPriority(string token) { switch (token) { case LeftParent: return 0; case Plus: case Minus: return 2; case UnPlus: case UnMinus: return 6; case Multiply: case Divide: return 4; case Degree: case Sqrt: return 8; case Sin: case Cos: case Tg: case Ctg: case Sh: case Ch: case Th: case Log: case Ln: case Exp: case Abs: return 10; default: throw new ArgumentException("Unknown operator"); } } #endregion #region Calculate expression in RPN /// /// Calculate expression in reverse-polish notation /// ///Math expression in reverse-polish notation /// Result private double Calculate(string expression) { int pos = 0; // Current position of lexical analysis var stack = new Stack(); // Contains operands // Analyse entire expression while (pos < expression.Length) { string token = LexicalAnalysisRPN(expression, ref pos); stack = SyntaxAnalysisRPN(stack, token); } // At end of analysis in stack should be only one operand (result) if (stack.Count > 1) { throw new ArgumentException("Excess operand"); } return stack.Pop(); } /// /// Produce token by the given math expression /// ///Math expression in reverse-polish notation ///Current position of lexical analysis /// Token private string LexicalAnalysisRPN(string expression, ref int pos) { StringBuilder token = new StringBuilder(); // Read token from marker to next marker token.Append(expression[pos++]); while (pos < expression.Length && expression[pos] != NumberMaker[0] && expression[pos] != OperatorMarker[0] && expression[pos] != FunctionMarker[0]) { token.Append(expression[pos++]); } return token.ToString(); } /// /// Syntax analysis of reverse-polish notation /// ///Stack which contains operands ///Token /// Stack which contains operands private Stack SyntaxAnalysisRPN(Stack stack, string token) { // if it's operand then just push it to stack if (token[0] == NumberMaker[0]) { stack.Push(double.Parse(token.Remove(0, 1))); } // Otherwise apply operator or function to elements in stack else if (NumberOfArguments(token) == 1) { double arg = stack.Pop(); double rst; switch (token) { case UnPlus: rst = arg; break; case UnMinus: rst = -arg; break; case Sqrt: rst = Math.Sqrt(arg); break; case Sin: rst = ApplyTrigFunction(Math.Sin, arg); break; case Cos: rst = ApplyTrigFunction(Math.Cos, arg); break; case Tg: rst = ApplyTrigFunction(Math.Tan, arg); break; case Ctg: rst = 1 / ApplyTrigFunction(Math.Tan, arg); break; case Sh: rst = Math.Sinh(arg); break; case Ch: rst = rst = Math.Cosh(arg); break; case Th: rst = Math.Tanh(arg); break; case Ln: rst = Math.Log(arg); break; case Exp: rst = Math.Exp(arg); break; case Abs: rst = Math.Abs(arg); break; default: throw new ArgumentException("Unknown operator"); } stack.Push(rst); } else { // otherwise operator's number of arguments equals to 2 double arg2 = stack.Pop(); double arg1 = stack.Pop(); double rst; switch (token) { case Plus: rst = arg1 + arg2; break; case Minus: rst = arg1 - arg2; break; case Multiply: rst = arg1 * arg2; break; case Divide: if (arg2 == 0) { throw new DivideByZeroException("Second argument is zero"); } rst = arg1 / arg2; break; case Degree: rst = Math.Pow(arg1, arg2); break; case Log: rst = Math.Log(arg2, arg1); break; default: throw new ArgumentException("Unknown operator"); } stack.Push(rst); } return stack; } /// /// Apply trigonometric function /// ///Trigonometric function ///Argument /// Result of function private double ApplyTrigFunction(Func<double, double> func, double arg) { if (!isRadians) { arg = arg * Math.PI / 180; // Convert value to degree } return func(arg); } /// /// Produce number of arguments for the given operator /// private int NumberOfArguments(string token) { switch (token) { case UnPlus: case UnMinus: case Sqrt: case Tg: case Sh: case Ch: case Th: case Ln: case Ctg: case Sin: case Cos: case Exp: case Abs: return 1; case Plus: case Minus: case Multiply: case Divide: case Degree: case Log: return 2; default: throw new ArgumentException("Unknown operator"); } } #endregion } } |
I think it would be amazing if you could go into detail
Hello! Cool post, amazing!!!
Very neat article post.Much thanks again. Much obliged. eedeecbfecde