Thread: biggest prime factor

  1. #16
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    There can't be a factor larger than the number divided by 2
    What if the number is a prime number - 997, 911, 821, ect...

  2. #17
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Click_here View Post
    I suppose that it would be faster if you only tried primes, but you'd need to have all of them in an array up to (and including) the value of the number you were testing (consider 997, 911, 821, ect...). This isn't impossible, because you have an upper limit to the number you can test - It's just VERY impractical.

    Another thing that I found was that the last prime number found always seemed to be the largest - Can anyone confirm my observation?
    There are many ways of finding primes (or factors of a number, if you want to see it that way) that do not involve storing all of them in an implementation, like wheel factorization. All the methods I know depend on finding smaller primes first though, because you can use those to speed up the search for bigger primes.

    Quote Originally Posted by Click_here View Post
    What if the number is a prime number - 997, 911, 821, ect...
    That doesn't help you do anything. 1 and the number are trivial factors.

  3. #18
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    http://im.cprogramming.com/images/misc/quote_icon.png Originally Posted by Click_herehttp://im.cprogramming.com/images/bu...post-right.pngWhat if the number is a prime number - 997, 911, 821, ect...



    That doesn't help you do anything. 1 and the number are trivial factors.
    I'm not sure what you are saying - Are you saying that you shouldn't consider the actual number as a possibility of being the highest prime factor?

  4. #19
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Yes, but I'm not really sure what I'm saying anymore. There are a lot of primes. I'm just pointing out that he might be right. While you're insistent that N might be the highest prime factor, he is also right that 2 is the least possible prime factor. So if you want to be clever you can eliminate at least 1 + N/2 pointless tests. No matter what you are doing I think.

    Quote Originally Posted by wiki on trial division
    A definite bound on the prime factors is possible. Suppose P(i) is the ith prime, so that P(1) = 2, P(2) = 3, P(3) = 5, etc. Then the last prime number worth testing as a possible factor of n is P(i) where P(i + 1)^2 > n; equality here would mean that P(i + 1) is a factor. Thus, testing with 2, 3, and 5 suffices up to n = 48 not just 25 because the square of the next prime is 49, and below n = 25 just 2 and 3 are sufficient. Should the square root of n be integral, then it is a factor and n is a perfect square.
    Last edited by whiteflags; 08-10-2012 at 05:05 AM.

  5. #20
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by Adak View Post
    Yes. That was mis-stated. There are no prime *factors* of a number, greater than the square root of the number - but that's not what you're doing here. Sorry for the confusion
    You're still offering confusion, and I'll offer a counter example. The prime factors of 10 are 2 and 5. The square root of 10 is about 3.16, as noted by laserlight. Five is generally considered to be greater than 3.16.


    The rule you're thinking of is that, if there is no prime factor less than the square root, then there is also no prime factor exceeding the square root.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  6. #21
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Quote Originally Posted by grumpy View Post
    You're still offering confusion, and I'll offer a counter example. The prime factors of 10 are 2 and 5. The square root of 10 is about 3.16, as noted by laserlight. Five is generally considered to be greater than 3.16.


    The rule you're thinking of is that, if there is no prime factor less than the square root, then there is also no prime factor exceeding the square root.
    Yes, absolutely. That is not what the OP is trying to find however.

    I've definitely joined your "3%" on this thread.

    I'm not sure what you are saying - Are you saying that you shouldn't consider the actual number as a possibility of being the highest prime factor?
    I believe what is meant is that 1 and the number itself, will always be a factor for any number. One can't be a prime (by definition), and the fact that the number can be a factor for itself, proves nothing, since that is a truism for any number.
    Last edited by Adak; 08-10-2012 at 06:26 AM.

  7. #22
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I'm just pointing out that he might be right.
    There can't be a factor larger than the number divided by 2
    What if the number is a prime number - 997, 911, 821, ect...
    What I was saying was that the statement that "there can't be any factor larger than the number divided by 2" was wrong. I then provided examples of numbers where that logic fails. Yes you are correct in saying that you can save a lot of time by skipping the numbers between N/2 and N, but that is not what they were saying.

  8. #23
    Registered User
    Join Date
    Aug 2012
    Posts
    9
    Wow. a lot of respond.
    I don't understand all of your respond, but it was an interesting read.
    I took your advices and start from the max instead and decremented it by 2.
    Here the new code:
    Code:
    #include <stdio.h>
    int main()
    {
        long long big, p, max=100;
        for(p=99; p<=max; p=p-2)
            if((p%3!=0)&&(p%5!=0)&&(p%7!=0)&&(max%p==0))
            {
            break;
            }
            printf("%d \n", p);
        return 0;
    }
    It give me result for small number, but then i saw something that i did wrong from the beginning.
    My prime factor test does not work.
    Code:
    (p%3!=0)&&(p%5!=0)&&(p%7!=0)
    For example: 143=11*13 so it is not a prime, however it would pass my test above.
    I decide to google way to find prime(mathematically, not coding). I found a few methods, which hurt my brain while reading it.

    I'm not going to ask you guys for a way to find prime(using programming) since i want to figure it out myself. However i think it going to be a while because i have a test to prep for.

    See you guys later, when i'm done or have more question.
    Thanks

  9. #24
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    I believe what is meant is that 1 and the number itself, will always be a factor for any number
    Yes... But not always a Prime Factor... Every number can be made up by multiplying only prime numbers together (it's "Prime Factors", not to be confused with "Factors") and we are trying to find the highest Prime Factor.

    Have a look at this Table of prime factors - Wikipedia, the free encyclopedia

    Notice that in some instances, the greatest Prime Factor is the number itself - That is what the confusion was about.

  10. #25
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    For example: 143=11*13 so it is not a prime, however it would pass my test above.
    I decide to google way to find prime(mathematically, not coding). I found a few methods, which hurt my brain while reading it.
    Googling a way to find primes mathematically may not really help. There are things like Fermat's little theorem, but that gives wrong answers sometimes.

    I wouldn't give up on what you're trying. What you are currently doing is a lot like wheel factorization, which is a kind of sieve for primes; it's just an incomplete implementation.

    After you fix the algorithm, here's something that may help: if you give a wheel a number it will find a divisor for you. For primes, it should be the same number you put in, for composites it should be the smallest nontrivial divisor. Keep the divisors for later use, and if you can divide the number to make the problem smaller, eventually you will know all of the divisors of the big number.

  11. #26
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    This is such an interesting problem - I'd love to see what people have come up with -

    Does anyone want to send me their code and I'll send you mine

  12. #27
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by Click_here
    Does anyone want to send me their code and I'll send you mine
    Deal

    Oh wait, I erased the example that I wrote, but no matter, I'll write it again
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #28
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Quote Originally Posted by Click_here View Post
    This is such an interesting problem - I'd love to see what people have come up with -

    Does anyone want to send me their code and I'll send you mine
    OK... no complaints if you don't have 64 bit integers though.

  14. #29
    Registered User
    Join Date
    Mar 2009
    Posts
    344
    Quote Originally Posted by robinnbastar View Post
    My prime factor test does not work.
    Code:
    (p%3!=0)&&(p%5!=0)&&(p%7!=0)
    For example: 143=11*13 so it is not a prime, however it would pass my test above.
    Yes, that's why you should be testing using a loop. First test 3 as a factor and add 2 each time through the loop. Exit the loop when you've passed the sqrt of p. That way if p is large enough you'll get to 11 eventually.

    What should you do inside the loop? That's been posted several times already - keep dividing p by that factor while it is a factor.

    Whatever value of p is left after you've exited the loop is the greatest prime factor of the number you started with.
    Last edited by KCfromNC; 08-13-2012 at 09:36 AM. Reason: fix quotes

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Largest Prime Factor
    By seanksg in forum C Programming
    Replies: 16
    Last Post: 12-02-2011, 09:12 AM
  2. Prime factor
    By alperen1994 in forum C Programming
    Replies: 6
    Last Post: 03-28-2009, 01:31 PM
  3. last prime factor
    By frango9000 in forum C Programming
    Replies: 7
    Last Post: 07-27-2006, 12:42 PM
  4. Prime factor
    By caroundw5h in forum C Programming
    Replies: 6
    Last Post: 01-13-2004, 03:39 PM
  5. Prime Factor Fxn
    By alpha in forum C++ Programming
    Replies: 2
    Last Post: 10-21-2003, 10:44 AM