Results 1 to 4 of 4
  1. #1
    abv
    abv is offline Member
    Join Date
    Aug 2012
    Posts
    4
    Rep Power
    0

    Default Why doesn't this code work?

    it's supposed to find the largest prime factor of a:

    long a = 55;
    long max = 0;
    int m = 0;
    for(long i = 3; i < a; i++){
    for(long k = 2; k < i; k++){
    if(i%k == 0){
    m = 1;
    }
    }
    if(m==0){
    if(a%i==0){
    if(i > max){
    max = i;
    }
    }
    }
    }

  2. #2
    awinston is offline Student
    Join Date
    Jul 2012
    Location
    United States
    Posts
    328
    Rep Power
    3

    Default Re: Why doesn't this code work?

    Perhaps a better question: why should it work?
    "Success is not final, failure is not fatal: it is the courage to continue that counts." - Winston Churchill

  3. #3
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,784
    Rep Power
    7

    Default Re: Why doesn't this code work?

    When posting code wrap it in code tags to preserve indenting.

    What happens if m == 1?
    If m does equal 1 should it be reset for the next loop iteration?
    If you objective is to find the LARGEST prime factor then why do you start at the SMALLEST?

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,193
    Rep Power
    19

    Default Re: Why doesn't this code work?

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. My code doesn't work! Pane.showOptionDialog()
    By 0070071 in forum AWT / Swing
    Replies: 5
    Last Post: 01-18-2012, 05:56 AM
  2. Why doesn't this work?
    By mailman in forum Java Applets
    Replies: 5
    Last Post: 01-10-2012, 01:01 PM
  3. why this doesn't work?
    By hitesh_public in forum New To Java
    Replies: 5
    Last Post: 08-09-2010, 08:07 AM
  4. Repaint calling Statement Doesn't Work in my code ?
    By nitin_daviet88 in forum CLDC and MIDP
    Replies: 2
    Last Post: 07-24-2010, 03:09 PM
  5. This code doesn't work. Why? thanks.
    By seanzhang in forum New To Java
    Replies: 10
    Last Post: 07-09-2010, 05:53 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •