sathyashrayan said:
Dear group,
The below given link which I want to implement in C.
http://online-judge.uva.es/p/v1/100.html
The code which I wrote does not even reaches the near to the
given problem.(I am just learning)
Can any one give me some clue or hint for correct implementation.
of the above?
Hmmm....
"The input will consist of a series of pairs of integers i and j, one
pair
of integers per line. All integers will be less than 1,000,000 and
greater than 0. ...
"You can assume that no opperation overflows a 32-bit integer. "
You know what they say about _ass_u_me_ don't you... ;-)
% type 3n1.c
#include <stdio.h>
#include <stdlib.h>
#if 0
#define PRINT printf
#else
#define PRINT while (0) printf
#endif
int main(int argc, char **argv)
{
unsigned long i, j, input, n;
unsigned long c, mc = 0;
if (argc != 3) { return 0; }
i = strtoul(argv[1], 0, 10);
j = strtoul(argv[2], 0, 10);
if (i == 0) { return EXIT_FAILURE; }
for (input = i; input <= j; input++)
{
n = input;
PRINT("%lu -\n", n);
c = 1;
while (n != 1)
{
if ((n % 2) == 0) /* even? */
n /= 2;
else if (n <= (-1UL - 1) / 3) /* 'safe' odd? */
n = n * 3 + 1;
else /* overflow! */
{
printf("\noverflow");
printf(": input = %lu", input);
printf(", cycle = %lu", c);
printf(", n = %lu\n", n);
exit(EXIT_FAILURE);
}
PRINT(" %lu\n", n);
c++;
}
PRINT(" cycle(s):%lu\n", c);
if (c > mc) mc = c;
}
printf("%lu %lu %lu\n", i, j, mc);
return 0;
}
% acc 3n1.c -o 3n1.exe
% 3n1.exe 1 10
1 10 20
% 3n1.exe 100 200
100 200 125
% 3n1.exe 201 210
201 210 89
% 3n1.exe 900 1000
900 1000 174
% 3n1.exe 1 999999
overflow: input = 159487, cycle = 60, n = 1699000271
%