Project Euler Problem 1,2,3,4,5 in Java
NOTE : Don't look at the solution if you had not tried it yourself. Because real learning is an active process and seeing how it is done is a long way from experiencing that epiphany of discovery.So you try possible solution but something doesn't work and it's hours now when that could be done in minutes. There is nothing wrong in looking at the solution if you have been doing it for long and couldn't figure it out. After looking the solution you may realize what bit of changes you could do for better result. It might be something very silly or stupid thing that you forgot and it's very frustrating.No matter how frustrating problems are, there is almost certainly a solution out there on web. These are my attempts or solution to the problems it might be simple or complex depending on how well you understand my algorithm. I have solved each problem using Java. It took me less than hour in some problems to design efficient and successful algorithm to meet the "One-minute rule" of Euler.
1) Problem 1 - Project Euler
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.
One possible solution:
public class main {
/***
* @Euler project 1
* @Sum of all multiple of 3 and 5 below 1000
* @Niraj Patel
*/
public static void main(String[] args) {
int sum=0;
for(int i=1;i<1000;i++){
if(i%3==0 || i%5==0){
sum+=i;
}
}
System.out.println(sum);
}
}
2) Problem 2 - Project Euler
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:
One possible solution:
public class main {
/**
* @Euler problem 2
* @Sum of the even Fibonacci terms
* @author Niraj patel
*/
public static void main(String[] args) {
int fib1 = 1,fib2 = 1,newFib = 2,sum = 0;
while(newFib < 4000000){
if(newFib % 2 == 0){
sum += newFib;
}
fib1 = fib2;
fib2 = newFib;
newFib = fib1 + fib2; //Fibonacci sequence is generated by adding the previous two terms.
} System.out.println("Sum = " + sum);
}
}
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.One possible solution:
public class main {
/**
* @Euler problem 2
* @Sum of the even Fibonacci terms
* @author Niraj patel
*/
public static void main(String[] args) {
int fib1 = 1,fib2 = 1,newFib = 2,sum = 0;
while(newFib < 4000000){
if(newFib % 2 == 0){
sum += newFib;
}
fib1 = fib2;
fib2 = newFib;
newFib = fib1 + fib2; //Fibonacci sequence is generated by adding the previous two terms.
} System.out.println("Sum = " + sum);
}
}
3) Problem 3 - Project Euler
The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
One possible solution:
public class main {
/**
* @Euler Project 3
* @Largest prime factor of 600851475143
* @Niraj Patel
*/
public static void main(String[] args) {
long num = 600851475143L;
long newnum = num;
long largestFactor = 0;
int counter = 2;
// counter * counter : To improve the algorithm, Fundamental Theorem of Arithmetic (Logical explanation can be found on web).
while (counter * counter <= newnum) {
if (newnum % counter == 0) {
newnum = newnum / counter;
largestFactor = counter;
} else {
counter++;
}
}
if (newnum > largestFactor) {
largestFactor = newnum;
}
System.out.println(largestFactor);
}
}
One possible solution:
public class main {
/**
* @Euler Project 3
* @Largest prime factor of 600851475143
* @Niraj Patel
*/
public static void main(String[] args) {
long num = 600851475143L;
long newnum = num;
long largestFactor = 0;
int counter = 2;
// counter * counter : To improve the algorithm, Fundamental Theorem of Arithmetic (Logical explanation can be found on web).
while (counter * counter <= newnum) {
if (newnum % counter == 0) {
newnum = newnum / counter;
largestFactor = counter;
} else {
counter++;
}
}
if (newnum > largestFactor) {
largestFactor = newnum;
}
System.out.println(largestFactor);
}
}
4) Problem 4 - Project Euler
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.
One possible solution:
import java.util.ArrayList;
import java.util.Collections;
public class main {
/**
* @Euler Project 4
* @Largest palindrome of three digit
* @Niraj Patel
*/
static int number=0,reversnum = 0,reminder,original;
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList();
for(int i=100;i<=999;i++){
for(int j=100;j<=999;j++){
number = i*j;
original=number;
//algorithm for palindrome number
while(number>0){
reminder = number%10;
reversnum = reversnum*10+reminder;
number= number/10;
}
if(reversnum==original){
list.add(original);
}
original=0;
reversnum=0;
}
}
System.out.println(Collections.max(list));// To get maximum number from the list
}
}
One possible solution:
import java.util.ArrayList;
import java.util.Collections;
public class main {
/**
* @Euler Project 4
* @Largest palindrome of three digit
* @Niraj Patel
*/
static int number=0,reversnum = 0,reminder,original;
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList();
for(int i=100;i<=999;i++){
for(int j=100;j<=999;j++){
number = i*j;
original=number;
//algorithm for palindrome number
while(number>0){
reminder = number%10;
reversnum = reversnum*10+reminder;
number= number/10;
}
if(reversnum==original){
list.add(original);
}
original=0;
reversnum=0;
}
}
System.out.println(Collections.max(list));// To get maximum number from the list
}
}
5) Problem 5 - Project Euler
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?
one possible solution:
This one is little dirty but I think it is the simplest and quickest possible solution.
public class main {
/**
* @Euler Project 5
* @Smallest evenly divisible number by all 1 to 20.
* @Niraj Patel
*/
public static void main(String[] args) {
int number = 1000; // Obviously there is no number less than 1000 evenly divisible by all 1 to 20.
while (!(number % 20 == 0 && number % 19 == 0 && number % 18 ==0
&&number % 17 == 0 && number % 16 == 0 && number % 15 ==0
&&number % 14 == 0 && number % 13 == 0 && number %12==0 &&number % 11 == 0)) {
number += 20; //Increment by 20
}
System.out.println("The number that is evenly divisible by all of the numbers from 1 to 20: " + number);
}
}
one possible solution:
This one is little dirty but I think it is the simplest and quickest possible solution.
public class main {
/**
* @Euler Project 5
* @Smallest evenly divisible number by all 1 to 20.
* @Niraj Patel
*/
public static void main(String[] args) {
int number = 1000; // Obviously there is no number less than 1000 evenly divisible by all 1 to 20.
while (!(number % 20 == 0 && number % 19 == 0 && number % 18 ==0
&&number % 17 == 0 && number % 16 == 0 && number % 15 ==0
&&number % 14 == 0 && number % 13 == 0 && number %12==0 &&number % 11 == 0)) {
number += 20; //Increment by 20
}
System.out.println("The number that is evenly divisible by all of the numbers from 1 to 20: " + number);
}
}
Comments
Post a Comment