Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8176303
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T23:07:08+00:00 2026-06-06T23:07:08+00:00

I am currently solving, Project Euler problem 14 : The following iterative sequence is

  • 0

I am currently solving, Project Euler problem 14:

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 rule above and starting with 13, we generate the following sequence:
               13 → 40 → 20 → 10 → 5 → 16 → 8 → 4 → 2 → 1  

Which starting number, under one million, produces the longest chain?

I devised the following algorithm to solve the problem:

  • Instead of finding series for each number separately, which will contain lots of redundant calculation, I try to develop the series backwards from 1. That is, start from number and predict the element before it.
  • Since multiple series can be generated, store the recent number of all series using a linked list. (The idea is to store only those elements that have longer series.)
  • I will loop this, until I find all the numbers under the given limit; the last number under the limit will have the longest series.

Here is my code:

void series_generate(long num)
{
    long n = 1;
    addhead(n);             //add 1 to list.
    while (n < num) 
    {
            series *p;
            for (p = head; p != NULL; p = p->next)
            {
                    long bf = p->num - 1;
                    if (p->num%2 == 0 && bf != 0 && bf%3 == 0) {
                            bf /= 3;
                            if (bf != 1)
                                    addhead(bf);
                            if (bf < num)
                                    n++; 
                    }
                    p->num *= 2;
                    if ( p->num < num)
                            n++;

            }       
    }       
}

Here is the link to complete code.
However, I don’t get the answers as I expected.
Can anyone expain why this algorithm won’t work?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T23:07:10+00:00Added an answer on June 6, 2026 at 11:07 pm

    You’re trying to build the Collatz tree backwards, level per level. Thus after the k-th iteration of the inner loop, the list contains (while no overflow occurred) all numbers needing exactly k steps to reach 1 in their Collatz sequence.

    That approach has two serious problems.

    1. The size of the levels increases exponentially, the size doubles roughly every three levels. You don’t have enough memory to store levels past not much over 100.
    2. The largest member in level k is 2k. Depending on the used type for the num member, you get overflow at level 31, 32, 63 or 64. Then, if you use a signed type, you have undefined behaviour, probably negative numbers in the list, and all goes haywire. If you use unsigned types, your list contains a 0, and all goes haywire.

    Since 27 needs 111 steps to reach 1, you have overflow whenever num > 27, therefore you get wrong results.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm currently working on a project Euler problem (www.projecteuler.net) for fun but have hit
While solving some Project Euler Problems to learn Haskell (so currently I'm a completly
I'm currently going through the Project Euler's problems and I'm solving them both in
I'm currently in the process of solving euler problems to improve in Haskell. Though,
Problem 17 on project euler states: If the numbers 1 to 5 are written
I'm currently solving a programming problem to enhance my skills (I'm still a newbie)
I am currently solving a system of differential equation under python using odeint to
I'm currently working with a project using Hibernate + JPA. I don't recall exactly
This is fairly 'math-y' but I'm posting here because it's a Project Euler problem,
I'm currently using the following query (simplified indeed) : SELECT ( SELECT COUNT(id) FROM

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.