TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Željko Perić
413
3.8k
223k
How many numbers 1 - n are divisible by a range of primes
Dec 30 2017 4:15 PM
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
Reply
Answers (
3
)
HttpContext.Current.Session Getting Null
HttpContext.Current.Session Getting Null