Problem 10-2

CSharp · Console

/Program.cs

// Write a function that prints prime factors of given integer number n>1. 
// Only simple variables and recursion are allowed, no loops.
using System;

public class MainClass 
{
	// Input: 1 < div <= n
	// Returns all prime factors of n in the range div...n
	// Completion: n - div is not negative and decreases at least by 1 in each nested call
	static void pfs(int n, int div)
	{
		if ( n != 1 )
		{
			if(n % div == 0)
			{
				Console.Write(" {0}", div);
				pfs(n / div, div);
			}
			else
			{
				pfs(n, div + 1);
			}
		}
	}
	
	public static void Main (string[] args) 
	{
		pfs(1001, 2);
	}
}
;