Liste des Groupes | Revenir à cl c |
On 3/19/2025 1:27 AM, Tim Rentsch wrote:
>DFS <nospam@dfs.com> writes:>
>On 3/18/2025 11:07 PM, Tim Rentsch wrote:>
>
>Have you thought about how large the value of 'n' can>
become inside the while() loop?
I was too smug in my first reply. [...]
Yes, I knew that already. Did you think I asked the question
without having first investigated the problem?
I wouldn't presume.
>
Did you investigate first?
I just now did:
>
gcc on Kali Linux (in Windows WSL)
>
run it: $ ./weird start stop
>
$time ./weird 1 1000000
<1000000 lines will be output>
max n = 56991483520 reached at input = 704511
>
real 0m5.792s
>
>
>
code
-----------------------------------------------------------------
// If n is even, divide it by two.
// If n is odd, multiply it by three and add one.
// Repeat until n is one.
// example: the sequence for n=3 is 3 10 5 16 8 4 2 1
>
#include <stdio.h>
#include <stdlib.h>
>
int main(int argc, char *argv[])
{
int steps, input;
int startN = atoi(argv[1]);
int stopN = atoi(argv[2]);
int64_t n = 0, max = 0, thismax = 0;
>
for (int i = startN; i <= stopN; i++) {
>
n = i;
steps = 1;
thismax = n;
while(1) {
>
if((n % 2) == 0)
{n /= 2;}
else
{n = (n * 3) + 1;}
>
if (n > max) {max = n; input = i;}
if (n > thismax) {thismax = n;}
steps++;
>
if (i == 1) {
printf("input 1, max n = 1, steps = 1\n");
break;
}
>
if(n == 1) {
printf("input %d, max n = %6lld, steps = %4d\n",
i, thismax, steps);
break;
}
}
>
>
}
printf("\nmax n = %lld reached at input = %d\n", max, input);
return 0;
}
>
-----------------------------------------------------------------
>
You'll get compilation warnings about the printf specifier used with
int64_t.
Les messages affichés proviennent d'usenet.