I'm trying to execute Project Euler Problem 14 in C ++, and I'm honestly stuck. Right now, when I run the problem, it is stuck on So Far: the number with the highest score: 113370 with the number 155 Until now: the number with the highest score, but when I try to change the value I am to 113371, it works. What's happening?
The question arises:
The following iterative sequence is defined for the set of positive integers: n → n / 2 (n is even) n → 3n + 1 (n is odd)
Using the above rule and starting at 13, we create the following Sequence:
13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1 You can see that this sequence (starting from 13 and ending with 1) contains 10 terms. Although this has not yet been proven (Collatz problem), it thought that all start numbers end with 1. Which start number, less than a million, produces the longest chain?
#include<stdio.h>
int main() {
int limit = 1000000;
int highNum, number, i;
int highCount = 0;
int count = 0;
for( number = 13; number <= 1000000; number++ )
{
i = number;
while( i != 1 ) {
if (( i % 2 ) != 0 ) {
i = ( i * 3 ) + 1;
count++;
}
else {
count++;
i /= 2;
}
}
count++;
printf( "So Far: the number with the highest count: %d with the count of %d\n",
number, count );
if( highCount < count ) {
highCount = count;
highNum = number;
}
count = 0;
}
printf( "The number with the highest count: %d with the count of %d\n",
highNum, highCount );
}
source
share