My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
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
package com.google.code.projecteuler;

import java.math.BigDecimal;
import java.util.Date;

/**
* 2^15 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.
*
* <p/>
*
* What is the sum of the digits of the number 2^1000?
*
* @link <a
* href="http://projecteuler.net/index.php?section=problems&id=16">Problem
* 16</a>
* @author Cesar Arevalo
*/
public class Problem016 {

public static void main(String[] args) {
Date startDate = new Date();
System.out.println("startDate: " + startDate.getTime());

int base = 2;
int power = 1000;
BigDecimal baseToThePowerOf = new BigDecimal(base);
baseToThePowerOf = baseToThePowerOf.pow(power);

System.out.println("twoToThePowerOf1000: " + baseToThePowerOf);

String baseToThePowerOfString = baseToThePowerOf.toString();

System.out.println("twoToThePowerOf1000String: "
+ baseToThePowerOfString);

int sumOfDigits = 0;

for (int i = 0; i < baseToThePowerOfString.length(); i++) {
sumOfDigits += Integer.valueOf(baseToThePowerOfString.substring(i,
i + 1));
}

System.out.println("sumOfDigits: " + sumOfDigits);

Date endDate = new Date();
System.out.println("endDate: " + endDate.getTime());
System.out.println("time in millis: "
+ (endDate.getTime() - startDate.getTime()));
}
}

Change log

r20 by cesar.arevalo1 on Feb 26, 2009   Diff
- Renaming the folder euler to java, so it
is descriptive of what type of code it
contains.
- Renaming the project of each java and
flex type of solutions to euler_java and
euler_flex so it is more descriptive of
the different solutions projects.
Go to: 
Project members, sign in to write a code review

Older revisions

r10 by cesar.arevalo1 on Nov 29, 2008   Diff
Renaming the root package so it
adheres to the place where the project
is hosted.
r8 by cesar.arevalo1 on Oct 17, 2008   Diff
Solved problem 16
All revisions of this file

File info

Size: 1351 bytes, 50 lines
Powered by Google Project Hosting