Having a problem with prime numbers

RedArmy

Platinum Member
Mar 1, 2005
2,648
0
0
I'm fooling around with the 3n+1 problem and I wanted to see if I could find how many prime numbers were in any given sequence of numbers where I chose the starting point. You can see my code here and while it works for the most part, for some values it doesn't.

I can't seem to find a pattern either. It works with the input of 22 and 80 but not 100 or 25. Those are only a few that I've tried, and I inserted cout statements in to track the progression of it...and it just seems like it quits at random points.

If anyone could possibly tell me the problem I'd greatly appreciate it. Thanks!
 

dinkumthinkum

Senior member
Jul 3, 2008
203
0
0
You seriously need to learn how to properly indent and format your code if you expect anyone else to read it. Plus it will help you spot errors. Automatically indenting editors are freely available. You have no excuse.

P.S. You probably didn't intend to put "n=(3*n)+1;" in the prime-checking loop. But, who knows ...
 

RedArmy

Platinum Member
Mar 1, 2005
2,648
0
0
Originally posted by: dinkumthinkum
You seriously need to learn how to properly indent and format your code if you expect anyone else to read it. Plus it will help you spot errors. Automatically indenting editors are freely available. You have no excuse.

P.S. You probably didn't intend to put "n=(3*n)+1;" in the prime-checking loop. But, who knows ...

I don't need an excuse. Every professor I've ever had in college has told us to do indenting and commenting different ways, so I just do what makes sense to me, because in the end I'm the one that has to deal with it. If this was some group collaboration then yeah, I would try and format it to a certain standard. However, I'm sure as hell not gonna go reading through a book of ISO standards of how to properly indent.

I'm just throwin' this out there, I'm not trying to please anyone. Also, I do need that n=(3*n)+1 in the loop since it needs to be implemented regardless if it's prime or not.
 

Cogman

Lifer
Sep 19, 2000
10,286
145
106
Originally posted by: RedArmy
Originally posted by: dinkumthinkum
You seriously need to learn how to properly indent and format your code if you expect anyone else to read it. Plus it will help you spot errors. Automatically indenting editors are freely available. You have no excuse.

P.S. You probably didn't intend to put "n=(3*n)+1;" in the prime-checking loop. But, who knows ...

I don't need an excuse. Every professor I've ever had in college has told us to do indenting and commenting different ways, so I just do what makes sense to me, because in the end I'm the one that has to deal with it. If this was some group collaboration then yeah, I would try and format it to a certain standard. However, I'm sure as hell not gonna go reading through a book of ISO standards of how to properly indent.

I'm just throwin' this out there, I'm not trying to please anyone. Also, I do need that n=(3*n)+1 in the loop since it needs to be implemented regardless if it's prime or not.

Indentation is important when you are asking for help on the internet. at very least be consistant. crap like

if(blah)
{ Do stuff; }

is very ugly and can be hard to read as the curvy braces look pretty simular to parentheses. at very least that line would look better like this.

if(blah)
Do stuff;

as those braces aren't needed.

as was stated, this is part of your problem

for(int i=2; i<=sqrt(n);i++)
if (n%i==0){
isPrime=false;
cout<<n<<endl;
n=(3*n)+1;}

your changing the value of n when checking to see if n is prime. That doesn't work. since your prime detection is based on incrementing i and dividing n by it, eventually you will hit a point where your new n is divisible by some i that you entered, or n changes enough that it won't be divisible by i. If you have to do this for whatever reason (I honestly don't see why) then do it in a separate loop.


Also, is there any particular reason that you have the iostream header included, you're using cout, but then for input you are using fgetc? Why? you already have that overhead of iostream, you might as well use cin.
 

iCyborg

Golden Member
Aug 8, 2008
1,352
62
91
The else block should look like:
else{
bool isPrime = true;
for(int i=3; i<=sqrt(n);i++)
if (n%i==0){
isPrime=false;
cout<<n<<endl;
n=(3*n)+1;
break;
}
if(isPrime){
prime+=1;
cout<<n<<endl;
n=(3*n)+1;}
}
}

1. You never reset isPrime, and you should do it in that else block.
2. As mentioned by people before me, don't update n inside that for loop more than once. And we can move on once we know it's not prime.
3. I don't find the identation too bad with the exception of that for loop. It consists of a single if statement and there's a second if statement right after it on the same level of identation even though it's outside of the for loop. That's confusing.