Hello, there was interesting simple math problem I have recently solved :
- How many positive integers in a range 1 to n are divisible by at least one of given primes ?
Here is the recursive function that solves the problem :
-
-
- long f(long n, long [] p)
- {
- if(p.Length==0)
- return 0;
-
- long [] q = new long[p.Length-1];
-
- Array.Copy(p,1,q,0,p.Length-1);
-
- long r = (n/p[0])- (f(n/p[0],q)) + (f(n,q));
-
- return r;
- }
My question is :
- Is it possible to write this function in C# differently so that its execution becomes faster ?
All the best,
Željko Peric