Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chapter 3. Prime Factorization. Complexity. #16

Open
unclelesha opened this issue Dec 12, 2019 · 0 comments
Open

Chapter 3. Prime Factorization. Complexity. #16

unclelesha opened this issue Dec 12, 2019 · 0 comments

Comments

@unclelesha
Copy link

unclelesha commented Dec 12, 2019

function primeFactors(n){
    // Print the number of 2s that divide n
    while (n % 2 == 0) {
        console.log(2);
        n = n/2;
    }

    // n must be odd at this point. So we can skip one element
    // (Note i = i +2)
    for (var i = 3; i * i <= n; i = i + 2) {
        // While i divides n, print i and divide n
        while (n % i == 0) {
            console.log(i);
            n = n / i;
        }
    }
    // This condition is to handle the case when n is a prime number
    // greater than 2
    if (n > 2) {
        console.log(n);
    }
}

This function has Big-O =
image
In your book you put it sqrt(x) only, but inside of the for loop there is another loop while, that has Big-O = log (x). Am I correct?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant