Prime factorization

Mesham is a type oriented programming language allowing the writing of high performance parallel codes which are efficient yet simple to write and maintain

Overview
This example will perform prime factorization on a number parallely, to return the prime factors which make up that number. The example uses the primitive communication, all reduce. There are actually a number of ways such a result can be obtained - this example is a simple parallel algorithm for this job.

Source Code
var n:=976; // this is the number to factorize var m:=10; // number of processes to use var s:Int :: allocated[multiple[]]; function void main { var p;   par p from 0 to m - 1 { var k:=p; var divisor; var quotient; while (n > 1) { divisor:= getprime(k); quotient:= n / divisor; var remainder:= n % divisor; if (remainder == 0) { n:=quotient; } else { k:=k + m;         }; s :: allreduce["min"]:=n; if ((s==n) && (quotient==n)) { print(itostring(divisor)+"\n"); };         n:=s; };   }; };
 * 1) include 
 * 2) include
 * 3) include

This code requires at least Mesham version 1.0

Download
You can download the prime factorization source code here and a legacy version for Mesham 0.5 is also available here