+ Reply to Thread
Results 1 to 18 of 18

Collatz conjecture

Hybrid View

  1. #1
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Collatz conjecture

    Hello everyone!

    The Collatz conjecture is a conjecture in mathematics that concerns a sequence defined as follows: start with any positive integer n. Then each term is obtained from the previous term as follows: if the previous term is even, the next term is one half the previous term. Otherwise, the next term is 3 times the previous term plus 1. The conjecture is that no matter what value of n, the sequence will always reach 1.

    I am trying to program something that will show me the amount of steps (of loops) that happened before the number n became smaller than the initial number.
    It is the variable dva, but I don't know how to find it!
    then I also need to show every step: every value of n (one or each loop) and
    Also the highest value that n takes throughout the loops (am)
    and the number of steps until n reaches 1 (dv)

    I have started something but I feel stuck moving forward as the beginning doesn't even work..

    can someone point me to where I'm messing up here?



    Sub collatz2()
    n = InputBox("type in a number ")
    
    am = 0
    
    Do While n <> 1
        If n Mod 2 = 0 Then
            n = n * 2
        Else: n = n * 3 = 1
        End If
    
    If n > am Then
        Z = am
    End If
    
    x = x + 1
    Dim ligne As Integer
    'Cells(1, 1).Value = "Trip"
    Cells(x, 1).Value = n
    
    Loop
    
    'Cells(1, 2).Value = "Steps"
    Cells(x, 2).Value = x
    
    'Cells(1, 3).Value = "Maximal altitude"
    Cells(x, 3).Value = am
    
    End Sub
    Last edited by candicee; 12-24-2017 at 10:00 PM.

  2. #2
    Registered User
    Join Date
    12-02-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    16

    Re: Collatz conjecture

    Is this what you want?

    Sub collatz2()
    n = InputBox("type in a number ")
    
    am = 0
    
    Do While n <> 1
        If n Mod 2 = 0 Then
            n = n / 2
        Else
            n = n * 3 - 1
        End If
    
    If n > am Then
        Z = am
    End If
    
    x = x + 1
    Dim ligne As Integer
    'Cells(1, 1).Value = "Trip"
    Cells(x, 1).Value = n
    
    Loop
    
    'Cells(1, 2).Value = "Steps"
    Cells(x, 2).Value = x
    
    'Cells(1, 3).Value = "Maximal altitude"
    Cells(x, 3).Value = am
    
    End Sub
    Last edited by svg3414; 12-24-2017 at 10:17 PM.

  3. #3
    Forum Moderator
    Join Date
    01-21-2014
    Location
    St. Joseph, Illinois U.S.A.
    MS-Off Ver
    Office 365 V 2504
    Posts
    13,742

    Re: Collatz conjecture

    Is it required the solution be VBA? If not here is formula approach.


    A
    B
    C
    D
    E
    F
    1
    Row #s
    + Integer
    n < 17 (dva)
    am
    loops till 'dv'
    2
    1
    17
    4
    52
    13
    In B3 and down: =ISEVEN(B2)*B2*0.5+ISODD(B2)*(B2*3+1)
    3
    2
    52
    In C2: =MATCH(1,INDEX(--($B$2:$B$22<B2),0),0)
    4
    3
    26
    In D2: =MAX(B:B)
    5
    4
    13
    In E2: =MATCH(1,$B$2:$B$22,0)
    6
    5
    40
    7
    6
    20
    8
    7
    10
    9
    8
    5
    10
    9
    16
    11
    10
    8
    12
    11
    4
    13
    12
    2
    14
    13
    1
    15
    14
    4
    16
    15
    2
    17
    16
    1
    18
    17
    4
    19
    18
    2
    20
    19
    1
    21
    20
    4
    22
    21
    2
    23
    Dave

  4. #4
    Registered User
    Join Date
    12-02-2017
    Location
    India
    MS-Off Ver
    2007
    Posts
    16

    Re: Collatz conjecture

    Try out my code and check if it works to your requirement.

  5. #5
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    Hey, sorry I had dozed off!

    Thank you! And merry Christmas

    It doesn't work, I think it's the same except you fixed the n = n * 2?

    My main problem is with the part where I'm supposed to show the steps, and the highest number, and the highest number before reaching n =1 (which I havent started coding yet because I can't find a way that works!)

  6. #6
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Collatz conjecture

    Let's start with this.
    Startvalue goes in C2 then push button.
    Attached Files Attached Files
    Avoid using Select, Selection and Activate in your code. Use With ... End With instead.
    You can show your appreciation for those that have helped you by clicking the * at the bottom left of any of their posts.

  7. #7
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    Can you explain to me what variable IntSeed is here?

  8. #8
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    OK I figured it out, thank you! There is a mistake with the "E2" value: the number of loops before N goes under the starting point.
    I'll try to fix it!

  9. #9
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    Yes it does work, thank you! that's very nice of you it really helps!

    Now I will try to insert an input box to enter the starting number.

    I think I will put a worksheet function.max(range("A2:?")) to show the max value of n before n goes under the starting number.
    To do that, I use the range between A2 and an unknown cell that I can find using the equiv function I think.

  10. #10
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Collatz conjecture

    There's nothing wrong with that. It reflects the total number of times value goes under startingvalue.

  11. #11
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    ah ok! I expressed myself wrong I need to show how many steps happen before the value goes under starting value.

    So id the series goes:
    339
    1018
    509
    1528
    764
    382
    191
    574
    287
    862
    431
    1294
    647
    1942
    971
    2914
    1457
    4372
    2186
    1093
    3280
    1640
    820
    410
    205
    616
    308
    154
    77
    232
    116
    58
    29
    88
    44
    22
    11
    34
    17
    52
    26
    13
    40
    20
    10
    5
    16
    8
    4
    2
    1

    with a starting value 678, It would be 0.

  12. #12
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Collatz conjecture

    Hi Rudi,

    I added another long variable and altered your loop to ending procedure:
    'The basic logic behind the process here.
    Do While intResult <> 1
        
        If intResult Mod 2 > 0 Then
    'If number is even
        intResult = (intResult * 3) + 1
        c = c + 1
    Else
    'If number is odd
        intResult = intResult / 2
        c = c + 1
    End If
        If intResult < intSeed Then _
        z = z + 1: k = IIf(k = 0, c, k)
        
    'display loop results
    Range("A" & startRow) = intCount: Range("B" & startRow) = intResult
    startRow = startRow + 1: intCount = intCount + 1
    
    Loop
    
    'display summary
    Range("D2") = c
    Range("E2") = z
    Range("F2") = k - 1
    If I've helped you, please consider adding to my reputation - just click on the liitle star at the left.

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~(Pride has no aftertaste.)

    You can't do one thing. XLAdept

    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~aka Orrin

  13. #13
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    And it tells me that
    If n Mod 2 > 0 then
    is false...

  14. #14
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    Thank you!

    I'm really stuck here because I had this code which was working perfectly except for the amount of steps before the value goes under the starting point, but now it won't work anymore and I don't know why...

    Sub collatz()
    
    
    Dim startRow As Double, duree As Double, x As Double
    
    Dim n As Double, ev As Long, z As Long, am As Long
    
    
    'On vide le classeur
    Range("A2:B" & Rows.Count).Value = vbNullString
    
    n = InputBox("Entrez un nombre entier")
    'On donne pour valeur initiale à n le nombre donné par l'utilisateur
    x = n
    
    startRow = 2
    duree = 1 'C'est le début du décompte du nombre d'étapes avant d'obtenir 1
    Range("C2").Value = x
    Range("C1").Value = "Nombre de départ"
    am = 0
    z = 0
        
    'On crée une boucle qui fonctionne tant que n n'a pas atteint 0
    Do While n <> 1
        
        If n Mod 2 > 0 Then
    'Si le nombre est pair
        n = n / 2
        ev = ev + 1
    Else
    'Si le nombre est impair
        n = (n * 3) + 1
        ev = ev + 1
    End If
    
    If x > n Then
        dva = dva + 1
    End If
    
    
    'If n > x And am = duree Then
    'dva = dva + 1
    'End If
    'Range("F1").Value = "durée du vol en altitude"
    'Range("F2") = dva
    
    
    
    
    'On fait apparaitre la valeur à chaque boucle
    Range("A" & startRow) = duree: Range("B" & startRow) = n
    startRow = startRow + 1: duree = duree + 1
    
    'If WorksheetFunction.Max(x, n) > am Then
    'am = WorksheetFunction.Max(x, n)
    'End If
    
    Loop
    
    
    
    'On affiche les réponses
    Range("D1").Value = "Durée du vol"
    Range("D2") = ev
    Range("E1").Value = "Durée du vol en altitude"
    Range("E2") = dva
    Range("F1").Value = "Altitude maximale"
    Range("F2") = am
    
    
    
    End Sub

  15. #15
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Collatz conjecture

    It doesn't accumulate the altitude maximale but this works:

    Sub collatzCandicee()
    
    
    Dim startRow As Double, duree As Double, x As Double
    
    Dim n As Double, ev As Long, z As Long, am As Long
    
    
    'On vide le classeur
    Range("A2:B" & Rows.Count).Value = vbNullString
    
    n = InputBox("Entrez un nombre entier")
    'On donne pour valeur initiale à n le nombre donné par l'utilisateur
    x = n
    
    startRow = 2
    duree = 1 'C'est le début du décompte du nombre d'étapes avant d'obtenir 1
    Range("C2").Value = x
    Range("C1").Value = "Nombre de départ"
    am = 0
    z = 0
        
    'On crée une boucle qui fonctionne tant que n n'a pas atteint 0
    Do While n <> 1
        
        If n Mod 2 > 0 Then
    'Si le nombre est pair
    n = (n * 3) + 1
        ev = ev + 1
    Else
    'Si le nombre est impair
        n = n / 2
        ev = ev + 1
    End If
    
    If x > n Then
        dva = IIf(dva = 0, ev, dva)
    End If
    
    
    'If n > x And am = duree Then
    'dva = dva + 1
    'End If
    'Range("F1").Value = "durée du vol en altitude"
    'Range("F2") = dva
    
    
    
    
    'On fait apparaitre la valeur à chaque boucle
    Range("A" & startRow) = duree: Range("B" & startRow) = n
    startRow = startRow + 1: duree = duree + 1
    
    'If WorksheetFunction.Max(x, n) > am Then
    'am = WorksheetFunction.Max(x, n)
    'End If
    
    Loop
    
    
    
    'On affiche les réponses
    Range("D1").Value = "Durée du vol"
    Range("D2") = ev
    Range("E1").Value = "Durée du vol en altitude"
    Range("E2") = dva
    Range("F1").Value = "Altitude maximale"
    Range("F2") = am
    
    
    
    End Sub

  16. #16
    Registered User
    Join Date
    12-24-2017
    Location
    strasbourg, France
    MS-Off Ver
    15.41 excel
    Posts
    21

    Re: Collatz conjecture

    thank you!

  17. #17
    Forum Guru xladept's Avatar
    Join Date
    04-14-2012
    Location
    Pasadena, California
    MS-Off Ver
    Excel 2003,2010
    Posts
    12,378

    Re: Collatz conjecture

    You're welcome!

  18. #18
    Forum Guru bakerman2's Avatar
    Join Date
    10-03-2012
    Location
    Antwerp, Belgium
    MS-Off Ver
    MSO Home and Business 2024
    Posts
    7,343

    Re: Collatz conjecture

    I think this covers it all.

    Sub collatzCandicee()
    
    Dim startRow As Double, duree As Double, x As Double
    Dim n As Double, ev As Long, z As Long, am As Long
    'On vide le classeur
    Range("A2:B" & Rows.Count).Value = vbNullString
    n = Application.InputBox("Insert startnumber", , , , , , , 1)
    'On donne pour valeur initiale à n le nombre donné par l'utilisateur
    x = n
    flag = False: am = 0
    startRow = 2
    duree = 1 'C'est le début du décompte du nombre d'étapes avant d'obtenir 1
    Range("C2").Value = x
    Range("C1").Value = "Nombre de départ"
    am = 0: z = 0
    'On crée une boucle qui fonctionne tant que n n'a pas atteint 0
    Do While n <> 1
        If n Mod 2 > 0 Then
    'Si le nombre est pair
            n = (n * 3) + 1
            ev = ev + 1
            If flag = False Then: If n > am Then am = n
        Else
    'Si le nombre est impair
            n = n / 2
            ev = ev + 1
            If flag = False Then: If n > am Then am = n
        End If
        If x > n Then
            dva = IIf(dva = 0, ev, dva)
            flag = True
        End If
    'On fait apparaitre la valeur à chaque boucle
        Range("A" & startRow) = duree: Range("B" & startRow) = n
        startRow = startRow + 1: duree = duree + 1
    Loop
    'On affiche les réponses
    Range("D1").Value = "Durée du vol"
    Range("D2") = ev
    Range("E1").Value = "Durée du vol en altitude"
    Range("E2") = dva
    Range("F1").Value = "Altitude maximale"
    Range("F2") = am
    End Sub

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1