Primes

CSharp · Console

/Program.cs

using System;
using System.Collections.Generic;
using System.Linq;
					
public class Program
{
  private static IEnumerable<int> PrimeFactors(int value) {
    if (value <= 1)
      yield break;

    if (value % 2 == 0) {
      yield return 2; 

      while (value % 2 == 0)
        value /= 2;
    }

    int n = (int) (Math.Sqrt(value) + 0.5);

    for (int divisor = 3; divisor <= n; divisor += 2) 
      if (value % divisor == 0) {
        yield return divisor;

        do
          value /= divisor;
        while (value % divisor == 0);

        n = (int) (Math.Sqrt(value) + 0.5); 
      }

    if (value > 1) 
      yield return value;
  }
	
  public static void Main()	{
	Console.WriteLine(string.Join(" ", PrimeFactors(500)));
  }
}
;