Skip to content

Instantly share code, notes, and snippets.

@Arnaud-Chevalier
Last active January 12, 2021 14:20
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Arnaud-Chevalier/e4dc659cddaad3533287c68169da8a09 to your computer and use it in GitHub Desktop.
Save Arnaud-Chevalier/e4dc659cddaad3533287c68169da8a09 to your computer and use it in GitHub Desktop.
#Function to calculate the Collatz conjecture
Function Calc ($number) {
#If number is even divide by 2
if ($number % 2 -eq 0){
$result = $number / 2
[void]$myArray.Add($result)
#Else (number is odd) multiply by 3 and add 1
}Else{
$result = $number * 3 + 1
[void]$myArray.Add($result)
}
#If result is not 1 Callback the function (recursive)
if ($result -ne 1){
Calc $result
#If number 1 is reached stop the Callback
}Else{write-host "Number 1 is reached"
#Show every results
write-host "List of steps : "
$myarray
#Measure how many results to reach 1
$flighttime = $myarray.Count
write-host "Flight Time = " $flighttime
#Get the biggest number in the results
$max = $myarray | measure -Maximum
write-host "Maximum altitude : " $max.maximum
write-host ""
}
}
#Values to calculate (numbers)
$numbers = 1..10
#Iterate on each numbers
foreach ($number in $numbers) {
#Reset the array of results
$myarray = [System.Collections.ArrayList]::new()
write-host -f green "Start calculus with number : " $number
#Launch function
Calc $number
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment