Visualizzazione dei risultati da 1 a 5 su 5
  1. #1
    Utente di HTML.it
    Registrato dal
    May 2008
    Messaggi
    475

    [C++] Scomposizione in fattori primi troppo lenta

    Ciao a tutti, stavo provando il problema 583 di UVA Online Judge, e penso di esserci riuscito.

    Le specifiche dicono che viene passato un file che contiene su ogni linea un numero compreso tra -2^31 e +2^31, e chiede di stamparne la fattorizzazione. L'ultima riga è uno 0 che chiude il file.

    In ogni mio test funziona perfettamente. Però il problema non viene accettato per "Time Limit Exceeded". Il tempo limite è di 3 secondi. L'algoritmo pare funzionare, però evidentemente vengono passati dei numeri troppo grandi e il mio algoritmo è troppo lento.

    Purtroppo non posso sapere che file (e quindi quanti e quali numeri) vengono passati al programma...

    codice:
    #include <iostream>
    #include <math.h>
    
    using namespace std;
    
    //Checks wether a number is prime
    bool IsPrime(long n)
    {
        //Sets the maximum factor to be tested
        //as the square root of the number
        long limit = (long)(sqrt(n) + 1);
    
        for (int i = 2; i < limit; i++)
            if ((n % i) == 0) return false;
    
        return true;
    }
    
    void PrintFactors(long n)
    {
        cout << n << " = ";
        bool alreadyPrinted = false;
    
        //Prints -1 and makes the number positive
        if (n < 0)
        {
            cout << "-1";
            alreadyPrinted = true;
            n *= -1;
        }
    
        long factor = 2;
    
        //Repeat while the number
        //can still be divided
        while (n > 1)
        {
            //Repeats while the number can be divided
            //by the current factor
            while ((n % factor) == 0)
            {
                if (alreadyPrinted) cout << " x ";
                cout << factor;
                alreadyPrinted = true;
    
                n /= factor;
            }
    
            //Finds next prime factor
            do
            {
                factor++;
            }
            while (!IsPrime(factor));
        }
    
        cout << "\n";
    }
    
    int main()
    {
        long readNumber = 0;
    
        cin >> readNumber;
    
        while (readNumber)
        {
            PrintFactors(readNumber);
            cin >> readNumber;
        }
    
        return 0;
    }
    "Let him who has understanding reckon the number of the beast, for it is a human number.
    Its number is rw-rw-rw-."

  2. #2
    L'algoritmo di ricerca dei numeri primi mi pare un po' tanto forza bruta...
    Amaro C++, il gusto pieno dell'undefined behavior.

  3. #3
    Utente bannato
    Registrato dal
    Feb 2004
    Messaggi
    2,803
    [ot] non ho capito che vuol dire limite 3 secondi
    puoi spiegarmi in due parole in che consistono questi problemi? l'inglese lo spicco poco

  4. #4
    Utente di HTML.it
    Registrato dal
    May 2008
    Messaggi
    475
    TUTTO è forza bruta xD

    A quale parte ti riferivi?

    Questa?
    codice:
    bool IsPrime(long n)
    {
        //Sets the maximum factor to be tested
        //as the square root of the number
        long limit = (long)(sqrt(n) + 1);
    
        for (int i = 2; i < limit; i++)
            if ((n % i) == 0) return false;
    
        return true;
    }
    O questa?
    codice:
    //Finds next prime factor
            do
            {
                factor++;
            }
            while (!IsPrime(factor));
    "Let him who has understanding reckon the number of the beast, for it is a human number.
    Its number is rw-rw-rw-."

  5. #5
    Entrambe. Esistono metodi molto più efficienti per cercare numeri primi; inoltre invece di ricercarli ogni volta per ogni numero dovresti farti una tabella di numeri primi, e aggiungere di volta in volta solo quelli che non hai ancora trovato.
    Amaro C++, il gusto pieno dell'undefined behavior.

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.