Click here to Skip to main content
15,888,142 members
Home / Discussions / C#
   

C#

 
QuestionPath.Combine ignoring first parameter of three Pin
Member 1185433519-Feb-16 3:59
Member 1185433519-Feb-16 3:59 
AnswerRe: Path.Combine ignoring first parameter of three Pin
Richard MacCutchan19-Feb-16 4:07
mveRichard MacCutchan19-Feb-16 4:07 
GeneralRe: Path.Combine ignoring first parameter of three Pin
Member 1185433519-Feb-16 4:11
Member 1185433519-Feb-16 4:11 
QuestionC# Pin
Member 1116162517-Feb-16 23:39
Member 1116162517-Feb-16 23:39 
AnswerRe: C# Pin
Richard MacCutchan17-Feb-16 23:53
mveRichard MacCutchan17-Feb-16 23:53 
AnswerRe: C# Pin
Jochen Arndt17-Feb-16 23:56
professionalJochen Arndt17-Feb-16 23:56 
AnswerRe: C# Pin
OriginalGriff17-Feb-16 23:59
mveOriginalGriff17-Feb-16 23:59 
SuggestionRe: C# Pin
Richard Deeming18-Feb-16 2:45
mveRichard Deeming18-Feb-16 2:45 
Member 11161625 wrote:
for (int j = 2; j <= 100; j++)

No number is ever going to be evenly divisible by anything greater than itself, so you can eliminate those numbers from your loop:
C#
if (int j = 2; j < i; j++)

But we can do better than that.

If i is evenly divisible by a number greater than the square root of i, then it will also be evenly divisible by a corresponding number less than the square root of i. Therefore, we can ignore all numbers greater than the square root:
C#
int squareRoot = (int)Math.Floor(Math.Sqrt(i));
for (int j = 2; j < squareRoot; j++)

But we can still do better.

Currently, you're checking for divisibility by every number in the range. But if it's not divisible by 2, then it won't be divisible by any multiple of 2. Using the Sieve of Eratosthenes[^], you can eliminate all multiples of the prime numbers, leaving just the prime numbers:
C#
var notPrime = new HashSet<int>();
for (int i = 2; i <= 100; i++)
{
    if (!notPrime.Contains(i))
    {
        // i is a prime number:
        Console.WriteLine("\t{0}", i);
        
        // All multiples of i are NOT prime numbers:
        for (int j = i * 2; j <= 100; j += i)
        {
            notPrime.Add(j);
        }
    }
}

There are other even more efficient ways to find prime numbers, but they tend to be much more complicated, and would be overkill for a simple scenario like this.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: C# PinPopular
BillWoodruff18-Feb-16 8:54
professionalBillWoodruff18-Feb-16 8:54 
GeneralRe: C# Pin
Mycroft Holmes18-Feb-16 13:18
professionalMycroft Holmes18-Feb-16 13:18 
GeneralRe: C# Pin
BillWoodruff18-Feb-16 16:13
professionalBillWoodruff18-Feb-16 16:13 
GeneralRe: C# Pin
CHill6018-Feb-16 13:30
mveCHill6018-Feb-16 13:30 
QuestionEF dynamic CRUD Pin
jackie.398117-Feb-16 3:38
jackie.398117-Feb-16 3:38 
AnswerRe: EF dynamic CRUD Pin
Dave Kreskowiak17-Feb-16 4:33
mveDave Kreskowiak17-Feb-16 4:33 
GeneralRe: EF dynamic CRUD Pin
jackie.398117-Feb-16 4:49
jackie.398117-Feb-16 4:49 
GeneralRe: EF dynamic CRUD Pin
Dave Kreskowiak17-Feb-16 5:07
mveDave Kreskowiak17-Feb-16 5:07 
GeneralRe: EF dynamic CRUD Pin
jackie.398117-Feb-16 5:22
jackie.398117-Feb-16 5:22 
GeneralRe: EF dynamic CRUD Pin
Dave Kreskowiak17-Feb-16 7:03
mveDave Kreskowiak17-Feb-16 7:03 
GeneralRe: EF dynamic CRUD Pin
jackie.398117-Feb-16 9:03
jackie.398117-Feb-16 9:03 
AnswerRe: EF dynamic CRUD Pin
Sascha Lefèvre17-Feb-16 5:36
professionalSascha Lefèvre17-Feb-16 5:36 
GeneralRe: EF dynamic CRUD Pin
jackie.398117-Feb-16 6:51
jackie.398117-Feb-16 6:51 
AnswerRe: EF dynamic CRUD Pin
Gerry Schmitz17-Feb-16 17:51
mveGerry Schmitz17-Feb-16 17:51 
QuestionBoss just gave me ReSharper Ultimate Pin
Ian Klek16-Feb-16 22:28
Ian Klek16-Feb-16 22:28 
AnswerRe: Boss just gave me ReSharper Ultimate Pin
OriginalGriff16-Feb-16 23:17
mveOriginalGriff16-Feb-16 23:17 
AnswerRe: Boss just gave me ReSharper Ultimate Pin
Pete O'Hanlon16-Feb-16 23:31
mvePete O'Hanlon16-Feb-16 23:31 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.