Thread: infinite loop

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    7

    infinite loop

    hi guys
    so i developed this piece of code to help me find a co prime of a number.
    basically it just a fancy way of finding a number between 1 and itself, that is a prime number and not a factor of that number
    here is the code
    any help is appreciated
    Code:
    int finde(int phi){
       int e =4;
       int resultprime; 
       resultprime = IsPrime(e);
       while(e<phi){
        resultprime=IsPrime(e);
        if(resultprime==1){
            if((e%phi)!=0){
                return e; 
                break;
            }
        e++; 
        }
      }
    }
    thanks in advance =)

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I believe people argue that 1 isn't actually a prime, so you should be stopping at 2 really. We can't actually see IsPrime here, so who knows what it is doing. You don't need a break there, since you have just returned right in front of that. You only change e if resultprime is 1. Terrible indentation habits will get you every time.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Hi Quzah
    Thanks for the help
    Basically the IsPrime Function returns a 1 if its a prime and a zero if it is not ?
    Hope that helps you help me =)
    thanks

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I already helped you. Here, this is what it looks like with quality indentation:
    Code:
    int finde(int phi){
        int e =4;
        int resultprime;
        resultprime = IsPrime(e);
        while(e<phi)
        {
            resultprime=IsPrime(e);
            if(resultprime==1)
            {
                if((e%phi)!=0)
                {
                    return e;
                    break;
                }
                e++;
           }
        }
    }

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    May 2011
    Location
    Around 8.3 light-minutes from the Sun
    Posts
    1,949
    As quzah just said, you only increment e if e is prime. Since e starts at 4, which is not prime; you never increment e. Hence you have your infinite loop.
    Quote Originally Posted by anduril462 View Post
    Now, please, for the love of all things good and holy, think about what you're doing! Don't just run around willy-nilly, coding like a drunk two-year-old....
    Quote Originally Posted by quzah View Post
    ..... Just don't be surprised when I say you aren't using standard C anymore, and as such,are off in your own little universe that I will completely disregard.
    Warning: Some or all of my posted code may be non-standard and as such should not be used and in no case looked at.

  6. #6
    Registered User
    Join Date
    Sep 2011
    Posts
    7
    Ok so how does one go about fixing this, i'm completely at loss on what to do

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    What part of these answers are confusing:
    Quote Originally Posted by AndrewHunter View Post
    As quzah just said, you only increment e if e is prime. Since e starts at 4, which is not prime; you never increment e. Hence you have your infinite loop.
    Quote Originally Posted by quzah View Post
    You only change e if resultprime is 1.

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. infinite loop
    By leahknowles in forum C Programming
    Replies: 2
    Last Post: 03-31-2011, 03:40 PM
  2. Can you tell me why this causes an infinite loop?
    By User Name: in forum C Programming
    Replies: 23
    Last Post: 07-10-2010, 12:21 AM
  3. stays in loop, but it's not an infinite loop (C++)
    By Berticus in forum C++ Programming
    Replies: 8
    Last Post: 07-19-2005, 11:17 AM
  4. infinite loop
    By puckparches in forum C++ Programming
    Replies: 4
    Last Post: 05-18-2005, 10:12 PM
  5. Why does cin>> go into an infinite loop
    By Panopticon in forum C++ Programming
    Replies: 2
    Last Post: 02-03-2003, 12:48 AM