Finding prime factors

前端 未结 12 473
北海茫月
北海茫月 2020-12-29 14:24
#include 
using namespace std;

void whosprime(long long x)
{
    bool imPrime = true;

    for(int i = 1; i <= x; i++)
    {
        for(int z =          


        
相关标签:
12条回答
  • 2020-12-29 14:43

    Simple way :

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long int ll;
    
    ll largeFactor(ll n)
    {
            ll ma=0;
            for(ll i=2; i*i<=n; i++)
            {
                while(n%i == 0)
                {
                    n=n/i;
                    ma=i;
                }
            }
            ma = max(ma, n);
            return ma;
    }
    
    int main() 
    {
        ll n;
        cin>>n;
        cout<<largeFactor(n)<<endl;
        return 0;
    }
    

    Implementation using prime sieve ideone.

    0 讨论(0)
  • 2020-12-29 14:44

    A C++ implementation using @user448810's pseudocode:

    #include <iostream>
    using namespace std;
    
    void factors(long long n) {
        long long z = 2;
        while (z * z <= n) {
            if (n % z == 0) {
                cout << z << endl;
                n /= z;
            } else {
                z++;
            }
        }
        if (n > 1) {
            cout << n << endl;
        }
    }
    
    int main(int argc, char *argv[]) {
        long long r = atoll(argv[1]);
        factors(r);
    }
    
    // g++ factors.cpp -o factors ; factors 600851475143
    

    Perl implementation with the same algorithm is below.
    Runs ~10-15x slower (Perl 0.01 seconds for n=600851475143)

    #!/usr/bin/perl
    use warnings;
    use strict;
    
    sub factors {
        my $n = shift;
        my $z = 2;
        while ($z * $z <= $n) {
            if ( $n % $z ) {
                $z++;
            } else {
                print "$z\n";
                $n /= $z;
            }
        }
        if ( $n > 1 ) {
            print "$n\n"
        }
    }
    
    factors(shift);
    
    # factors 600851475143
    
    0 讨论(0)
  • 2020-12-29 14:44

    Since 600851475143 is out of scope for int as well as single long type wont work here hence here to solve we have to define our own type here with the help of typedef. Now the range of ll is some what around 9,223,372,036,854,775,807.

    typedef long long int LL

    0 讨论(0)
  • 2020-12-29 14:48

    Try below code:

    counter = sqrt(n)
    i = 2;
    
    while (i <= counter)
        if (n % i == 0)
            output i
        else
            i++
    
    0 讨论(0)
  • 2020-12-29 14:51

    Your algorithm is wrong; you don't need i. Here's pseudocode for integer factorization by trial division:

    define factors(n)
    
        z = 2
    
        while (z * z <= n)
    
            if (n % z == 0)
                output z
                n /= z
    
            else
                z++
    
        if n > 1
            output n
    

    I'll leave it to you to translate to C++ with the appropriate integer datatypes.

    Edit: Fixed comparison (thanks, Harold) and added discussion for Bob John:

    The easiest way to understand this is by an example. Consider the factorization of n = 13195. Initially z = 2, but dividing 13195 by 2 leaves a remainder of 1, so the else clause sets z = 3 and we loop. Now n is not divisible by 3, or by 4, but when z = 5 the remainder when dividing 13195 by 5 is zero, so output 5 and divide 13195 by 5 so n = 2639 and z = 5 is unchanged. Now the new n = 2639 is not divisible by 5 or 6, but is divisible by 7, so output 7 and set n = 2639 / 7 = 377. Now we continue with z = 7, and that leaves a remainder, as does division by 8, and 9, and 10, and 11, and 12, but 377 / 13 = 29 with no remainder, so output 13 and set n = 29. At this point z = 13, and z * z = 169, which is larger than 29, so 29 is prime and is the final factor of 13195, so output 29. The complete factorization is 5 * 7 * 13 * 29 = 13195.

    There are better algorithms for factoring integers using trial division, and even more powerful algorithms for factoring integers that use techniques other than trial division, but the algorithm shown above will get you started, and is sufficient for Project Euler #3. When you're ready for more, look here.

    0 讨论(0)
  • 2020-12-29 14:52

    short and clear vesion:

        int main()
        {
            int MAX = 13195;
    
            for (int i = 2; i <= MAX; i++)
            {
                 while (MAX % i == 0)
                 {
                      MAX /= i;
                      cout <<  i << ", " << flush;   // display only prime factors
                 }
            return 0;
        }
    
    0 讨论(0)
提交回复
热议问题