| 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倍数的和。
可以直接循环求,也可以用抽屉原理及等差数列求和公式来。 |
| 代码: |
- public static int problem1(int max) {
- int sum3 = sumOfArray(3, (max - 1) / 3, 3);
- int sum5 = sumOfArray(5, (max - 1) / 5, 5);
- int sum15 = sumOfArray(15, (max - 1) / 15, 15);
- return sum3 + sum5 - sum15;
- }
-
- private static int sumOfArray(int a1, int n, int d) {
- return n * a1 + n * (n - 1) * d / 2;
- }
|
| 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且为偶数的和
慢慢来呗! |
| 代码: |
- public static int problem2(int max) {
- int pre = 1;
- int next = 2;
- int sum = 0;
- while (next <= max) {
- if (next % 2 == 0) {
- sum += next;
- }
- System.out.println(pre + "\t" + next + "\t" + sum);
- int temp = pre;
- pre = next;
- next += temp;
- }
- return sum;
- }
|
| Problem3 |
| 原题: |
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ? |
| 分析: |
就是求600851475143最大质因数。
|
| 代码: |
- public static long problem3(long num) {
- while (true) {
- long temp = 0;
- for (long i = 2; i <= Math.sqrt(num); i++) {
- if (num % i == 0) {
- temp = (num / i);
- break;
- }
- }
- if (temp == 0) {
- break;
- }
- num = temp;
- }
- return num;
- }
|
| 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:回文数
题目求的是两个三位数积中最大的回文数,邪恶的暴力。
|
| 代码: |
- public static int problem4() {
- int result = -1;
- for (int i = 999; i >= 100; i--) {
- for (int j = i; j >= 100; j--) {
- if (isPalindromic(i * j)) {
- if (i * j > result) {
- result = i * j;
- }
- }
- }
- }
- return result;
- }
- private static boolean isPalindromic(int num) {
- char[] cs = String.valueOf(num).toCharArray();
- for (int i = 0; i < cs.length / 2; i++) {
- if (cs[i] != cs[cs.length - i - 1]) {
- return false;
- }
- }
- return true;
- }
|
| 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中所有质数的公倍数,所以暴力吧! |
| 代码: |
- public static int problem5() {
- int begin = 1 * 3 * 5 * 7 * 11 * 13 * 17 * 19;
- for (int i = begin;; i += begin) {
- boolean check = true;
- for (int j = 1; j <= 20; j++) {
- if (i % j != 0) {
- check = false;
- }
- }
- if (check) {
- System.out.println(i / begin);
- return i;
- }
- }
- }
|
| 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和的平方的差
|
| 代码: |
- public static int problem6() {
- int result0 = 0;
- int result1 = 0;
- for (int i = 1; i <= 100; i++) {
- result0 += (i * i);
- result1 += i;
- }
- return result0 - result1 * result1;
- }
|
| 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个质数 |
| 代码: |
- public static int p7() {
- int length = 10001;
- List<Integer> array = new ArrayList<Integer>(length);
- array.add(2);
- int temp = 3;
- while (array.size() < length) {
- boolean isPrime = true;
- for (Integer i : array) {
- if (temp % i == 0) {
- isPrime = false;
- }
- }
- if (isPrime) {
- array.add(temp);
- }
- temp++;
- }
- return array.get(length - 1);
- }
|
| 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个数的积 |
| 代码: |
- public static int p8() {
- String s = "73167176531330624919225119674426574742355349194934"
- + "96983520312774506326239578318016984801869478851843"
- + "85861560789112949495459501737958331952853208805511"
- + "12540698747158523863050715693290963295227443043557"
- + "66896648950445244523161731856403098711121722383113"
- + "62229893423380308135336276614282806444486645238749"
- + "30358907296290491560440772390713810515859307960866"
- + "70172427121883998797908792274921901699720888093776"
- + "65727333001053367881220235421809751254540594752243"
- + "52584907711670556013604839586446706324415722155397"
- + "53697817977846174064955149290862569321978468622482"
- + "83972241375657056057490261407972968652414535100474"
- + "82166370484403199890008895243450658541227588666881"
- + "16427171479924442928230863465674813919123162824586"
- + "17866458359124566529476545682848912883142607690042"
- + "24219022671055626321111109370544217506941658960408"
- + "07198403850962455444362981230987879927244284909188"
- + "84580156166097919133875499200524063689912560717606"
- + "05886116467109405077541002256983155200055935729725"
- + "71636269561882670428252483600823257530420752963450";
- char[] cs = s.toCharArray();
- int result = 1;
- int index = 0;
- for (int i = 0; i < s.length() - 5; i++) {
- int num1 = cs[i] - '0';
- int num2 = cs[i + 1] - '0';
- int num3 = cs[i + 2] - '0';
- int num4 = cs[i + 3] - '0';
- int num5 = cs[i + 4] - '0';
- if (num1 * num2 * num3 * num4 * num5 > result) {
- result = num1 * num2 * num3 * num4 * num5;
- index = i;
- }
- }
- System.out.println(cs[index] + "" + cs[index + 1] + "" + cs[index + 2]
- + "" + cs[index + 3] + "" + cs[index + 4]);
- return result;
- }
|
| 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.
无耻地暴力吧 |
| 代码: |
- public static void p9() {
- for (int a = 3; a <= 333; a++) {
- for (int b = a + 1; b <= 666; b++) {
- int c = 1000 - a - b;
- if (c <= b) {
- break;
- }
- if (a * a + b * b == c * c) {
- System.out.printf("%d %d %d", a, b, c);
- System.out.println();
- System.out.printf("%d + %d = %d", a * a, b * b, c * c);
- System.out.println();
- System.out.printf("%d", a * b * c);
- }
- }
- }
- }
|
| 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百万以下质数的和
|
| 代码: |
- public static long p10() {
- List<Integer> array = new ArrayList<Integer>();
- array.add(2);
- int temp = 3;
- int max = 2000000;
- long sum = 2;
- for (; temp < max; temp += 2) {
- boolean isPrime = true;
- int sqrt = (int) Math.sqrt(temp);
- for (Integer i : array) {
- if (temp % i == 0) {
- isPrime = false;
- }
- if (i > sqrt) {
- break;
- }
- }
- if (isPrime) {
- array.add(temp);
- sum += temp;
- }
- }
- return sum;
- }
|
发表于 @
2008年08月25日 14:16:00 | | 编辑|
举报| 收藏