HiveBrain v1.2.0
Get Started
← Back to all entries
patterncMinor

Longest collatz sequence using dynamic programming

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
programmingsequenceusingdynamiclongestcollatz

Problem

I am trying to solve longest collatz sequence problem under 1000000 with the below code. Can anyone suggest a faster way to approach this problem? I was thinking of dynamic programming, but I'm having trouble in understanding it.

#include 

long long int col(long long int n)
{
    if(n==1)
    {
        return 1;
    }
    else
    {
        if(n%2==0)
            return (1+col(n/2));
        else
            return (1+col(3*n+1));
    }
}

int main()
{
    long long int i=0, c, max, k=1;
    max=1;
    for(i=1; imax)
        {
            max=c;
            k=i;
        }
    }

    printf("%lld",max);
    printf("\n%lld\n",k);
    return 0;
}

Solution

Well, it appears you are having a bit of trouble getting things off-the-ground, so to speak. Let's start with the basics. Let's name your file collatz.c and after a quick glance, it looks like it should compile:

gcc -Wall -Wextra -o ctz collatz.c


Good, it compiled with no errors and no warnings. Now let's see if it will run:

output:

$ time ./ctz
525
837799

real    0m2.088s
user    0m2.082s
sys     0m0.004s


Also good, max is 525 and k is 837799 and it completed in less than 2.1 seconds. The logic is implemented in a single recursive function akin to that used by a non-math-lib power function, so no great speed improvements come to mind. As was pointed out, there are optimization that can help reduce the execution time. Let's try the suggested -Ofast -fwhole-program:

gcc -Wall -Wextra -o ctz collatz.c -Ofast -fwhole-program


output:

$ time ./ctz
525
837799

real    0m0.480s
user    0m0.474s
sys     0m0.003s


A 400+% improvement. That's better. So it looks like your work is done. Drop a comment or edit your question if you have more specifics in mind.

Code Snippets

gcc -Wall -Wextra -o ctz collatz.c
$ time ./ctz
525
837799

real    0m2.088s
user    0m2.082s
sys     0m0.004s
gcc -Wall -Wextra -o ctz collatz.c -Ofast -fwhole-program
$ time ./ctz
525
837799

real    0m0.480s
user    0m0.474s
sys     0m0.003s

Context

StackExchange Code Review Q#63550, answer score: 2

Revisions (0)

No revisions yet.