Created
January 4, 2013 05:21
-
-
Save gogsbread/4450162 to your computer and use it in GitHub Desktop.
Basic numerical algorithms IsPrime(n) and ToBinary(n) where n is Integer.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
namespace RandomMusings | |
{ | |
public class NumericalAlgorithms | |
{ | |
static void Main(){ | |
/*Console.WriteLine(IsPrime(13)); | |
Console.WriteLine(IsPrime(7)); | |
Console.WriteLine(IsPrime(4));*/ | |
Console.WriteLine(ToBinary(742)); | |
} | |
static bool IsPrime(int n){ | |
int srt = (int)(Math.Sqrt((double)n)); | |
bool isPrime = true; | |
for(int i=2; i <= srt; i++){ | |
if(n % i == 0){ | |
isPrime = false; | |
break; | |
} | |
} | |
return isPrime; | |
} | |
static string ToBinary(int n){ | |
int r = 0; | |
List<int> binary = new List<int>(); | |
while(n / 2 != 0){ | |
r = n%2; | |
binary.Add(r); | |
n = n / 2; | |
} | |
binary.Add(1); | |
string sb = string.Empty; | |
foreach(int b in binary){ | |
sb = b + sb; | |
} | |
return sb; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment