• API
• FAQ
• Tools
• Archive
SHARE
TWEET ProjectEuler a guest Jan 20th, 2019 65 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
1. Project Euler Sample Answers
2.
3. https://projecteuler.net/archives
4.
5. 1 Multiples of 3 and 5
6.
7. public class Main {
8.     public static void main(String[] args) {
9.         int sum = 0;
10.         for(int i = 0; i < 1000; i++) {
11.             if( (i%3 == 0) || (i%5 == 0) ) {
12.                 sum+=i;
13.             }
14.         }
15.         System.out.println("Sum is " + sum);
16.     }
17. }
18.
19. 2 Even Fibonacci Numbers
20.
21. public class Main {
22.     public static void main(String[] args) {
23.         int sum = 0, fib = 1, prev = 1, temp = 0;
24.         while(fib < 4000000) {
25.             temp = prev;
26.             prev = fib;
27.             fib = fib + temp;
28.             if(fib%2 == 0) {
29.                 sum+=fib;
30.             }
31.         }
32.         System.out.println("Sum is " + sum);
33.     }
34. }
35.
36. 3 Largest Prime Factor
37.
38. public class Main {
39.     public static void main(String[] args) {
40.         long prime = 1;
41.         long num = 600851475143L;
42.         long i = 2;
43.         while(i < num) {
44.             while(num%i == 0) {
45.                 System.out.println("num: " + num + ", i: " + i);
46.                 num =  num/i;
47.             }
48.             i++;
49.         }
50.         prime = num;
51.         System.out.println("Factor is " + prime);
52.     }
53. }
54.
55. 4 Largest palindrome product
56.
57. import java.util.*;
58.
59. public class Main {
60.     public static void main(String[] args) {
61.         ArrayList<Integer> Palindromes = new ArrayList<>();
62.         for(int i = 999; i > 100; i--) {
63.             for(int j = 999; j > 100; j--) {
64.                 if(isPalindrome(i*j)) {
66.                 }
67.             }
68.         }
69.         System.out.println("Largest palindrome is: " + Collections.max(Palindromes));
70.     }
71.
72.     public static boolean isPalindrome(int n) {
73.         int temp = n, rem, rev = 0;
74.         while (temp != 0) {
75.             rem = temp % 10;
76.             rev = rev * 10 + rem;
77.             temp = temp / 10;
78.         }
79.         return n == rev;
80.     }
81. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy.

Top