Questions tagged [collatz]

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.

284 questions
2
votes
2 answers

How can I shorten a function which outputs the chain numbers of the Collatz conjecture?

This Python function outputs a list based on the collatz conjecture. This is an unsolved maths problem where the function will perform different operations on 'n' depending on if it is odd or even, outputting 'n' to a list called 'seq' each time the…
2
votes
2 answers

Further Optimisation of Project Euler problem 14 (Collatz Sequence)

When I first starting trying the question, my code would take over a minute to even finish running and give me the answer. I have already tried dynamic programming and storing previous numbers so it doesn't have to run the same number multiple…
2
votes
2 answers

Is there a way to optimise the Collatz conjecture into a branchless algorithm?

I'm trying to create an algorithm that will compute the collatz conjecture, this is the code so far: while (n > 1) { n % 2 == 0 ? n /= 2 : n = n * 3 + 1; } I was wondering if there was a way to optimize this any further since efficiency and…
Existentialist
  • 177
  • 2
  • 9
2
votes
1 answer

Why is memoization of collision-free sub-chains of Collatz chains slower than without memoization?

I've written a program to benchmark two ways of finding "the longest Collatz chain for integers less than some bound". The first way is with "backtrack memoization" which keeps track of the current chain from start till hash table collision (in a…
David Bandel
  • 252
  • 3
  • 19
2
votes
2 answers

How to count how many times a number was printed using python?

So I made a nooby Collatz Sequence showing program. I am interested in knowing how many times number was printed by the computer so that I can see how many steps it took for a number to eventually become 1. If you don't know much about the Collatz…
2
votes
2 answers

Does Python have trouble with large numbers/lists, or is there something wrong with my code?

I have the following code: def main(n): sequence = [] while n not in sequence: sequence.append(n) if n % 2 == 0: i = n / 2 else: i = (n * 3) + 1 n = int(i) print("Sequence length:…
BWPanda
  • 145
  • 7
2
votes
1 answer

Finding the 2nd longest array in Ruby - Collatz conjecture algorithm

The Collatz conjecture: https://en.wikipedia.org/wiki/Collatz_conjecture For any positive integer n we define two rules: if even: divide by two if odd: multiply by three, then add one, and repeat until the result is the number 1. The smallest value…
huxley
  • 23
  • 4
2
votes
1 answer

Collatz Sequence - getting a None at the end

Learning from "Automate The Boring Stuff" by Al Sweigart. At the end of Chapter 3, the Collatz Sequence is given as practice. The output seems correct, but the last row has a 'None' in it. In the code below, I am guessing that when p = 1, it gets…
Philomath
  • 27
  • 5
2
votes
2 answers

I have a problem with "The 3n+1 problem".when i debug it

I've tried to solve "The 3n+1" problem. When I debug my code it stuck at line 12, calculation function. "According to Collatz conjecture, j should converge to 1." Main file #include "input_output.h" #include int main() { …
BoHyunK
  • 59
  • 6
2
votes
4 answers

Collatz Loop Structure

Please help me understand what i did wrong. The problem is The Collatz Sequence Write a function named collatz() that has one parameter named number. If number is even, then collatz() should print number // 2 and return this value. If number is…
chris123
  • 43
  • 3
2
votes
2 answers

3n+1 challenge at UVa

I'm having trouble running the "3n+1 Problem" from the "Programming Challenges" book. I've tried every solution in Java I could find on google (even the ones on Stack Overflow), and not a single one works, they all report a "Wrong answer". I also…
Óscar López
  • 232,561
  • 37
  • 312
  • 386
2
votes
2 answers

Python: Repeat try/except statement until condition met

I'm learning python, and am stuck on a project. The main part of the project was to code the collatz sequence, which wasn't a problem. The next part is to validate the user input using try and except, in order to make sure only an integer value is…
sar91
  • 125
  • 1
  • 4
2
votes
1 answer

Collatz sequence from 1 to a given value

#include int main() { int rangeValue; int x; printf("Please input a number to be considered in the range:\n"); scanf("%d", &rangeValue); while (rangeValue != 1) { x = rangeValue; if ((x % 2) == 0) { …
C-flow
  • 19
  • 1
  • 3
2
votes
2 answers

Collatz Chain Algorithm RUBY

I am trying to populate an array according to the Collatz sequence. The constraints for the sequence are as follows: positive integers: n → n/2 (n is even) n → 3n + 1 (n is odd) Example Output 3 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1 Ideally, I…
Natasha Kelly
  • 73
  • 1
  • 12
2
votes
1 answer

Ruby - Using Hash to solve Collatz sequence

I am pretty new to solving algorithms so bear with me. I've solved the Collatz sequence for max length of 1,000,000 but I want to make it more efficient by using a hashtable to look up keys that already exist in order make the function faster.…
Lulu Li
  • 83
  • 1
  • 5