Difference between revisions of "TADM2E 3.28"

From Algorithm Wiki
Jump to: navigation, search
(Remove strange boiler plate code at the start.)
(Clean up the code and state that it is Java.)
Line 8: Line 8:
 
<math>M_k=P_{k-1}Q_{k+1}, k\in[1,n]</math>
 
<math>M_k=P_{k-1}Q_{k+1}, k\in[1,n]</math>
 
-------------------------------------------------------------------------------------
 
-------------------------------------------------------------------------------------
Using Iteration
+
Using Iteration:
  
 +
Java example:
 
<pre>
 
<pre>
 
public class Multiplication {
 
public class Multiplication {
+
  public static int[] product(int[] x) {  
      private static int productLeft(int[] x,int i,int j){
+
    int[] M = new int[x.length];
if(i<0)
+
 
return 1;
+
    for (int i = 0; i < x.length; i++) {
return x[i]*productLeft(x, i-1, j);
+
  M[i] = product(x, M, i + 1, x.length);
}
+
    }
      private static int productRight(int[] x,int i,int j){
+
 
if(i>=j)
+
    return M;
return 1;
+
  }
return x[i]*productRight(x, i+1, j);
+
 
}
+
  private static int productLeft(int[] x, int i, int j) {
        private static int product(int[] x,int[] y,int i,int j){
+
    if (i < 0)
  if(i==j){
+
      return 1;
  return productLeft(x, i-2, j);
+
 
        }
+
    return x[i] * productLeft(x, i - 1, j);
return x[i]*productLeft(x, i-2, j)*productRight(x, i+1, j);
+
  }
            }
+
 
  public static int[] product(int[] x){  
+
  private static int productRight(int[] x, int i, int j) {
  int[] M=new int[x.length];
+
    if (i >= j)
  for (int i=0;i<x.length;i++){
+
      return 1;
    M[i]=product(x, M, i+1, x.length);
+
 
  }
+
    return x[i] * productRight(x, i + 1, j);
  return M;
+
  }
  }
+
 
 +
  private static int product(int[] x, int[] y, int i, int j) {
 +
    if(i == j)
 +
      return productLeft(x, i - 2, j);
 +
 
 +
    return x[i] * productLeft(x, i - 2, j) * productRight(x, i + 1, j);
 +
  }
 
}
 
}
 
</pre>
 
</pre>
  
 
--[[User:Tnoumessi|Tnoumessi]] ([[User talk:Tnoumessi|talk]]) 00:21, 8 April 2015 (EDT)
 
--[[User:Tnoumessi|Tnoumessi]] ([[User talk:Tnoumessi|talk]]) 00:21, 8 April 2015 (EDT)

Revision as of 09:21, 8 November 2015

We need two passes over X:

1. Calculate cumulative production P and Q:
$ P_0 = 1, P_k=X_k P_{k-1}=\prod_{i=1}^kx_i $
$ Q_n = 1, Q_k=X_k Q_{k+1}=\prod_{i=k}^nx_i $

2. Calculate M:
$ M_k=P_{k-1}Q_{k+1}, k\in[1,n] $


Using Iteration:

Java example:

public class Multiplication {
  public static int[] product(int[] x) {	   
    int[] M = new int[x.length];

    for (int i = 0; i < x.length; i++) {
		  M[i] = product(x, M, i + 1, x.length); 
    }

    return M;
  }

  private static int productLeft(int[] x, int i, int j) {
    if (i < 0)
      return 1;

    return x[i] * productLeft(x, i - 1, j);
  }

  private static int productRight(int[] x, int i, int j) {
    if (i >= j)
      return 1;

    return x[i] * productRight(x, i + 1, j);
  }

  private static int product(int[] x, int[] y, int i, int j) {	
    if(i == j)
      return productLeft(x, i - 2, j);
  
    return x[i] * productLeft(x, i - 2, j) * productRight(x, i + 1, j);
  }
}

--Tnoumessi (talk) 00:21, 8 April 2015 (EDT)