Finding prime factors

前端 未结 12 474
北海茫月
北海茫月 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:57

    This is one of the easiest and simple-to-understand solutions of your question. It might not be efficient like other solutions provided above but yes for those who are the beginner like me.

    int main() {
    
    int num = 0;
    cout <<"Enter number\n";
    cin >> num;
    int fac = 2;  
    while (num > 1) {
        if (num % fac == 0) {
            cout << fac<<endl;
            num=num / fac;
        }
        else fac++;
    }
    return 0;
    

    }

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

    Edit: I'm wrong (see comments). I would have deleted, but the way in which I'm wrong has helped indicate what specifically in the program takes so long to produce output, so I'll leave it :-)

    This program should immediately print 1 (I'm not going to enter a debate whether that's prime or not, it's just what your program does). So if you're seeing nothing then the problem isn't execution speed, there muse be some issue with the way you're running the program.

    0 讨论(0)
  • 2020-12-29 15:00
    # include <stdio.h>
    # include <math.h>
    void primeFactors(int n)
    {
        while (n%2 == 0)
        {
            printf("%d ", 2);
            n = n/2;
        }
        for (int i = 3; i <= sqrt(n); i = i+2)
        {
            while (n%i == 0)
            {
                printf("%d ", i);
                n = n/i;
            }
        }
       if (n > 2)
            printf ("%d ", n);
    }
    int main()
    {
        int n = 315;
        primeFactors(n);
        return 0;
    }
    
    0 讨论(0)
  • 2020-12-29 15:04

    Here is my code that worked pretty well to find the largest prime factor of any number:

    #include <iostream>
    using namespace std;
    
    // --> is_prime <--
    // Determines if the integer accepted is prime or not
    bool is_prime(int n){
        int i,count=0;
        if(n==1 || n==2)
          return true;
        if(n%2==0)
          return false;
        for(i=1;i<=n;i++){
        if(n%i==0)
            count++;
        }
        if(count==2)
          return true;
        else
          return false;
     }
     // --> nextPrime <--
     // Finds and returns the next prime number
     int nextPrime(int prime){
         bool a = false;
         while (a == false){
             prime++;
             if (is_prime(prime))
                a = true;
         }
      return prime;
     }
     // ----- M A I N ------
     int main(){
    
          int value = 13195;
          int prime = 2;
          bool done = false;
    
          while (done == false){
              if (value%prime == 0){
                 value = value/prime;
                 if (is_prime(value)){
                     done = true;
                 }
              } else {
                 prime = nextPrime(prime);
              }
          }
            cout << "Largest prime factor: " << value << endl;
     }
    

    Keep in mind that if you want to find the largest prime factor of extremely large number, you have to use 'long' variable type instead of 'int' and tweak the algorithm to process faster.

    0 讨论(0)
  • 2020-12-29 15:06

    600851475143 is outside of the range of an int

    void whosprime(int x) //<-----fix heere ok?
    {
        bool imPrime = true;
    
        for(int i = 1; i <= x; i++)
        {... 
          ...
    
    0 讨论(0)
  • 2020-12-29 15:10

    Try this code. Absolutely it's the best and the most efficient:

    long long number;
    bool isRepetitive;
    
    for (int i = 2; i <= number; i++) {
        isRepetitive = false;
        while (number % i == 0) {
            if(!isRepetitive){
                cout << i << endl;
                isRepetitive = true;
            }
            number /= i;
        }
    }
    

    Enjoy! ☻

    0 讨论(0)
提交回复
热议问题