Projecteuler Problem 1-10

Problem1
原题:

If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.

Find the sum of all the multiples of 3 or 5 below 1000。

分析:

求出1000(不包括1000)以下3或5倍数的和。

可以直接循环求,也可以用抽屉原理及等差数列求和公式来。

代码: 
  1.     public static int problem1(int max) {
  2.         int sum3 = sumOfArray(3, (max - 1) / 33);
  3.         int sum5 = sumOfArray(5, (max - 1) / 55);
  4.         int sum15 = sumOfArray(15, (max - 1) / 1515);
  5.         return sum3 + sum5 - sum15;
  6.     }
  7.     /**
  8.      * Calculate the sum of a arithmetic progression.
  9.      * 
  10.      * @param a1 : the first term
  11.      * @param n: the length of array
  12.      * @param d:common difference
  13.      */
  14.     private static int sumOfArray(int a1, int n, int d) {
  15.         return n * a1 + n * (n - 1) * d / 2;
  16.     }
Problem2
原题:

Each new term in the Fibonacci sequence is generated by adding the previous two terms. By starting with 1 and 2, the first 10 terms will be:

1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...

Find the sum of all the even-valued terms in the sequence which do not exceed four million.

分析:

求出Fibonacci数列中,小于4000000且为偶数的和

慢慢来呗!

代码: 
  1.     public static int problem2(int max) {
  2.         int pre = 1;
  3.         int next = 2;
  4.         int sum = 0;
  5.         while (next <= max) {
  6.             if (next % 2 == 0) {
  7.                 sum += next;
  8.             }
  9.             System.out.println(pre + "/t" + next + "/t" + sum);
  10.             int temp = pre;
  11.             pre = next;
  12.             next += temp;
  13.         }
  14.         return sum;
  15.     }
Problem3
原题:

The prime factors of 13195 are 5, 7, 13 and 29.

What is the largest prime factor of the number 600851475143 ?

分析:

就是求600851475143最大质因数。

代码: 
  1.     public static long problem3(long num) {
  2.         while (true) {
  3.             long temp = 0;
  4.             for (long i = 2; i <= Math.sqrt(num); i++) {
  5.                 if (num % i == 0) {
  6.                     temp = (num / i);
  7.                     break;
  8.                 }
  9.             }
  10.             if (temp == 0) {
  11.                 break;
  12.             }
  13.             num = temp;
  14.         }
  15.         return num;
  16.     }
Problem4
原题:

A palindromic number reads the same both ways. The largest palindrome made from the product of two 2-digit numbers is 9009 = 91 99.

Find the largest palindrome made from the product of two 3-digit numbers

分析:

palindromic number:回文数

题目求的是两个三位数积中最大的回文数,邪恶的暴力。

代码: 
  1.     public static int problem4() {
  2.         int result = -1;
  3.         for (int i = 999; i >= 100; i--) {
  4.             for (int j = i; j >= 100; j--) {
  5.                 if (isPalindromic(i * j)) {
  6.                     if (i * j > result) {
  7.                         result = i * j;
  8.                     }
  9.                 }
  10.             }
  11.         }
  12.         return result;
  13.     }
  14.     private static boolean isPalindromic(int num) {
  15.         char[] cs = String.valueOf(num).toCharArray();
  16.         for (int i = 0; i < cs.length / 2; i++) {
  17.             if (cs[i] != cs[cs.length - i - 1]) {
  18.                 return false;
  19.             }
  20.         }
  21.         return true;
  22.     }
Problem5
原题:

2520 is the smallest number that can be divided by each of the numbers from 1 to 10 without any remainder.

What is the smallest number that is evenly divisible by all of the numbers from 1 to 20?

分析:

其实题目求的就是1~20的最小公倍数。

由于数字比较小,且这个数必须是1~20中所有质数的公倍数,所以暴力吧!

代码: 
  1.     public static int problem5() {
  2.         int begin = 1 * 3 * 5 * 7 * 11 * 13 * 17 * 19;
  3.         for (int i = begin;; i += begin) {
  4.             boolean check = true;
  5.             for (int j = 1; j <= 20; j++) {
  6.                 if (i % j != 0) {
  7.                     check = false;
  8.                 }
  9.             }
  10.             if (check) {
  11.                 System.out.println(i / begin);
  12.                 return i;
  13.             }
  14.         }
  15.     }
Problem6
原题:

The sum of the squares of the first ten natural numbers is,
1^2 + 2^2 + ... + 10^2 = 385
The square of the sum of the first ten natural numbers is,
(1 + 2 + ... + 10)^2 = 55^2 = 3025

Hence the difference between the sum of the squares of the first ten natural numbers and the square of the sum is 3025 - 385 = 2640.

Find the difference between the sum of the squares of the first one hundred natural numbers and the square of the sum.

分析:

求的是:1到100的平方和减去1到100和的平方的差

代码: 
  1.     public static int problem6() {
  2.         int result0 = 0;
  3.         int result1 = 0;
  4.         for (int i = 1; i <= 100; i++) {
  5.             result0 += (i * i);
  6.             result1 += i;
  7.         }
  8.         return result0 - result1 * result1;
  9.     }
Problem7
原题:

By listing the first six prime numbers: 2, 3, 5, 7, 11, and 13, we can see that the 6th prime is 13.

What is the 10001st prime number?

分析:

求第10001个质数

