via Project Euler My Solution: Code: [SPOILER] int result = 0; for (int i = 0; i < 1000; i++) { if (i%3 == 0 || i%5 == 0) { Console.Write(i + ", "); result += i; } } Console.WriteLine(""); Console.WriteLine(result); Console.ReadLine();[/SPOILER] Multiples of 3 or 5 below 1000: Code: [spoiler]3, 5, 6, 9, 10, 12, 15, 18, 20, 21, 24, 25, 27, 30, 33, 35, 36, 39, 40, 42, 4 5, 48, 50, 51, 54, 55, 57, 60, 63, 65, 66, 69, 70, 72, 75, 78, 80, 81, 84, 85, 8 7, 90, 93, 95, 96, 99, 100, 102, 105, 108, 110, 111, 114, 115, 117, 120, 123, 12 5, 126, 129, 130, 132, 135, 138, 140, 141, 144, 145, 147, 150, 153, 155, 156, 15 9, 160, 162, 165, 168, 170, 171, 174, 175, 177, 180, 183, 185, 186, 189, 190, 19 2, 195, 198, 200, 201, 204, 205, 207, 210, 213, 215, 216, 219, 220, 222, 225, 22 8, 230, 231, 234, 235, 237, 240, 243, 245, 246, 249, 250, 252, 255, 258, 260, 26 1, 264, 265, 267, 270, 273, 275, 276, 279, 280, 282, 285, 288, 290, 291, 294, 29 5, 297, 300, 303, 305, 306, 309, 310, 312, 315, 318, 320, 321, 324, 325, 327, 33 0, 333, 335, 336, 339, 340, 342, 345, 348, 350, 351, 354, 355, 357, 360, 363, 36 5, 366, 369, 370, 372, 375, 378, 380, 381, 384, 385, 387, 390, 393, 395, 396, 39 9, 400, 402, 405, 408, 410, 411, 414, 415, 417, 420, 423, 425, 426, 429, 430, 43 2, 435, 438, 440, 441, 444, 445, 447, 450, 453, 455, 456, 459, 460, 462, 465, 46 8, 470, 471, 474, 475, 477, 480, 483, 485, 486, 489, 490, 492, 495, 498, 500, 50 1, 504, 505, 507, 510, 513, 515, 516, 519, 520, 522, 525, 528, 530, 531, 534, 53 5, 537, 540, 543, 545, 546, 549, 550, 552, 555, 558, 560, 561, 564, 565, 567, 57 0, 573, 575, 576, 579, 580, 582, 585, 588, 590, 591, 594, 595, 597, 600, 603, 60 5, 606, 609, 610, 612, 615, 618, 620, 621, 624, 625, 627, 630, 633, 635, 636, 63 9, 640, 642, 645, 648, 650, 651, 654, 655, 657, 660, 663, 665, 666, 669, 670, 67 2, 675, 678, 680, 681, 684, 685, 687, 690, 693, 695, 696, 699, 700, 702, 705, 70 8, 710, 711, 714, 715, 717, 720, 723, 725, 726, 729, 730, 732, 735, 738, 740, 74 1, 744, 745, 747, 750, 753, 755, 756, 759, 760, 762, 765, 768, 770, 771, 774, 77 5, 777, 780, 783, 785, 786, 789, 790, 792, 795, 798, 800, 801, 804, 805, 807, 81 0, 813, 815, 816, 819, 820, 822, 825, 828, 830, 831, 834, 835, 837, 840, 843, 84 5, 846, 849, 850, 852, 855, 858, 860, 861, 864, 865, 867, 870, 873, 875, 876, 87 9, 880, 882, 885, 888, 890, 891, 894, 895, 897, 900, 903, 905, 906, 909, 910, 91 2, 915, 918, 920, 921, 924, 925, 927, 930, 933, 935, 936, 939, 940, 942, 945, 94 8, 950, 951, 954, 955, 957, 960, 963, 965, 966, 969, 970, 972, 975, 978, 980, 98 1, 984, 985, 987, 990, 993, 995, 996, 999[/spoiler]
My Solution: Code: [spoiler] long current = 2; long previous = 1; long hold = 0; long result = 2; while (current <= 4000000) { hold = current; current = previous + current; previous = hold; if (current%2 == 0) { Console.Write(current + ", "); result += current; } } Console.WriteLine(""); Console.WriteLine(result); Console.ReadLine();[/spoiler] Even values in the Fibonacci sequence below 4 million: Code: [spoiler]8, 34, 144, 610, 2584, 10946, 46368, 196418, 832040, 3524578[/spoiler]
The answer is Spoiler 233168 calculated in 0.010s In perl: Code: sub multi_under { my @pass = @_; my $sum = 0; for (my $counter = 1; $counter * $pass[0] < $pass[1]; $counter++) { $sum += $pass[0] * $counter if $pass[0] * $counter % $pass[2] != 0; } return $sum; } my $total = multi_under(3, 1000, 5); $total += multi_under(5, 1000, 1000); print "$total\n"; While Arimil's would run faster with one pass I wrote mine out of habit by making the code fixable and reusable however I don't know why I ever would though.
Answer: Spoiler 4613732 calculated in 0.009s In Perl: Code: my $high = 1; my $low = 1; my $count = 0; my $sum = 2; while($sum < 4000000) { $count += $sum if $sum % 2 == 0; $low = $high; $high = $sum; $sum = $high + $low; } print "$count\n";
Alright lets try #3: My solution in Perl took 0.012s Code: [SPOILER]sub factors { my @pass = @_; my @factors, $pass[1]++ while($pass[0] % $pass[1] != 0); push(@factors,$pass[1]); push(@factors, factors($pass[0]/$pass[1], $pass[1])) if $pass[0] > $pass[1]; return @factors; } my @prime = factors(600851475143, 2); print pop(@prime) . "\n";[/SPOILER] I love recursion ^_^
You destroyed me on this one, mines been running for 2 days and still hasn't found it. ' EDIT: And... I just realized I don't know what a prime factor is and was trying to do it wrong. My solution: Code: [spoiler] public static bool isprime(long num) { for (int i = 2; i < num; i++) { if (num%i == 0) { return false; } } return true; } static void Main(string[] args) { long val = 600851475143; List<long> factors = new List<long>(); for (int i = 2; i < val; i++) { if (val%i == 0) { factors.Add(i); val = val / i; i = 1; } } factors.Add(val); long result = 0; foreach (int factor in factors) { if (factor > result && isprime(factor)) { result = factor; } } Console.WriteLine(result); Console.ReadLine(); }[/spoiler]
#4 My Solution: Code: [spoiler] public static bool ispalindrome(string num) { char[] numbers = num.ToCharArray(); for (int i = 1; i <= num.Length / 2; i++) { if (numbers[i - 1] != numbers[num.Length - i]) { return false; } } return true; } static void Main(string[] args) { int i2 = 100; int val = 0; int solution = 0; for (int i = 100; i < 1000; i++) { val = i * i2; if (val > solution && ispalindrome(val.ToString())) { solution = val ; } if (i == 999 && i2 < 999) { i = 100; i2++; } } Console.WriteLine(solution); Console.ReadLine(); }[/spoiler] It asks you which language you use to solve the problems, there is an option for pencil and paper.
Consider yourself lucky, my school was technically retarded. Best tech class we had was HTML and they taught it all wrong.
Looks a lot like discreet structures which I'm taking this semester. My teach focused more on the mathmatics and algorithims instead of using that math to make programs.
I realized there was a site the second time around looking at the link lol. But the first paragraph is this. No pencil is going to solve these problems in under a minute heh.
906609 by multiplying 993x913 Paper, pencil, calculator...figured for the highest palindrome, the last digit has to be a 9, since the first digit will most likely be a 9. Only three single digit integer products end in a 9 (1x9 or 3x3 or 7x7), so I decided to check the 3x3 possibilities first. Fun fact: all (at least most) 6 digit palindromes are divisible by 11. So at least one of the numbers has to be divisible by 11. The only number >900 that is divisible by 11 and ends in a 3 is 913. Then I started multiplying by numbers in the 900s ending in 3 so the last digit of the palindrome would be 9. Luckily the first one I tried (and the highest possible one), 993, created the product 906609, a palindrome. Logic + guess and check ftw