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 6088253
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:57:50+00:00 2026-05-23T11:57:50+00:00

I am new to Mathematica and am trying to understand patterns and rules. So

  • 0

I am new to Mathematica and am trying to understand patterns and rules. So I tried the following:

A = {1, 2, 3, 4}
A //. {x_?EvenQ -> x/2, x_?OddQ -> 3 x + 1}

This is based on: http://en.wikipedia.org/wiki/Collatz_conjecture

This is supposed to converge, but what I got is:

ReplaceRepeated::rrlim: Exiting after {1,2,3,4} scanned 65536 times. >>

Please help me understand my error in the pattern/rule.

Regards

  • 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-05-23T11:57:50+00:00Added an answer on May 23, 2026 at 11:57 am

    The way you wrote this, it does not terminate, so it eg ends up alternating between 1 and 4, 2 etc. (all recursive descriptions must eventually bottom out somewhere, and your does not include a case to do that at n=1).

    This works:

    ClearAll[collatz];
    collatz[1] = 1;
    collatz[n_ /; EvenQ[n]] := collatz[n/2]
    collatz[n_ /; OddQ[n]] := collatz[3 n + 1]
    

    although it does not give a list of the intermediate results. A convenient way to get them is

    ClearAll[collatz];
    collatz[1] = 1;
    collatz[n_ /; EvenQ[n]] := (Sow[n]; collatz[n/2])
    collatz[n_ /; OddQ[n]] := (Sow[n]; collatz[3 n + 1])
    runcoll[n_] := Last@Last@Reap[collatz[n]]
    
    runcoll[115]
    (*
    -> {115, 346, 173, 520, 260, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56,
    28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}
    *)
    

    or

    colSeq[x_] := NestWhileList[
    Which[
    EvenQ[#], #/2,
    True, 3*# + 1] &,
     x,
     # \[NotEqual] 1 &]
    

    so that eg

    colSeq[115]
    (*
    -> {115, 346, 173, 520, 260, 130, 65, 196, 98, 49, 148, 74, 37, 112, 56,
    28, 14, 7, 22, 11, 34, 17, 52, 26, 13, 40, 20, 10, 5, 16, 8, 4, 2, 1}
    *)
    

    By the way the fastest approach I could come up with (I think I needed it for some project Euler problem) was something like

    Clear@collatz;
    collatz[1] := {1}
    collatz[n_] := collatz[n] = If[
      EvenQ[n] && n > 0,
      {n}~Join~collatz[n/2],
      {n}~Join~collatz[3*n + 1]]
    

    compare:

    colSeq /@ Range[20000]; // Timing
    (*
    -> {6.87047, Null}
    *)
    

    while

    Block[{$RecursionLimit = \[Infinity]},
      collatz /@ Range[20000];] // Timing
    (*
    -> {0.54443, Null}
    *)
    

    (we need to increase the recursion limit to get this to run correctly).

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

Sidebar

Related Questions

So I'm completely new to MATLAB and I'm trying to understand colon notation within
I am very new to Java, and trying to use Mathematica's Java interface to
I'm pretty new to Mathematica and am stumped by this problem. I have a
New to javascript/jquery and having a hard time with using this or $(this) to
New to Linux programming in general. I am trying to communicate with a kernel
new Date(05-MAY-09 03.55.50) is any thing wrong with this ? i am getting illegalArgumentException
I'm currently reading the Mathematica Guidebooks for Programming and I was trying to work
I am getting an error while trying to run this Application ... the error
I'm very new to Mathematica. I want to use it as a data source
this is my first post here. First off, some background, I'm new to C#

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.