代码: 
  1.     public static int p7() {
  2.         int length = 10001;
  3.         List<Integer> array = new ArrayList<Integer>(length);
  4.         array.add(2);
  5.         int temp = 3;
  6.         while (array.size() < length) {
  7.             boolean isPrime = true;
  8.             for (Integer i : array) {
  9.                 if (temp % i == 0) {
  10.                     isPrime = false;
  11.                 }
  12.             }
  13.             if (isPrime) {
  14.                 array.add(temp);
  15.             }
  16.             temp++;
  17.         }
  18.         return array.get(length - 1);
  19.     }
Problem8
原题:

Find the greatest product of five consecutive digits in the 1000-digit number.

73167176531330624919225119674426574742355349194934
96983520312774506326239578318016984801869478851843
85861560789112949495459501737958331952853208805511
12540698747158523863050715693290963295227443043557
66896648950445244523161731856403098711121722383113
62229893423380308135336276614282806444486645238749
30358907296290491560440772390713810515859307960866
70172427121883998797908792274921901699720888093776
65727333001053367881220235421809751254540594752243
52584907711670556013604839586446706324415722155397
53697817977846174064955149290862569321978468622482
83972241375657056057490261407972968652414535100474
82166370484403199890008895243450658541227588666881
16427171479924442928230863465674813919123162824586
17866458359124566529476545682848912883142607690042
24219022671055626321111109370544217506941658960408
07198403850962455444362981230987879927244284909188
84580156166097919133875499200524063689912560717606
05886116467109405077541002256983155200055935729725
71636269561882670428252483600823257530420752963450

分析:

在上面1000个数字中,求出最大连续5个数的积

代码: 
  1.     public static int p8() {
  2.         String s = "73167176531330624919225119674426574742355349194934"
  3.                 + "96983520312774506326239578318016984801869478851843"
  4.                 + "85861560789112949495459501737958331952853208805511"
  5.                 + "12540698747158523863050715693290963295227443043557"
  6.                 + "66896648950445244523161731856403098711121722383113"
  7.                 + "62229893423380308135336276614282806444486645238749"
  8.                 + "30358907296290491560440772390713810515859307960866"
  9.                 + "70172427121883998797908792274921901699720888093776"
  10.                 + "65727333001053367881220235421809751254540594752243"
  11.                 + "52584907711670556013604839586446706324415722155397"
  12.                 + "53697817977846174064955149290862569321978468622482"
  13.                 + "83972241375657056057490261407972968652414535100474"
  14.                 + "82166370484403199890008895243450658541227588666881"
  15.                 + "16427171479924442928230863465674813919123162824586"
  16.                 + "17866458359124566529476545682848912883142607690042"
  17.                 + "24219022671055626321111109370544217506941658960408"
  18.                 + "07198403850962455444362981230987879927244284909188"
  19.                 + "84580156166097919133875499200524063689912560717606"
  20.                 + "05886116467109405077541002256983155200055935729725"
  21.                 + "71636269561882670428252483600823257530420752963450";
  22.         char[] cs = s.toCharArray();
  23.         int result = 1;
  24.         int index = 0;
  25.         for (int i = 0; i < s.length() - 5; i++) {
  26.             int num1 = cs[i] - '0';
  27.             int num2 = cs[i + 1] - '0';
  28.             int num3 = cs[i + 2] - '0';
  29.             int num4 = cs[i + 3] - '0';
  30.             int num5 = cs[i + 4] - '0';
  31.             if (num1 * num2 * num3 * num4 * num5 > result) {
  32.                 result = num1 * num2 * num3 * num4 * num5;
  33.                 index = i;
  34.             }
  35.         }
  36.         System.out.println(cs[index] + "" + cs[index + 1] + "" + cs[index + 2]
  37.                 + "" + cs[index + 3] + "" + cs[index + 4]);
  38.         return result;
  39.     }
Problem9
原题:

A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
a^2 + b^2 = c2

For example, 3^2 + 4^2 = 9 + 16 = 25 = 5^2.

There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.

分析:

求满足以下条件三个整数的积:

1,构成勾股数;2,和为1000.

无耻地暴力吧

代码: 
  1.     public static void p9() {
  2.         for (int a = 3; a <= 333; a++) {
  3.             for (int b = a + 1; b <= 666; b++) {
  4.                 int c = 1000 - a - b;
  5.                 if (c <= b) {
  6.                     break;
  7.                 }
  8.                 if (a * a + b * b == c * c) {
  9.                     System.out.printf("%d %d %d", a, b, c);
  10.                     System.out.println();
  11.                     System.out.printf("%d + %d = %d", a * a, b * b, c * c);
  12.                     System.out.println();
  13.                     System.out.printf("%d", a * b * c);
  14.                 }
  15.             }
  16.         }
  17.     }
Problem10
原题:

The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.

Find the sum of all the primes below two million.

分析:

求2百万以下质数的和

代码: 
  1.     public static long p10() {
  2.         List<Integer> array = new ArrayList<Integer>();
  3.         array.add(2);
  4.         int temp = 3;
  5.         int max = 2000000;
  6.         long sum = 2;
  7.         for (; temp < max; temp += 2) {
  8.             boolean isPrime = true;
  9.             int sqrt = (int) Math.sqrt(temp);
  10.             for (Integer i : array) {
  11.                 if (temp % i == 0) {
  12.                     isPrime = false;
  13.                 }
  14.                 if (i > sqrt) {
  15.                     break;
  16.                 }
  17.             }
  18.             if (isPrime) {
  19.                 array.add(temp);
  20.                 sum += temp;
  21.             }
  22.         }
  23.         return sum;
  24.     }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值