Skip to main content

Questions tagged [collatz-sequence]

This tag should be used for code related to the sequences described in the Collatz conjecture.

Filter by
Sorted by
Tagged with
9 votes
4 answers
2k views

"Weird Algorithm" (Collatz) from CSES Problem Set

I had to solve the following problem: Consider an algorithm that takes as input a positive integer n. If n is even, the algorithm divides it by two, and if n is odd, the algorithm multiplies it by ...
user23259461's user avatar
6 votes
2 answers
223 views

Collatz - another view

Most coders have, at one time or another, dabbled with "The Collatz Conjecture"; a pair of simple math operations that seem to always find their way cycling through ...
user avatar
3 votes
1 answer
152 views

Collatz conjecture plots (python)

I have tried to improve my code with the suggestions I recieved in my old question about Collatz conjecture Here is the new code: ...
Lorenzo's user avatar
  • 157
4 votes
3 answers
757 views

Plotting Collatz conjecture values - Python

I am writing a program to display the behavior or certain seeds when you apply the Collatz conjecture algorithm to them. I would like to know what I should change and also if there is a way to see ...
Lorenzo's user avatar
  • 157
10 votes
2 answers
2k views

Collatz sequence in C

First of all, if you're unfamiliar with the Collatz sequence, it recursively applies the following function to an integer: $$ f(n) = \begin{cases} n/2 & \text{if } n \equiv 0 \pmod{2}, \\ 3n+1 &...
nyz's user avatar
  • 203
3 votes
3 answers
1k views

Automate the boring stuff with Python - The Collatz Sequence

The Collatz Sequence Write a function named collatz() that has one parameter named number. If ...
Jarne Vercruysse's user avatar
2 votes
1 answer
106 views

Meta Collatz Sequence

