Permalink
Browse files

4 euler projects

  • Loading branch information...
ehthisham committed Mar 15, 2012
1 parent 2618b91 commit 119416db0a9db9876f504a83e37f21de1f0cb64f
Showing with 77 additions and 0 deletions.
  1. +23 −0 FibonacciAdd.java
  2. +16 −0 PrimeFactor.java
  3. +22 −0 PrimeSum.java
  4. +16 −0 Sum.java
View
@@ -0,0 +1,23 @@
+//By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
+public class FibonacciAdd{
+
+ public static void main(String[] args) {
+ long i = 1;
+ long j = 2;
+ long k = 3;
+ long sum = 2;
+
+ while(k < 4000000) {
+ if( k%2 == 0) {
+ sum = sum + k;
+ }
+ k = i + j;
+ i = j;
+ j = k;
+ }
+
+ System.out.println(" the sum is "+sum);
+ }
+
+
+}
View
@@ -0,0 +1,16 @@
+//Find the largest prime factor of a composite number.
+public class PrimeFactor{
+ public static void main(String[] args){
+ long n = 600851475143L;
+ int factor = 3;
+ while( n > 1)
+ {
+ if(n % factor == 0)
+ {
+ n/=factor;
+ }else
+ factor += 2;
+ }
+ System.out.println (factor);
+ }
+}
View
@@ -0,0 +1,22 @@
+//Find the sum of all the primes below two million?
+import static java.lang.Math.sqrt;
+public class PrimeSum{
+ public static void main(String[] args){
+ long n = 2;
+ long sum = 2;
+ int is_not_prime = 0;
+ while (n < 2000000){
+ n++;
+ for(int i=2;i<=Math.sqrt(n);i++){
+ if(n%i == 0){
+ is_not_prime = 1;
+ }
+ }
+ if(is_not_prime ==0){
+ sum = sum + n;
+ }
+ is_not_prime = 0;
+ }
+ System.out.println(sum);
+ }
+}
View
@@ -0,0 +1,16 @@
+public class Sum{
+ public static void main(String[] args){
+ int sum=0;
+ int num=0;
+ System.out.println("The sum of all natural number below 1000 which are multiples of 3 or 5");
+ while (num<999){
+ num++;
+ if(num%3 == 0 || num%5 == 0){
+ sum = sum + num ;
+ }
+ }
+ System.out.println("The sum is:"+ sum);
+ }
+
+}
+

0 comments on commit 119416d

Please sign in to comment.