Categories > Coding > C# >

Primes Calculation Optimization || C# Only!

Posts: 1

Threads: 1

Joined: Oct, 2023

Reputation: 0

Posted

Heres my code and time try making a faster one and ill try to beat it and remember c# only!

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Primesv1
{
    internal class Program
    {
        static void Main()
        {
            const int limit = 1000000;
            const int repetitions = 100;
            long totalTicks = 0;

            for (int i = 0; i < repetitions; i++)
            {
                Stopwatch sw = new Stopwatch();
                sw.Start();

                int primeCount = CountPrimes(limit);

                sw.Stop();
                totalTicks += sw.ElapsedTicks;

                Console.WriteLine($"Iteration {i + 1}: Found {primeCount} prime numbers in {sw.ElapsedMilliseconds} ms");
            }

            double averageTimeMs = (double)totalTicks / Stopwatch.Frequency / repetitions * 1000;
            Console.WriteLine($"\nAverage time for {repetitions} iterations: {averageTimeMs} ms");
        }

        static int CountPrimes(int n)
        {
            bool[] isPrime = new bool[n + 1];
            for (int i = 2; i <= n; i++)
            {
                isPrime[i] = true;
            }

            for (int p = 2; p * p <= n; p++)
            {
                if (isPrime[p])
                {
                    for (int i = p * p; i <= n; i += p)
                    {
                        isPrime[i] = false;
                    }
                }
            }

            int count = 0;
            for (int i = 2; i <= n; i++)
            {
                if (isPrime[i])
                {
                    count++;
                }
            }

            return count;
        }
    }
}

https://cdn.discordapp.com/attachments/1161846372551622688/1162389570554433606/image.png?ex=653bc2a5&is=65294da5&hm=b99cda85c974b0f84c673e7e2651213b9dfe1715b364cc3a8457039552d0fd68&

Comments

ItsNitro 4 Reputation

Commented

Its been optimized a little https://i.imgur.com/4SekY8P.png

  • 0

PrimerWHopper 0 Reputation

Commented

@ItsNito ,It seems your "Unfammiliar" with maths as someone who is indian we know BY HEART THAT 1 MILLION DOES NOT CONTAIN 943745 PRIME NUMBERS

  • 0

ItsNitro 4 Reputation

Commented

@PrimerWHopper if you understood seives algorithm used for the program The first program utilizes the Sieve of Eratosthenes algorithm, which is efficient for smaller values of n. On the other hand, my program uses a segmented sieve, which is more suitable for larger values of n and performs better

  • 0

Added

if you understood what you were talking about then you would know

  • 0

  • 0

Posts: 522

Threads: 41

Joined: May, 2020

Reputation: 4

Replied

Slow as hell 

https://i.imgur.com/5vjjWyO.png

  • 0

nltr | Xaml & C# Developer Former Fluxus Administrator
------------------------------------------------------------------------------------
💜 Developer of Kronos ðŸ’œ

Senior Dev of Orbit

Discord

Zayn

.zayn0 (Previously B00M)

vip

Posts: 306

Threads: 16

Joined: Jul, 2022

Reputation: 10

Replied

hi ishan /charsss

  • 0

Added

I already did this challenge last time ishan was on here and was some fast a** score

Comments

PrimerWHopper 0 Reputation

Commented

csharp only try beat me

  • 0

  • 0

#Road to 15 Rep

https://media.discordapp.net/attachments/1081737726048608379/1088936664941989979/WRDBanner3.png

PC Specs: RTX 4080 OC (16GB), 32 GB 3200MHz, i7-11700k

Moonzyss_

i make funny lua code

Posts: 84

Threads: 5

Joined: Dec, 2022

Reputation: 4

Replied

hi ishannnnnnn

/charsssssssssss

  • 0

https://cdn.discordapp.com/attachments/1147631284772937819/1150957497524428890/Tnwc9wP.png

Posts: 3

Threads: 0

Joined: Oct, 2023

Reputation: 0

Replied

@Zayn @Alawrpar @ItsNitro 

using System;
using System.Diagnostics;

class GroovyApp_WAKE_UP
{
    static void Main(string[] args)
    {
        int groovyRange_WAKE_UP = 1000000;
        int numGroovyTests_WAKE_UP = 1000;

        double groovyTotalTime_WAKE_UP = 0;

        for (int i = 0; i < numGroovyTests_WAKE_UP; i++)
        {
            int groovyPrimeCount_WAKE_UP = 0;

            Stopwatch groovyStopwatch_WAKE_UP = new Stopwatch();
            groovyStopwatch_WAKE_UP.Start();

            groovyPrimeCount_WAKE_UP = GroovySieveOfEratosthenes_WAKE_UP(groovyRange_WAKE_UP);

            groovyStopwatch_WAKE_UP.Stop();
            long groovyElapsedTicks_WAKE_UP = groovyStopwatch_WAKE_UP.ElapsedTicks;
            double groovyElapsedMilliseconds_WAKE_UP = (double)groovyElapsedTicks_WAKE_UP / Stopwatch.Frequency * 1000;

            Console.WriteLine($"Test {i + 1}: Found {groovyPrimeCount_WAKE_UP} groovy prime numbers in {groovyElapsedMilliseconds_WAKE_UP} ms");
            groovyTotalTime_WAKE_UP += groovyElapsedMilliseconds_WAKE_UP;
        }

        double groovyAverageTime_WAKE_UP = groovyTotalTime_WAKE_UP / numGroovyTests_WAKE_UP;
        Console.WriteLine($"Average time for {numGroovyTests_WAKE_UP} groovy tests: {groovyAverageTime_WAKE_UP} ms");
        Console.ReadLine();
    }

    static unsafe int GroovySieveOfEratosthenes_WAKE_UP(int groovyRange_WAKE_UP)
    {
        int groovyPrimeCount_WAKE_UP = 0;
        int groovySieveSize_WAKE_UP = (groovyRange_WAKE_UP - 1) / 2;
        int groovyLimit_WAKE_UP = ((int)Math.Sqrt(groovyRange_WAKE_UP) - 1) / 2;
        bool* groovySieve_WAKE_UP = stackalloc bool[groovySieveSize_WAKE_UP];

        for (int i = 0; i < groovySieveSize_WAKE_UP; i++)
        {
            *(groovySieve_WAKE_UP + i) = false;
        }

        for (int i = 0; i <= groovyLimit_WAKE_UP; i++)
        {
            if (!*(groovySieve_WAKE_UP + i))
            {
                int groovyStep_WAKE_UP = 2 * i + 3;
                for (int j = 2 * i * (i + 3) + 3; j < groovySieveSize_WAKE_UP; j += groovyStep_WAKE_UP)
                {
                    *(groovySieve_WAKE_UP + j) = true;
                }
            }
        }

        for (int i = 0; i < groovySieveSize_WAKE_UP; i++)
        {
            if (!*(groovySieve_WAKE_UP + i))
            {
                groovyPrimeCount_WAKE_UP++;
            }
        }

        if (groovyRange_WAKE_UP >= 2)
        {
            groovyPrimeCount_WAKE_UP++;
        }

        return groovyPrimeCount_WAKE_UP;
    }
}



https://cdn.discordapp.com/attachments/1155154884174299216/1163193585919856822/image.png?ex=653eaf71&is=652c3a71&hm=66369e02d620c834182e5bdad9c417d75d948616a53728e57b2983ea578a5bb9&

Comments

ItsNitro 4 Reputation

Commented

Slow https://i.imgur.com/Mo5kJMe.png

  • 0

  • 0

Posts: 26

Threads: 6

Joined: Jul, 2023

Reputation: 2

Replied

why none of you use Parallel.For?

  • 0

Users viewing this thread:

( Members: 0, Guests: 1, Total: 1 )