Calvin Godfrey
Calvin Godfrey

Reputation: 2369

Why is there a difference between the running time of these two algorithms?

As a part of a larger problem, I had to find the sum of prime factors of an integer n. I wrote my own version, but my solution to the problem timed out. I looked online and found a different version of the same method, summing the prime factors of an integer, and when I changed nothing but that one method, the solution was fast enough. But I can't find any difference in the running time just based on looking at the code.

My (slower) version:

static int factors(int n) {
        int total = 0;
        while (n % 2 == 0) {
            total += 2;
            n /= 2;
        }
        int j = 3;
        while (n > 1) {
            while (n % j == 0) {
                total += j;
                n /= j;
            }
            j += 2;
        }
        return total;
    }

The (faster) version I found:

static int factors(int n) {
        int sum = 0;
        int i = 2;
        while (true) {
            if (n % i == 0) {
                n /= i;
                sum += i;
                if (isPrime(n)) {
                    sum += n;
                    break;
                }
                i = 1;
            }
            i++;
        }
        return sum;
    }

For the sake of brevity, I'm not including the isPrime method in the bottom one; it's just the basic brute force one looping to sqrt(n), not even Sieve of Eratosthenes.

If anything, I would've thought my version would be faster, because it gets all the duplicates of a prime factor out of the way, and also doesn't require repeatedly testing if a number is prime.

Upvotes: 1

Views: 95

Answers (3)

maaartinus
maaartinus

Reputation: 46492

The second method profits from early break, while your method keeps iterating until j reaches n. For big n, this takes ages....

Use a condition like while (n > 1 && n <= j * j), so you can break out at about sqrt(n) instead of n (I mean the original n given as parameter, rather than the current value).

When you exit due to the second part of the condition, don't forget that with n > 1 is the last factor.

Upvotes: 0

Francesco Pitzalis
Francesco Pitzalis

Reputation: 2112

I believe it depends on your input.

I believe that on paper the complexity of the 2 algorithms is the same: O(n log(n)) (assuming that isPrime(n) has a complexity of O(log(n)) - and please if someone better than me at calculating Big O can confirm or deny it would be great), so they should be pretty much equivalent, but as you know this analysis requires the assumption of the worst case.

In practice:

E.g. imagine your number n is 3^1000, your method will divide it by 3 some 1000 times, the second method will divide it by 3, then 9, then 27, etc, cutting down the number of operations required.

In general I would assume that if in n there is the same factor multiple times your method will be slower.

Upvotes: 1

Andrzej Jaromin
Andrzej Jaromin

Reputation: 269

Your algorithm has an error. You are always adding j += 2. But prime factors are increasing by two only to 13. Next one is 17 (not 15). The further you go - the average step between prime number is longer. In your algorithm you're always stepping by two.

Here are prime factors to 1000:

2 3 5 7 11 13 17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79 83 89 97 101 103 107 109 113 127 131 137 139 149 151 157 163 167 173 179 181 191 193 197 199 211 223 227 229 233 239 241 251 257 263 269 271 277 281 283 293 307 311 313 317 331 337 347 349 353 359 367 373 379 383 389 397 401 409 419 421 431 433 439 443 449 457 461 463 467 479 487 491 499 503 509 521 523 541 547 557 563 569 571 577 587 593 599 601 607 613 617 619 631 641 643 647 653 659 661 673 677 683 691 701 709 719 727 733 739 743 751 757 761 769 773 787 797 809 811 821 823 827 829 839 853 857 859 863 877 881 883 887 907 911 919 929 937 941 947 953 967 971 977 983 991 997 1009

In your algorithm you'll have 500 numbers (step by two).

So if you've a big number - you'll have much more iterations than the second algorithm.

Upvotes: 0

Related Questions