Tuesday, July 17, 2012

Project Euler Problem 25 in Java

PROBLEM


The Fibonacci sequence is defined by the recurrence relation:

Fn = Fn1 + Fn2, where F1 = 1 and F2 = 1.
Hence the first 12 terms will be:

F1 = 1
F2 = 1
F3 = 2
F4 = 3
F5 = 5
F6 = 8
F7 = 13
F8 = 21
F9 = 34
F10 = 55
F11 = 89
F12 = 144
The 12th term, F12, is the first term to contain three digits.

What is the first term in the Fibonacci sequence to contain 1000 digits?


ANSWER

4782

JAVACODE

import java.math.BigInteger;

public class Problem25 {
public static void main(String[]args){
int term=3;
BigInteger pr=new BigInteger("1");
BigInteger in=new BigInteger("2");
for (; in.toString().length()<1000; ) {
BigInteger tm=pr;
pr=in;
in=in.add(tm);
term++;
}
System.out.println(term);
}
}

Project Euler Problem 16 in Java

PROBLEM


215 = 32768 and the sum of its digits is 3 + 2 + 7 + 6 + 8 = 26.

What is the sum of the digits of the number 21000?

ANSWER


1366

JAVA CODE

import java.math.BigInteger;

public class Problem16 {
public static void main(String[]args){
BigInteger mul=new BigInteger("1");
BigInteger bg=new BigInteger("2");
for (int i = 0; i < 1000; i++) {
mul=mul.multiply(bg);
}
int sum=0;
String text=mul.toString();
for (int i = 0; i < text.length(); i++) {
sum+=Integer.parseInt(new Character(text.charAt(i)).toString());
}
System.out.println(sum);
}
}


Project Euler Problem 9 in Java

PROBLEM


A Pythagorean triplet is a set of three natural numbers, a  b  c, for which,

a2 + b2 = c2
For example, 32 + 42 = 9 + 16 = 25 = 52.

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

ANSWER

31875000

JAVA CODE


public class Problem9 {
public static void main(String[]args){
int a=3;
int b=4;
int c=1000-(b+a);
int ans=0;
boolean find=false;
for (int i = 4; i < 496; i++) {

for (int j = 5; j < 499; j++) {

if((Math.pow(a,2)+Math.pow(b,2))==Math.pow(c,2)){
ans=a*b*c;
System.out.println("a="+a+"b="+b+"c="+c);
find=true;
break;
}
b=j;
c=1000-(b+a);
}

a=i;
if(find)
break;
}


System.out.println(ans);

}
}


java code for eular project problem 20

PROBLEM


n! means n  (n  1)  ...  3  2  1

For example, 10! = 10  9  ...  3  2  1 = 3628800,
and the sum of the digits in the number 10! is 3 + 6 + 2 + 8 + 8 + 0 + 0 = 27.

Find the sum of the digits in the number 100!

ANSWER

648

JAVA CODE


public class Problem20 {
public static void main(String[]args){
int ans=0;
BigInteger val=getNum(100);
String data=val.toString();
for (int i = 0; i < data.length(); i++) {
ans+=Integer.parseInt(new Character(data.charAt(i)).toString());
}
System.out.println(ans);
}

private static BigInteger getNum(int i) {
BigInteger ret=new BigInteger("1");
for (int j = 1; j <= i; j++) {
ret=ret.multiply(new BigInteger(Integer.toString(j)));
}
return ret;
}
}


answer java code for eular problem 10

PROBLEM


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

Find the sum of all the primes below two million.

ANSWER



142913828922

JAVA CODE

public class Problem10 {

public static void main(String[]args){
primeGenerator();
}

private static void primeGenerator() {
int max=2000000;
BigInteger bg=new BigInteger("0");
for (int j = 2; j < max; j++) {
boolean prime=true;
for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;
}
if(prime){
BigInteger add=new BigInteger(Integer.toString(j));
bg=bg.add(add);
}
}
System.out.println("ans="+bg);
}
}




java code for eular project problem5

PROBLEM


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 positive number that is evenly divisible by all of the numbers from 1 to 20?

ANSWER


232792560

