Tuesday, 5 June 2018

Project Euler Problem 4:Largest palindrome product

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.


Solution:-


 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
public class Problem_4{

     public static void main(String []args){
        
        int pal=0;
        int fdigit=0;
        int sdigit=0;
        
        for(int i=100;i<999;i++){
            
            for(int j=100;j<999;j++){
                
                int temp= i*j;
                if(isPalindrome(temp)){
                    if(temp>pal){
                        pal=temp;
                        fdigit=i;
                        sdigit=j;
                    }
                }
                
            }
            
        }
        
        System.out.println("Largest palindrome is "+pal+"\n First Digit is"+fdigit+"\n Second Digit is"+sdigit);
     }
     
     static boolean isPalindrome(int a){
         int rev=0;
         int temp=a;
         while(temp>0){
             rev=temp%10 + rev*10;
             temp=temp/10;
         }
         
         if(rev==a)
            return true;
        else
            return false;
         
         
     }
     
}

Monday, 4 June 2018

Project Problem 3: Largest prime factor

The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?

______________________________________________________________________________


Solution:


public class Problem_3{
    
    public static void main(String[] args){
        
        long a=600851475143l;
        long prime=0;
        
        for(long i=1l;i<a;a++){
            long temp=0l;
            if(a%i==0){
                if(isPrime(i)){
                    System.out.println("Prime factor is "+i);
                }
            }
        }
    }
    
    static boolean isPrime(long a){
        for(long i=2l;i<a;i++){
            if(a%i==0)
                return false;
        }
        
        return true;
    }
    
}

Project Euler Problem 2: Even Fibonacci numbers

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.

______________________________________________________________________________


Solution:-


public class Problem_2{

    public static void main(String[] args){
        
        int prev =1;
        int result=0;
        double sum=0;
        
        while(i<4000000){
            result=i+prev;
            prev=i;
            i=result;
            
            if(result(%2==0){
                sum=sum+result;
                System.out.print(","+i);
            }
        }
        
        System.out.println("\nSum is "+sum);
    }
}

Monday, 28 May 2018

Processing drawing using Math.





Code for the final dragrams.

float t=0;

void setup() {
  size(1200, 500);
  background(0);
  //Usingg same formula three time with different constant in tis case 7 and 11
  translate(200,200);
  for(t=0;t<Math.PI*2;t+=0.001){
    float x=cos(t) + cos(7 * t)/2 + sin(11 * t)/3;
    float y=sin(t) + sin(7 * t)/2 + cos(11 * t)/3;
    noStroke();
    color c=color(38,191,250);
    fill(c);
    ellipse(100*x,100*y,3,3);
  }
  
  //here 6 and 14
  resetMatrix();
  translate(600,200);
  
  for(t=0;t<Math.PI*2;t+=0.001){
      float x=cos(t) + cos(6 * t)/2 + sin(14 * t)/3;
      float y=sin(t) + sin(6 * t)/2 + cos(14 * t)/3;
      noStroke();
      color c=color(38,191,250);
      fill(c);
      ellipse(100*x,100*y,3,3);
  }
  // in las one 5 and 15
  resetMatrix();
  translate(1000,200);
  
  for(t=0;t<Math.PI*2;t+=0.001){
      float x=cos(t) + cos(5 * t)/2 + sin(15 * t)/3;
      float y=sin(t) + sin(5 * t)/2 + cos(15 * t)/3;
      noStroke();
      color c=color(38,191,250);
      fill(c);
      ellipse(100*x,100*y,3,3);
  }
}

Friday, 25 May 2018

Simple processing Generative Art/Drawing 4

 




Below is the code to generate the above shown drawing..

float r=0;

void setup() {
  size(400, 400);
  background(0);
  smooth();
  noStroke();
}

void draw() {
  
  if (r>300) {     // To reset the drawing
    fill(0);
    r=0;
    rect(0, 0, width, height);
  }
  rotate(r);
  translate(width/2, height/2); //to bring it to center
  fill(0, random(r, 255), random(r, 255));
  float circle_size= random(5, 15);
  ellipse(100+r, 10, circle_size, circle_size);
  r=r+0.2;
}

Thursday, 24 May 2018

Project Euler Problem 1 Solution in Java- Multiples of 3 and 5

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.

__________________________________________________________________________________


public class Problem_1 {

  public static void main(String[] args) {

    int n=10;
    int sum=0;
    for (int i=0; i<1000; i++) {
      if (i%3==0)
        sum+=i;
      else
        if (i%5==0)
          sum+=i;
    }
    System.out.println("Sum is "+sum);
  }
}



Answer: Sum is 233168

Tuesday, 8 May 2018

Cafe Wall Illusion using processing




Cafe Wall Illusion

int s=89;
color black=color(0);
color white=color(255);
Line l[];
float update=0;

void setup() {
  size(810, 680);
  stroke(89, 89, 89);
  strokeWeight(2);
  l=new Line[8];

  for (int i=0; i<8; i++) {
    l[i]=new Line(i*90);
  }
}

void draw() {

  for (int i=0; i<8; i++) {
    if (i%2==0)
      l[i].drawL(-270+update);
    else
      l[i].drawL(-270-update);
  }
  update=update+0.5;
  //println(update+"");
  if (update==180)
    update=0;
}

class Line {

  int n=20;
  float posY;

  Line(float posY) {
    this.posY=posY;
  }

  void drawL(float diff) {

    for (int i=0; i<16; i++) {
      float posX=i*90 +diff;
      if (i%2==0) {
        fill(0);
        rect(posX, posY, s, s);
      } else {
        fill(255);
        rect(posX, posY, s, s);
      }
    }
  }
}