# infinite loop

Page 1 of 1

## 6 Replies - 751 Views - Last Post: 15 August 2009 - 11:39 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=120366&amp;s=12d517fd6f0abc455621dec1a6aa8abe&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 daliash

• D.I.C Head

Reputation: 0
• Posts: 51
• Joined: 30-November 08

# infinite loop

Posted 15 August 2009 - 08:17 AM

why this code inter in infinite loop when the input is

19000 100000

* this program accept two integers and output the number of primes number between them ( inclusive the begin and end number ) and also show the number of primes number which be able to write as the result of 2 squsred number p = c^2 + b^2 , the program stopped when inter -1 -1 as input

```# include  <iostream>
# include < math.h>
using namespace std;
bool func( int num)
{
bool square= false;
int sum1=-1, sum2=-1;
float number = num;
float temp1;
int temp2;

while ( number !=1)
{
temp1=sqrt(number);
temp2= (int)temp1;

if (temp1-temp2==0)
{
sum1=sqrt(number);
number =num-(sum1*sum1);

temp1=sqrt(number);
temp2= (int)temp1;
if (temp1-temp2==0)
{
sum2=sqrt(number);
square= true;
break;

}
number= sum1*sum1;
}

number--;

}
return square;

}
bool IsPrime(int num)
{
bool prime = true;
if ( num<= 1)
return false;
for ( int i=2; i<num;i++)
if ( num%i== 0  )
prime =false;

return prime;
}

int main()
{

int first_Num, last_Num,Total_primes=0,NUm_primes_has_squares=0;
bool change = false;
cin >> first_Num >> last_Num;
while ( first_Num!=-1 || last_Num!=-1)
{
if ( first_Num ==last_Num)
{ change = true;
last_Num++;
}
for ( int i=first_Num; i<=last_Num;i++)
{
if ( IsPrime(i))
{
Total_primes++;
if  ( func(i))
NUm_primes_has_squares++;
}

}
if ( change)
last_Num--;
cout << first_Num << " " << last_Num<<" "<<Total_primes <<" " <<NUm_primes_has_squares<<endl;

cin >> first_Num >> last_Num;
Total_primes=0;
NUm_primes_has_squares=0;

}

return  0;
}

```

This post has been edited by daliash: 15 August 2009 - 08:18 AM

Is This A Good Question/Topic? 0

## Replies To: infinite loop

### #2 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: infinite loop

Posted 15 August 2009 - 08:24 AM

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers
Was This Post Helpful? 0

### #3 deery5000

• D.I.C Lover

Reputation: 87
• Posts: 1,097
• Joined: 09-May 09

## Re: infinite loop

Posted 15 August 2009 - 08:40 AM

to create a never ending loop just use

```while(1)
{

}

```

to come out of the loop if you meet a condition use the keyword
break;

Hope this helps
Kevin
Was This Post Helpful? 0

### #4 daliash

• D.I.C Head

Reputation: 0
• Posts: 51
• Joined: 30-November 08

## Re: infinite loop

Posted 15 August 2009 - 08:41 AM

Dogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers

thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd

Thanks in advance
Was This Post Helpful? 0

### #5 Dogstopper

• The Ninjaducky

Reputation: 2965
• Posts: 11,222
• Joined: 15-July 08

## Re: infinite loop

Posted 15 August 2009 - 08:45 AM

daliash, on 15 Aug, 2009 - 07:41 AM, said:

Dogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers

thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd

Thanks in advance

I think that this is due to the fact that int datatypes are too small to hold the product of these numbers. try using a long datatype instead.

```long sum1, sum2;

```

Was This Post Helpful? 0

### #6 daliash

• D.I.C Head

Reputation: 0
• Posts: 51
• Joined: 30-November 08

## Re: infinite loop

Posted 15 August 2009 - 09:33 AM

Dogstopper, on 15 Aug, 2009 - 07:45 AM, said:

daliash, on 15 Aug, 2009 - 07:41 AM, said:

Dogstopper, on 15 Aug, 2009 - 07:24 AM, said:

I tried it, and it is not infinite. If you enter 2 negative numbers, the process terminates.

Cheers

thanks a lot for reply

try to inter 19000 100000 as a input there is no output appear ( it log in infinite loop )

I do not know where this loop and when was enterd

Thanks in advance

I think that this is due to the fact that int datatypes are too small to hold the product of these numbers. try using a long datatype instead.

```long sum1, sum2;

```

I correct it as u say , but
it still not appear the output
Was This Post Helpful? 0

### #7 NickDMax

• Can grep dead trees!

Reputation: 2255
• Posts: 9,245
• Joined: 18-February 07

## Re: infinite loop

Posted 15 August 2009 - 11:39 AM

well this is a good time to learn a little bit about debugging. You might want to add some print statements into the program in a few places to figure out what the values are. You should be able to extract what is happening.
Was This Post Helpful? 0

Page 1 of 1

 .related ul { list-style-type: circle; font-size: 12px; font-weight: bold; } .related li { margin-bottom: 5px; background-position: left 7px !important; margin-left: -35px; } .related h2 { font-size: 18px; font-weight: bold; } .related a { color: blue; }