JAVA CODE


public class Problem5 {
public static void main(String[]args){
int ans=0;
for (int i = 100000000; i <1000000000; i++) {
boolean state=true;
for (int j = 3; j <21; j++) {
ans=i;
if(i%j!=0||i==9999999){
//ans=-1;
state=false;
break;
}

}

if(state){
break;
}
}
System.out.println(ans);
}
}


Monday, July 16, 2012

java answer for eular project problem 7

PROBLEM 7



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 10 001st prime number?

ANSWER

104743

JAVA CODE


public class Problem7 {
public static void main(String[]args){


primeGenerator();
}

private static void primeGenerator() {

int max=775146;
int primeCount=0;
int ans=0;
for (int j = 2; j < max; j++) {
boolean prime=true;

for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;

}


if(prime){
primeCount++;
ans=j;
}

if(primeCount>=10001)
break;

}
System.out.println("ans="+ans);
}
}


eular project-problem6


PROBLEM 6
The sum of the squares of the first ten natural numbers is,

12 + 22 + ... + 102 = 385
The square of the sum of the first ten natural numbers is,

(1 + 2 + ... + 10)2 = 552 = 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.

ANSWER


25164150

JAVA CODE


public class Problem6 {
public static void main(String[]args){
int sumOfTheSquares=0;
int squareOfTheSum=0;
for (int i = 0; i <101; i++) {
sumOfTheSquares+=(int)Math.pow(i, 2);
squareOfTheSum+=i;
}
System.out.println((int)Math.pow(squareOfTheSum,2)-sumOfTheSquares);
}
}


answer for eular project-problem4


PROBLEM


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.

ANSWER
906609

JAVA CODE



public class Palindrome {
public static void main(String[]args){

int palindrome=0;
for (int i = 999; i >0; i--) {
for (int j = 999; j>0; j--) {
int prod=i*j;
String forward=Integer.toString(prod);
String reverse=new StringBuffer(forward).reverse().toString();
//System.out.println(forward+"back="+reverse);
if(forward.equalsIgnoreCase(reverse)&&prod>palindrome){

palindrome=i*j;

break;
}
}

}
System.out.println(palindrome);
}
}


answer for eula project- problem3


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

What is the largest prime factor of the number 600851475143 ?

ANSWER

6857

JAVA CODE

import java.math.BigInteger;

public class PrimeFactor {
public static void main(String[]args){
BigInteger req=new BigInteger("600851475143");
primeGenerator(req);
}

private static void primeGenerator(BigInteger req) {
int max=775146;//sqrt of 600851475143
int ans=0;
for (int j = 2; j < max; j++) {
boolean prime=true;
for (int i = 2; i <= (int)Math.sqrt(j); i++) {
if(j%i==0 )
prime=false;
}
if(prime){
BigInteger bi=new BigInteger(Integer.toString(j));
BigInteger mode=new BigInteger("0");
if(req.mod(bi).equals(mode) &&ans<j){
ans=j;
}
}
}
System.out.println("ans="+ans);
}
}


answer java programs for Eular projects-problem2


PROBLEM
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, ...
By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.

ANSWER
4613732

JAVA CODE


public class Fibbonaci {
public static void main(String []args){
int pre=1;
int sum=0;
for (int i = 2; i <4000000; ) {
if(i%2==0)
sum+=i;
int tmp=pre;
pre=i;
i+=tmp;
}
System.out.println(sum);
}
}

answer java programs for Eular projects-problem1



PROBLEM 1

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.

ANSWER
233168

JAVA CODE



public class NaturalNumber {

 public static void main(String[]args){
  int sum=0;
  for (int i = 3; i < 1000; i++) {
   if(i%3==0)
    sum+=i;
   else if(i%5==0)
    sum+=i;
  }
  System.out.println(sum);
 }
}

Thursday, July 12, 2012

build scripts

build scripts is the way of automating the deployment of a project.Such as make class file from java file, copy required libraries to required folders and make jar file etc...

following link help you to make class file of your project, copy libraries , and make single jar file which include all your class,libraries


http://www.mit.edu/~kimo/blog/jar_build.html