Context Chapter 6 of Learn You A Haskell contains a function called chain, which outputs the Collatz sequence of a given input. (In short, it takes a natural number....
Jessica's user avatar
  • 910
7 votes
1 answer
242 views

A Python 3 script that generates art using matplotlib

I am always super interested in both science and art, science and art are two different ways to describe the objective reality, they are two sides of the same coin, in many areas they overlap, and ...
Ξένη Γήινος's user avatar
3 votes
2 answers
216 views

Beating the compiler with a simple program finding the longest collatz sequence

I found this question in Stackoverflow asking why his/her assembly program isn't faster than the C++ program. In his/her assembly program I saw the lines ...
xiver77's user avatar
  • 419
3 votes
1 answer
256 views

Collatz conjecture with plots

I have an assignment where I write a Collatz Conjecture program for a series of starting values from 1 to N and make two plots: number of iterations vs starting value and computed numbers vs starting ...
Dila's user avatar
  • 215
2 votes
0 answers
230 views

Maximizing Efficiency of Collatz Conjecture Program Python

My question is very simple. I wrote this program for pure entertainment. It takes a numerical input and finds the length of every Collatz Sequence up to and including that number. I want to make it ...
Ghull's user avatar
  • 41
2 votes
1 answer
952 views

How to make my Collatz Conjecture code run a lot faster? (and even better, how do I implement it into multiple computers?) Python

I have been writing this piece of code for a while now, and I was wondering, is it possible to run this code multiple times on my pc? For example could I make the code solve all the numbers from 1 to ...
Friendio1's user avatar
10 votes
2 answers
3k views

My recursive attempt at Collatz Sequence in Python

...
flamethrower10's user avatar
5 votes
2 answers
250 views

Project Euler Problem #277

I am trying to solve Euler project problem 277 using python 3.8. First, I will show my program and than ask specific questions for enhancements. The problem is a modified version of Collatz sequence. ...
Lady Be Good's user avatar
6 votes
2 answers
698 views

Basic Collatz program with input validation in Python

In chapter 3 of Automate the Boring Stuff with Python by Al Sweigart, one exercise asks to write a program which performs the Collatz sequence. I wrote a basic version first, then added and ...
astralcheeks's user avatar
5 votes
1 answer
136 views

Hailstone Sequence in NASM

For practice, I wrote some NASM code that prints out the hailstone sequence of a (unfortunately, hardcoded) number. This is by far the most complex code I've ever written in NASM. I'd like advice on ...
Carcigenicate's user avatar
2 votes
1 answer
232 views

Collatz sequence using Python 3.x

I created this program originally for Project Euler 14. Here's the code: ...
Sriv's user avatar
  • 2,800
4 votes
3 answers
636 views

Calculating a Collatz Sequence

I've written a small script to calculate Collatz sequences. For background, read here about the Collatz conjecture. Largest number I've tried so far in my implementation is 93571393692802302 (which ...
Gloweye's user avatar
  • 1,746
4 votes
1 answer
566 views

Computational verification of Collatz conjecture using OpenCL

This code review request follows my previous request Computational verification of Collatz conjecture. Unlike the previous program (which was designed for the CPU), this code should run on modern GPUs....
DaBler's user avatar
  • 309
4 votes
4 answers
454 views

Optimized Longest Collatz Sequence

I'm writing a program to calculate the longest collatz sequence for positive integers below some N. I have optimized it as far as I can - is there anything more I could do to optimize it? Any ...
Ori Yonay's user avatar
10 votes
3 answers
9k views

Python - The Collatz Sequence

Can I get a review of my code for the the Collatz Sequence from Chapter three of Automate the Boring Stuff with Python? The Collatz Sequence Write a function named collatz() that has one ...
cyberprogrammer's user avatar
3 votes
2 answers
170 views

Computational verification of Collatz conjecture using gmplib

Prerequisites The unsigned long type has a size of 64 bits. The following macros are defined. ...
DaBler's user avatar
  • 309
3 votes
1 answer
476 views

Computational verification of Collatz conjecture

Prerequisites The typedef name uint128_t designates an unsigned integer type with width exactly 128 bits. The ...
DaBler's user avatar
  • 309
1 vote
1 answer
139 views

python : Collatz Sequence

I am a beginner in python. I found this problem in the book Automate the Boring Stuff with Python. How can I improve this program using latest version of python. ...
coder's user avatar
  • 2,471
2 votes
1 answer
313 views

Automate the Boring Stuff - Collatz Exercise

I wrote the following code for the Collatz exercise: The Collatz Sequence Any feedback is appreciated. Write a function named collatz() that has one parameter ...
drapozo's user avatar
  • 115
0 votes
1 answer
350 views

Finding the longest Collatz sequence using Go, concurrently

I am working through this problem on project euler. Basic purpose of the code: ...
The Nightman's user avatar
3 votes
4 answers
1k views

Testing a Collatz Conjecture Conjecture (Python)

I had an intuition that the maximal trajectory lengths for collatz conjecture numbers would be produced by numbers of the form (2**n)-1 so I wrote a program to test ...
Tobi Alafin's user avatar
  • 1,792
6 votes
1 answer
160 views

Collatz Conjecture Disprover Unit - Googol Edition

The Wikipedia article about the Collatz Conjecture has these quotes: If the conjecture is false, it can only be because there is some starting number which gives rise to a sequence that does not ...
Sep Roland's user avatar
  • 4,813
3 votes
2 answers
580 views

Contest Solution: Hailstones

The Problem In the January 1984 issue of Scientific American, an interesting sequence of numbers known as a hailstone series was described. The series is formed by generating the next number ...
T145's user avatar
  • 3,159
15 votes
2 answers
3k views

Automate the Boring Stuff with Python - The Collatz sequence project

I am new to programming and started learning with Automate the Boring Stuff with Python. I just completed chapter 3's "Collatz sequence" project. I was wondering what would you do differently and ...
Alex's user avatar
  • 151
4 votes
0 answers
298 views

Collatz fractal in Rust

Among the many fractals, there is Collatz Fractal based on a complex extension of: $$f(x) = \left\{ \begin{array}{ll} \frac{x}{2} \space \text{if even} \\ 3x + 1 \space \text{if odd} \end{array} \...
Dair's user avatar
  • 6,220
1 vote
1 answer
228 views

Longest Collatz sequence in Scala

I am trying to find the longest Collatz Sequence: Which starting number, ≤ N, produces the longest chain? If many possible such numbers are there print the maximum one. (1 ≤ N ≤ 5×106) I tried ...
Shankar Shastri's user avatar
1 vote
3 answers
689 views

Euler Project 14 Longest Collatz Sequence [closed]

I have been working through the euler project and I have created the following code for Problem 14, which asks which starting number, under 1 million, produces the longest Collatz Sequence chain. It ...
johnashu's user avatar
  • 433
10 votes
4 answers
5k views

Python Automate the Boring Stuff Collatz exercise

I'm looking for some feedback on my code for the collatz function exercise in the Python book Automate the Boring Stuff. It works, but doesn't feel very elegant to me. Is there a better way to ...
user3342947's user avatar
4 votes
1 answer
217 views

Generating the Collatz sequence in F#

Below is my code for building the collatz sequence so a given number. I'm trying to make the function tail recursive and I believe that this impelemtation is tail recursive but I'm not yet so ...
Jackson's user avatar
  • 507
3 votes
1 answer
195 views

Project Euler problem #14 (longest Collatz chain) in Swift

This is my Swift code for finding the starting number, under one million, which produces the longest collatz chain. ...
mobs_boss's user avatar
  • 131
5 votes
4 answers
8k views

Collatz Conjecture (c++)

I am new to C++ and this is my first program. The objective of this program is to fully complete the sequence called the Collatz conjecture. It concerns a sequence defined as follows: start with any ...
jzbakos's user avatar
  • 415
3 votes
2 answers
562 views

Counting terms in Collatz sequences until 1 is reached or a limit is exceeded

My friend and I are into competitive programming and we wrote two very similar programs for a problem. His got accepted with a runtime of 0.6 s while mine didn't get accepted even after 3 s. I was ...
Karan Singh's user avatar
3 votes
1 answer
405 views

Collatz Conjecture Meet of 2 Numbers

I need the most efficient way to calculate when the Collatz Conjecture of two numbers, a and b respectively, are the same. Here ...
Imagine Dragons's user avatar
3 votes
3 answers
862 views

Longest Collatz sequence using dynamic programming in Java

I'm not getting where else should I optimize it. Is there any way to optimize this code any further? Just give me hints. ...
Abhinav Kushagra's user avatar
4 votes
2 answers
8k views

Project Euler #14 - Longest Collatz Sequence in Python

I recently solved Project Euler Problem 14 in Python: The following iterative sequence is defined for the set of positive integers: ...
D Farmer's user avatar
3 votes
2 answers
362 views

UVA 100: “The 3n + 1 problem” take 2

This is my second attempt to solve this problem, my first attempt is here: UVA 100: "The 3n + 1 problem" This time I tried to take lessons from the failures and mistakes You pointed out in ...
gaazkam's user avatar
  • 581
12 votes
2 answers
2k views

"Longest Collatz sequence" in C slower than in Python 3

This tight-looped memoization-less C solution to Project Euler's problem 14 seems efficient, but ran much slower than a similarly trivial Python 3 program. For the desired ...
user133296's user avatar
9 votes
4 answers
1k views

Project Euler problem 14 (longest Collatz sequence) in Swift 3

I was trying to solve Project Euler:Problem 14 using Swift 3, but it takes ages to give me an answer, which is a sign that my code is absolute garbage performance-wise. What could I do to increase the ...
user avatar
6 votes
2 answers
853 views

UVA 100: "The 3n + 1 problem"

I’ve solved UVA Problem 100: The 3n + 1 problem . In short, the task was to write a program that calculates a maximum of Collatz sequence lengths for any given range of starting values. My program: <...
gaazkam's user avatar
  • 581
2 votes
3 answers
681 views

Project Euler: Longest Collatz Sequence

The Collatz Sequence is an iterative sequence that is defined for all positive integers as such: n → n/2 (n is even) n → 3n + 1 (n is odd) If 13 were to be entered into this sequence, the result ...
user avatar
6 votes
1 answer
642 views

Locating the largest Collatz Sequence with memoization

Inspired by this question I wrote a memoized form of the collatz sequence calculator. The idea was to cache the sequence lengths for as many sequences as possible, from 0 to some value. I managed to (...
Der Kommissar's user avatar
-4 votes
3 answers
6k views

Finding the longest Collatz Sequence

Inspired by Project Euler 14: Longest Collatz Sequence "Which starting number, under one million, produces the longest Collatz Sequence chain?" I wrote the following Java code: ...
Ruwangi's user avatar
  • 13
1 vote
1 answer
774 views

Python program for testing the Collatz Conjecture

This is a simple program to test if integers will follow an interesting, improvable statement that if disproven, calls for a bravo. But, ignoring the pretentious poetry, this code allows the user to ...
JessLovely's user avatar
3 votes
2 answers
1k views

Calculating the Collatz conjecture of a number

I've made a Collatz Conjecture sequence calculator and I would like some feedback on what I can do better in regards to the code I've implemented, i.e. bad practices you see, improvements that can be ...
Oscar Guillamon's user avatar

Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant