From d23d573472278529d986ac816a8f7f794f8d69bf Mon Sep 17 00:00:00 2001 From: Vincent Dolez Date: Mon, 26 Nov 2018 11:39:15 +0100 Subject: [PATCH] feat: add a recursive implementation of syracuse conjecture --- go100/09-exercices/correction/main.go | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/go100/09-exercices/correction/main.go b/go100/09-exercices/correction/main.go index affa2a8..cf5af36 100644 --- a/go100/09-exercices/correction/main.go +++ b/go100/09-exercices/correction/main.go @@ -2,6 +2,22 @@ package main import "fmt" +func realSyracuse(val, currentItern, maxIter int) (bool, int) { + if val == 1 { + return true, currentItern + } else if currentItern >= maxIter { + return false, currentItern + } else if val % 2 == 0 { + return realSyracuse(val / 2, currentItern+1, maxIter) + } else { + return realSyracuse(3*val + 1, currentItern+1, maxIter) + } +} + +func recursiveSyracuse(val, nbMaxIteration int) (bool, int) { + return realSyracuse(val, 0, nbMaxIteration) +} + //syracuse prend en paramètre un entier et un nombre max d'itérations à effectuer. //Elle retourne un booléen indiquant si le nombre 1 a été atteint, suivi du nombre d'itérations qui ont été effectuées. func syracuse(start int, maxIterations int) (bool, int) {