Tuesday, August 25, 2009

Highest Prime Number



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
public class PrimeNumber {

/*
* Given a number, finds the highest prime number.
*/
public static int highestPrime(int n) {
int value = 1;

for (int i = 2; i <= n; i++)
{
System.Console.WriteLine("n: " + n + " i: " + i + " mod: " + n % i);
if (i == n) { value = n; break;
}
if (n % i == 0)
{
n--;
}
}
return value;
}
static void Main(string[] args)
{
int value = highestPrime(100);
System.Console.WriteLine("Prime:" + value.ToString());
System.Console.Read();
}
}
}
}

No comments:

Post a Comment