Project Euler Problem 6,7,8,9,10 in Java
6) Problem 6- Project Euler Solution in Java
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.
My approach to the problem:
public class squareDifference {
/*
* @euler project 6
* @author Niraj patel
*/
public static void main(String[] args) {
int sum=0,sumSquare=0;
for(int num =1;num<101;num++){
sum+=num;
sumSquare+=num*num;
}
System.out.println(sum*sum - sumSquare);
}
}
7) Problem 7- Project Euler Solution in Java
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?
public class Prime {
/*
* @euler project 7
* @author Niraj patel
*/
public static void main(String[] args) {
int count =0,num=2;
while(count!=10001){
if(isPrime(num)){
count++;
}
if(count==10001){
System.out.println(num);
}
num++;
}
}
//Prime method.
public static boolean isPrime(int n){
boolean result = true;
for (int num=2; num*num<=n; num++){
if (n % num == 0){
result= false;
break;
}
}
return result;
}
}
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Okay, This one looks is ugly. But, it works!!
You could save the numbers in text file and can do it that way also. But, I wanted make it simple so I did it using string. In fact, the following problem can be done many ways, you could also do it using Two-D array. Leave comment below for any improvment.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Largestproduct {
/*
* @euler project 8
* @author Niraj patel
*/
public static ArrayList<Long> values = new ArrayList();
public static ArrayList<Integer> multiply = new ArrayList();
public static void main(String[] args) {
String numbers ="73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
String nums;
int limit =13,val = 0;
long multVal = 1,maxVal = 0;
//This can be improved further if you wish.
for (int i=0;i<1000-limit+1;i++){
multVal = 1;
//breaks string into 13 digits.
nums = numbers.substring(i,i+limit);
for (int j=0;j<limit;j++) {
//takes each element and convert it into digit.
val = Integer.parseInt(nums.substring(j,j+1));
multiply.add(val);
}
for (int j=0;j<=multiply.size()-1;j++){
multVal = multVal*multiply.get(j);
}
multiply.clear();
values.add(multVal);
}
System.out.println(Collections.max(values));
}
}
What is the 10 001st prime number?
public class Prime {
/*
* @euler project 7
* @author Niraj patel
*/
public static void main(String[] args) {
int count =0,num=2;
while(count!=10001){
if(isPrime(num)){
count++;
}
if(count==10001){
System.out.println(num);
}
num++;
}
}
//Prime method.
public static boolean isPrime(int n){
boolean result = true;
for (int num=2; num*num<=n; num++){
if (n % num == 0){
result= false;
break;
}
}
return result;
}
}
8) Problem 8- Project Euler Solution in Java
Find the thirteen adjacent digits in the 1000-digit number that have the greatest product. What is the value of this product?
Okay, This one looks is ugly. But, it works!!
You could save the numbers in text file and can do it that way also. But, I wanted make it simple so I did it using string. In fact, the following problem can be done many ways, you could also do it using Two-D array. Leave comment below for any improvment.
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class Largestproduct {
/*
* @euler project 8
* @author Niraj patel
*/
public static ArrayList<Long> values = new ArrayList();
public static ArrayList<Integer> multiply = new ArrayList();
public static void main(String[] args) {
String numbers ="73167176531330624919225119674426574742355349194934" +
"96983520312774506326239578318016984801869478851843" +
"85861560789112949495459501737958331952853208805511" +
"12540698747158523863050715693290963295227443043557" +
"66896648950445244523161731856403098711121722383113" +
"62229893423380308135336276614282806444486645238749" +
"30358907296290491560440772390713810515859307960866" +
"70172427121883998797908792274921901699720888093776" +
"65727333001053367881220235421809751254540594752243" +
"52584907711670556013604839586446706324415722155397" +
"53697817977846174064955149290862569321978468622482" +
"83972241375657056057490261407972968652414535100474" +
"82166370484403199890008895243450658541227588666881" +
"16427171479924442928230863465674813919123162824586" +
"17866458359124566529476545682848912883142607690042" +
"24219022671055626321111109370544217506941658960408" +
"07198403850962455444362981230987879927244284909188" +
"84580156166097919133875499200524063689912560717606" +
"05886116467109405077541002256983155200055935729725" +
"71636269561882670428252483600823257530420752963450";
String nums;
int limit =13,val = 0;
long multVal = 1,maxVal = 0;
//This can be improved further if you wish.
for (int i=0;i<1000-limit+1;i++){
multVal = 1;
//breaks string into 13 digits.
nums = numbers.substring(i,i+limit);
for (int j=0;j<limit;j++) {
//takes each element and convert it into digit.
val = Integer.parseInt(nums.substring(j,j+1));
multiply.add(val);
}
for (int j=0;j<=multiply.size()-1;j++){
multVal = multVal*multiply.get(j);
}
multiply.clear();
values.add(multVal);
}
System.out.println(Collections.max(values));
}
}
9) Problem 9- Project Euler Solution in Java
A Pythagorean triplet is a set of three natural numbers, a < b < c, for which,
There exists exactly one Pythagorean triplet for which a + b + c = 1000.
Find the product abc.
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.
public class main {
/**
* @euler project 9
* @author Niraj patel
* @Finding pythagorean triplet
*/
public static void main(String[] args) {
// Find a number a+b+c=1000 where a<b<c.
int a = 0, b = 0, c = 0;
int limit = 1000;
boolean isFound = false;
for (a = 1; a < limit; a++) {
for (b = a; b < limit; b++) {
// as given a+b+c=1000 --> c must equal c = 1000-a-b
c = limit - a - b;
if (a * a + b * b == c * c) {
isFound = true;
break;
}
}
if (isFound) {
System.out.println(a*b*c);
break;
}
}
}
10) Problem 10- Project Euler Solution in Java
The sum of the primes below 10 is 2 + 3 + 5 + 7 = 17.
Find the sum of all the primes below two million.
Find the sum of all the primes below two million.
public class sumOfTwoM {
/*
* @euler project 10
* @author Niraj patel
*
*/
public static void main(String[] args) {
long sum =2;
boolean isPrime = true;
for (int i=3; i<2000000; i++) {
isPrime = true;
//There is no need to go beyond the square root of number to check weather it is prime or not.
//we only need to test for factors less than or equal to the square root.
for (int j=2; j<=(int)Math.sqrt(i); j++){
if (i % j == 0){
isPrime = false;
break;
}
}
if(isPrime){
sum +=i;
}
}
System.out.println("Sum = "+sum);
}
}
Comments
Post a Comment