Monday 9 July 2018

ROBIN MILLER CODE | JAVA

  1. CODING FOR GRAPHIC PRIME:
package javaapplication2;
import java.awt.*;
import java.awt.geom.*;
import javax.swing.*;
public class GraphingData extends JPanel {
    double[] data = {

        10007, 10009, 10037, 10039, 10061, 10067, 10069, 10079, 10091, 10093,10099, 10103, 10111 ,10133, 10139, 10141 ,10151, 10159, 10163, 10169, 10177, 10181, 10193, 10211 ,10223, 10243, 10247 ,10253, 10259, 10267, 10271, 10273, 10289, 10301 ,10303, 10313, 10321, 10331, 10333, 10337 ,10343, 10357, 10369 ,10391 ,10399 ,10427 ,10429 ,10433 ,10453, 10457, 10459 ,10463, 10477, 10487, 10499, 10501, 10513
    };
    final int PAD = 20;
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2 = (Graphics2D)g;
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
                            RenderingHints.VALUE_ANTIALIAS_ON);
        int w = getWidth();
        int h = getHeight();
        // for Drawing ordinates.
        g2.draw(new Line2D.Double(PAD, PAD, PAD, h-PAD));
        // for Drawing abcissa.
        g2.draw(new Line2D.Double(PAD, h-PAD, w-PAD, h-PAD));
        double xInc = (double)(w - 2*PAD)/(data.length-1);
        double scale = (double)(h - 2*PAD)/getMax();
        //for marking the points(data points)
        g2.setPaint(Color.red);
        for(int i = 0; i < data.length; i++) {
            double x = PAD + i*xInc;
            double y = h - PAD - scale*data[i];
            g2.fill(new Ellipse2D.Double(x-2, y-2, 4, 4));
        }
    }
    private int getMax() {
        int maximum = -Integer.MAX_VALUE;
        for(int i = 0; i < data.length; i++) {
            if(data[i] > maximum)
                maximum = (int) data[i];
        }
        return maximum;
    }
    public static void main(String[] args) {
        JFrame f = new JFrame();
        f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        f.add(new GraphingData());
        f.setSize(400,400);
        f.setLocation(200,200);
        f.setVisible(true);
    }
}
      2  CODING FOR  RABIN-MILLER ALGORITHM:
import java.util.Scanner;
import java.util.Random;
import java.math.BigInteger;
public class MillerRabin
{
    public boolean isPrimenumber(long m, int iterationnumber)
    {
       
        if (m == 0 || m == 1)
            return false;
        if (m == 2)
            return true;
        if (m % 2 == 0)
            return false;
        long t = m - 1;
        while (t % 2 == 0)
            t /= 2;
        Random ran = new Random();
        for (int j = 0; j < iterationnumber; j++)
        {
            long r = Math.abs(ran.nextLong());           
            long a = r % (m- 1) + 1, tempnum = t;
            long mod = modPow(a, tempnum, m);
            while (tempnum != m - 1 && mod != 1 && mod != m - 1)
            {
                mod = mulMod(mod, mod, m);
                tempnum *= 2;
            }
            if (mod != m - 1 && tempnum % 2 == 0)
                return false;
        }
        return true;       
    }
    public long modPow(long x, long y, long z)
    {
        long res = 1;
        for (int j = 0; j < y; j++)
        {
            res *= x;
            res %= z;
        }
        return res % z;
    }
        public long mulMod(long x, long y, long mod)
    {
        return BigInteger.valueOf(x).multiply(BigInteger.valueOf(y)).mod(BigInteger.valueOf(mod)).longValue();
    }
//using Main Function
    public static void main (String[] args)
    {
        Scanner scan = new Scanner(System.in);
        System.out.println("Using Miller Rabin Primality Algorithm Testn");
        MillerRabin mr = new MillerRabin();
        System.out.println("Enter the exact numbern");
        long number = scan.nextLong();
        System.out.println("nEnter the Iteration number");
        int k = scan.nextInt();
        boolean prime = mr.isPrimenumber(number, k);
        if (prime)
            System.out.println("n"+ number +" is primenumber");
        else
            System.out.println("n"+ number +" is not a primenumber is an Inconclusive");
    }
}
  1. CODING FOR IMPLEMENTING NEW CLASS:
package javaapplication3;
import java.util.Scanner;
public class NewClass {
 public static void main(String[] args) {
  int x,i,y = 0, b, a;int n = 0;
  Scanner s = new Scanner(System.in);
  System.out.println("enter the integer");
  a =s.nextInt();
  System.out.println("enter the no.of iterations");
  for(i=0;i<n;i++)
  {
      if(n>0||n<10)
      {
      }
  n= s.nextInt();
  }
 System.out.println("Enter the number to check ");
          y = s.nextInt();
  s.close();
  if (y == 0 || y == 1)
   System.out.println(y +" the number is Prime nor Composite" );
  for (x = 2; x <= y - 1; x++) {
   if (y % x == 0) {
    System.out.println(y +" the number is Composite" );
    break;
   }
  }
  if (y == x)
   System.out.println(y +"  the number is inconclusive " );
 }
}
CODING FOR PRIMENUMBERS 10000-11000
public class Prime_number {
    public static void main(String[] args) {
        int lower = 10000, higher = 11000;
        while (lower < higher) {
            boolean flag = false;
            for(int j = 2; j <= lower/2; ++j) {
               //looping condition
                if(lower % j == 0) {
                    flag = true;
                    break;
                }
            }
            if (!flag)
                System.out.println(lower + " ");
            ++lower;

No comments:

Post a Comment

Recent Questions

Learn 11 Unique and Creative Writing Examples | AssignmentHelp4Me

Learn 11 Unique and Creative Writing Examples | AssignmentHelp4Me elp4Meelp4Me