I'm completing a pretty easy programming excersize with the following code:
using System;
namespace Factorial
{
class MainClass
{
static int fives(int x) {
int r = 0;
while(x % 5 == 0) {
r++;
x /= 5;
}
return r;
}
static int z(int x) {
if (x == 1)
return 0;
else
return z (x-1) + fives (x);
}
public static void Main (string[] args)
{
int testCases = Convert.ToInt32 (Console.ReadLine ());
int[] xs = new int[testCases];
for (int i=0; i<testCases; i++)
xs [i] = Convert.ToInt32 (Console.ReadLine ());
foreach (int x in xs)
Console.WriteLine (z (x));
}
}
}
It seems to work OK with small numbers, but with 8735373 from the example it prints "Segmentation fault: 11". Does it mean that I run out of memory because of recursion going in too deep? What causes it?
(I run C# in Mono 2.10.8 on a Mac.)
P.S.: If anyone's interested in the excersize itself, here's my final solution (much more optimized).