##### Site index · List index
Message view
Top
Subject Factorial in Map-Reduce
Date Wed, 13 Oct 2010 06:57:20 GMT
```Hi
I am practising some programs in Map-Reduce such as WordCount, Word
Search  , Grep etc
Now I want to know is it possible to write Map-Reduce  program on hadoop
for finding *Factorial of Number*.
In that case how we give InputFormat, what are key-values etc.
I made this program in Java but not able to  convert  in Hadoop
Map-Reduce. Below is the  code :
import java.util.*;
public class FactMapReduce
{
List buckets = new ArrayList();
List intermediateresults = new ArrayList();
List values = new ArrayList();

public void init(int n)
{

for(int i = 1; i<=n; i++)
{
System.out.println(values);
}
System.out.println("**Running Conversion into Buckets**\n");
//convert the input data in smaller chunks. Here dividing 30 strings
into chunks of 6 chunks of 5.
List b = step1ConvertIntoBuckets(values,3);
System.out.println("*************DONE*******************\n\n");
System.out.println("************Running Map Function concurrently
for all Buckets**********\n\n");
List res = step2RunMapFunctionForAllBuckets(b);
System.out.println("*MAP Done*\n");
System.out.println(res);
System.out.println("**Running #Reduce Function# for collating
Intermediate Results and Printing Results\n");
step3RunReduceFunctionForAllBuckets(res);
System.out.println("*REDUCE Done*\n");
}
public List step1ConvertIntoBuckets(List list,int numberofbuckets)
{
int n = list.size();
int m = n / numberofbuckets;
int rem = n% numberofbuckets;

int count = 0;
System.out.println("BUCKETS");
for(int j =1; j<= numberofbuckets; j++)
{
List<String> temp = new ArrayList<String>();
for(int i=1; i<= m; i++)
{
count++;
}
temp = new ArrayList<String>();
}
if(rem != 0)
{
List<String> temp = new ArrayList<String>();
for(int i =1; i<=rem;i++)
{
count++;
}
}
System.out.println(buckets);
return buckets;
}

public List step2RunMapFunctionForAllBuckets(List list)
{
for(int i=0; i< list.size(); i++)
{
List<String> elementList = (ArrayList)list.get(i);
}
try
{
}catch(Exception e)
{    }
return intermediateresults;
}

public void step3RunReduceFunctionForAllBuckets(List list)
{
int sum =1;
for(int i=0; i< list.size(); i++)
{
//you can do some processing here, like finding max of all results etc
int t = Integer.parseInt((String)list.get(i));
sum *= t;
}
System.out.println("\nFactorial of num is  "+ sum+"\n");
}

{

private List<String> tempList = new ArrayList<String>();
{
tempList = list;
}
public void run()
{
int mul=1;
System.out.println("In Map...");

System.out.println(tempList);
for(int i=0;i < tempList.size();i++)
{  mul *= Integer.parseInt((String)tempList.get(i));
System.out.println(mul);
}
String s = String.valueOf(mul);
System.out.println(intermediateresults);
}
}

public static void main(String[] args)
{
FactMapReduce my = new FactMapReduce();
try
{