Skip to content

Instantly share code, notes, and snippets.

@tanayseven
Created April 8, 2016 11:19
Show Gist options
  • Save tanayseven/c32d10e96d619bd6a0085a9809c731e4 to your computer and use it in GitHub Desktop.
Save tanayseven/c32d10e96d619bd6a0085a9809c731e4 to your computer and use it in GitHub Desktop.
String to upper
using System;
using System.Threading;
using System.Threading.Tasks;
namespace StringToUpper
{
class ParallelString
{
string str;
ParallelString(string str){
this.str = str;
}
public void ParallelChangeCase() {
string []sub_str = str.Substring ();
Parallel.ForEach (0, str.Length, i => {
});
}
}
class Matrix
{
long [,]matA;
public void RandomizeMat (int n, int m){
matA = new long[n, m];
var rnd = new Random (int.Parse(DateTime.Now.ToString("HHmmssfff"))+(int)(n));
for (long i = 0 ; i < n ; ++i) {
for (long j = 0; j < m; ++j) {
matA [i,j] = rnd.Next (0, n);
}
}
}
private int IsPrime(long n) {
int res = 1;
Parallel.For (2,(int)Math.Ceiling(Math.Sqrt(n))+1, i => {
if(n%i == 0){
res = 0;
}
});
if (n == 2) {
res = 1;
} else if (n <= 1) {
res = -1;
}
return res;
}
private bool IsOdd(long n) {
return ((n & 1) == 1);
}
public void ParallelPrimeReplace(){
Parallel.For (0, matA.GetLength (0), i => {
Parallel.For(0, matA.GetLength(1), j => {
matA[i,j] = IsPrime(matA[i,j]);
});
});
}
public void ParallelOddSqEvenCuReplace(){
Parallel.For (0, matA.GetLength (0), i => {
Parallel.For(0, matA.GetLength(1), j => {
matA[i,j] = (long) (IsOdd(Thread.CurrentThread.ManagedThreadId)?Math.Pow(matA[i,j],2):Math.Pow(matA[i,j],3));
});
});
}
public override string ToString() {
string res = "";
for (long i = 0; i < matA.GetLength (0); ++i) {
for (long j = 0; j < matA.GetLength (1); ++j) {
res += (matA [i, j] + "\t");
}
res += "\n";
}
return res;
}
}
class MainClass
{
public static void Main (string[] args)
{
var mat = new Matrix ();
mat.RandomizeMat (5,5);
Console.WriteLine (mat);
mat.ParallelPrimeReplace ();
Console.WriteLine (mat);
mat.RandomizeMat (5,5);
Console.WriteLine (mat);
mat.ParallelOddSqEvenCuReplace ();
Console.WriteLine (mat);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment