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 :
- // n range of numbers from 1 to n
- // p array of primes for divisibility check
- 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

Dharmraj ThakurPosted Jan 3, 2018, 7:30 AM
Željko PerićPosted Jan 3, 2018, 7:14 AM
Dharmraj ThakurPosted Jan 1, 2018, 2:01 AM