Advertisement
DaWoblefet

Probability of N Set of Unions start

Apr 18th, 2017
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.67 KB | None | 0 0
  1. import java.util.Scanner;
  2.  
  3. public class UnionNSets
  4. {
  5.     public static void main(String[] args)
  6.     {
  7.         Scanner keyboard = new Scanner(System.in);
  8.         int i;
  9.         int size;
  10.         double totalProbability = 0;
  11.         double probArray[];
  12.  
  13.         System.out.println("This only works for 3-set unions atm");
  14.         System.out.print("Please enter the size of the array: ");
  15.         size = keyboard.nextInt();
  16.         probArray = new double[size];
  17.        
  18. //Gathering input
  19.         for (i = 0; i < size; i++)
  20.         {
  21.             System.out.print("Please enter element array[" + i + "]: ");
  22.             probArray[i] = keyboard.nextDouble();
  23.         }
  24.  
  25.         totalProbability += calcOnes(probArray, size);
  26.         totalProbability -= calcTwos(probArray, size);
  27.         totalProbability += calcThrees(probArray, size);
  28.  
  29.         System.out.println("Probability of " + size + "-set union: " + totalProbability);
  30.     }
  31.  
  32. //Sums the initial probabilities
  33.     public static double calcOnes(double[] probArray, int size)
  34.     {
  35.         double result = 0;
  36.         for (int i = 0; i < size; i++)
  37.         {
  38.             result+= probArray[i];
  39.         }
  40.         return result;
  41.     }
  42.  
  43. //Subtracts every intersection of pairs
  44.     public static double calcTwos(double[] probArray, int size)
  45.     {
  46.         double result = 0;
  47.         for (int i = 0; i < size - 1; i++)
  48.         {
  49.             for (int j = i + 1; j < size; j++)
  50.             {
  51.                 result+= probArray[i] * probArray[j];
  52.             }
  53.         }
  54.         return result;
  55.     }
  56.  
  57. //Sums every intersection of three-pairs
  58.     public static double calcThrees(double[] probArray, int size)
  59.     {
  60.         double result = 0;
  61.         for (int i = 0; i < size - 2; i++)
  62.         {
  63.             for (int j = i + 1; j < size - 1; j++)
  64.             {
  65.                 for (int k = j + 1; k < size; k++)
  66.                 {
  67.                     result+= probArray[i] * probArray[j] * probArray[k];
  68.                 }
  69.             }
  70.         }
  71.         return result;
  72.     }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement