What is the best way to find the largest simple number coefficient?

I am new to programming, and my friend suggested I do an exercise on the Euler project to get better. I ran into issue 3 issue:

"The main factors of 13195 are 5, 7, 13, and 29. What is the largest prime coefficient of 600851475143?"

Now here is my solution:

class Program
{
    static void Main(string[] args)
    {
        long number = 600851475143;
        bool prime = true;

        for (long i = 3; i <= number; i++)
        {
            for (long n = 2; n < i; n++)
            {

                if (i % n == 0)
                {
                    prime = false;
                    break;
                }
            }

            if (prime)
            {
                if (number % i == 0)
                {
                    Console.WriteLine(i);

                }

            }
            prime = true;

        }
        Console.ReadKey();
    }
}

Now that I got the correct answer (this is 6857), I found that my method is very inefficient. If you run my code, you will see that it will still work after more than two minutes ... My question is, how can I write a more efficient / faster code for this?

+4
source share
4

, / ?

. , .

:

  • ?
  • ?

, . , .

, : , " "? ​​:

class Program
{
  static bool IsPrime(long i)
  {
    for (long n = 2; n < i; n++)
    {
      if (i % n == 0)
        return false;
    }
    return true;
  }

  static void Main(string[] args)
  {
    long number = 600851475143;
    for (long i = 3; i <= number; i++)
    {
      if (IsPrime(i))
        Console.WriteLine(i);
    }
    Console.ReadKey();
  }
}

, . . IsPrime? , . ? 3 .

. ? IsPrime true 1, . . , . , , . ! , , , , .

, , . , IsPrime? :

  • i. (, n * n <= i , n <= Sqrt(i))
  • 2, 4, 6, 8,...

?

. , , .

+5

, , , , (order (n2)), , , .

, , , , n.

√n, , n p, n = p × q, p ≤ q, , p ≤ √n. , n √n.

, , " ".

, , . (https://en.wikipedia.org/wiki/Sieve_of_Eratosthenes)

, .

   public int primes(int n) {
     boolean[] isPrime = new boolean[n];
     for (int i = 2; i < n; i++) {
        isPrime[i] = true;
     }
     // Loop ending condition is i * i < n instead of i < sqrt(n)
     // to avoid repeatedly calling an expensive function sqrt().
     for (int i = 2; i * i < n; i++) {
       if (!isPrime[i]) continue;
       for (int j = i * i; j < n; j += i) {
         isPrime[j] = false;
       }
     }
     int count = 0;
     for (int i = 2; i < n; i++) {
       if (isPrime[i]) count++;
     }

    //return the max prime
    int maxPrime = 1;
    for(int i = 0; i < isPrime.count; i++){
      if(isPrime[i]){
        maxPrime = i;
      }
      return maxPrime;
    }
  }
+3

, . , :

public void PrimeFactor1(long n)
{
    List<int> sList = new List<int>();
    long temp = 1;
    for (int i = 2; i <= n; i++)
    {
        if ((n % i) == 0)
        {
            temp = n / i;
            sList.Add(i);
            i = 1;
            n = temp;
        }     
    }
    string arr = string.Join(",", sList.ToArray());
    Console.Write(arr);
    Console.WriteLine(".");
    Console.WriteLine("The Biggest Prime number is: {0}", sList.Max());
}
+1
source

If you intend to work on many of the problems of Project Euler, you will need a good implementation of the Eratosthenes sieve. Two of the useful methods for your Eratosthenes class are: nextPrime(int p)and previousPrime(int p), which respectively return the next and next lower primes.

ETA: The pseudo code has been edited as invalid. Unfortunately.

0
source

Source: https://habr.com/ru/post/1629468/


All Articles