The Collatz Conjecture is a conjecture that a certain algorithm always terminates. The algorithm is stated as follows: starting with some positive integer n, divide n by two if it is even and otherwise triple n and add one. The algorithm terminates when n reaches one. It is currently an open problem whether this terminates for all positive integers. It is also called the Hailstone Sequence.
Questions tagged [collatz]
284 questions
5
votes
5 answers
why does the loop die? ( Collatz conjecture )
i am trying some math-operations with java, that does test a number if its (un)even and alter it as long as it gets to 1.
I try to run my loop for 999999times, it seems to get stuck at around ~120000times. Well, it is not stopping with an…

bofredo
- 2,348
- 6
- 32
- 51
5
votes
1 answer
Dynamic programming with Data.Map in Haskell?
I am trying to implement a simple dp algorithm in Haskell (this is for the Collatz conjecture problem from Project Euler); here is the equivalent c++:
map a;
int solve(int x) {
if (a.find(x) != a.end()) return a[x];
return a[x] = 1 + /*…

Kirill
- 470
- 3
- 13
4
votes
1 answer
Different ways of expressing collatz conjecture in prolog fail
I'm learning prolog using SWI Prolog and the tutorial here. I find that if I express the collatz conjecture exactly like they do in the video, it works as long as I replace #= with is which I'm guessing is a swipl vs scryer-prolog difference. But if…

Joseph Garvin
- 20,727
- 18
- 94
- 165
4
votes
2 answers
Segmentation Fault Error while doing big calculations in python
I want to calculate collatz sequence for some very big number but i guess python is failing to handle this much big numbers and i don't know how to make it handle so.
Here's my program:
def collatz(n):
if (n == -1 or n == 1 or n == -17 or n ==…

Vicrobot
- 3,795
- 1
- 17
- 31
4
votes
1 answer
Memoization Efficiency Problems (Collatz Hailstone Sequence)
I was particularly interested over the last few days (more from an algorithmic than mathematical perspective) in investigating the length of a given number's Hailstone sequence (Collatz conjecture). Implementing a recursive algorithm is probably the…

SilverSylvester
- 51
- 7
4
votes
4 answers
Stuck in an infinite loop? (Maybe)
I am trying to complete Project Euler Problem 14 in c++ and I am honestly stuck. Right now when I run the problem it gets stuck at So Far: the number with the highest count: 113370 with the count of 155
So Far: the number with the highest count but…

PictureMeAndYou
- 533
- 2
- 5
- 16
4
votes
2 answers
Memoization with recursive method in java
I am working on a homework assignment, and I have completely exhausted myself. I'm new to programming, and this is my first programming class.
this is the problem:
Consider the following recursive function in Collatz.java, which is related to a…
user3340067
4
votes
1 answer
Is this memoization working properly?
I'm have been working on solving Project Euler #14 for a while now in Haskell, but for some reason, I'm unable to get it working. I solved the problem using Groovy a while ago, and I think I'm using basically the same method here. However, the…

Benjamin Kovach
- 3,190
- 1
- 24
- 38
3
votes
1 answer
Why is `fmap` needed to call `succ` on a `Maybe Integer`?
I am brand new to Haskell and am working on a practice Collatz conjecture problem. The required output is the number of steps needed to get to 1 from a given integer. This is the first time I've had to use the Maybe type, which may be contributing…

arsalanQ
- 127
- 6
3
votes
0 answers
How to resolve this type error in my Prolog code for generating the Collatz Sequence?
I am learning Prolog, specifically GNU Prolog, for fun. I thought an interesting challenge would be to generate a sequence of Collatz numbers given a seed, which here is 5. Here's my pseudocode:
collatz(curr, next) -> if even, next is curr /…

Caspian Ahlberg
- 934
- 10
- 19
3
votes
5 answers
Automate The Boring Stuff With Python - Collatz Sequence
I'm very new to any sort of coding, currently using python 3.3. I've managed to run the Collatz Sequence accurately in python with the following:
while True: # The main game loop.
number = int(input('Enter number:\n'))
def collatz(number):
…

biscuit
- 29
- 2
3
votes
2 answers
Does recursive functions have some limitations? eg: how many layers does the function require?
Made a recursive function which gives how many terms are there in a collatz sequence given a starting number, this is the code n=13 for exemple :
int collatz(long n,long o)
{
if (n!=1) {
if(n%2==0)
return collatz(n/2,o+1);
…

Takelovski
- 53
- 1
- 4
3
votes
1 answer
Collatz conjecture in R
I am still teaching some R mainly to myself (and to my students).
Here's an implementation of the Collatz sequence in R:
f <- function(n)
{
# construct the entire Collatz path starting from n
if (n==1) return(1)
if (n %% 2 == 0)…

tschm
- 2,905
- 6
- 33
- 45
3
votes
1 answer
python Reverse Collatz Conjecture
What the program should do is take steps and a number and than output you how many unique sequences there are with exactly x steps to create number.
Does someone know how I can save some memory - as I should make this work for pretty huge numbers…

Dominik Lemberger
- 2,287
- 2
- 21
- 33
3
votes
2 answers
3n+1 giving negative numbers
I recently found out about the 3n+1 problem and wanted to write a simple code to do the problem.
It all works but at high odd numbers such as 999,999,999 it goes to negative numbers and repeats an endless cycle and I have no clue why.
// if n is…

Sulhan
- 116
- 1
- 7