But as a function in some languages. I'll not explain this problem, cuz I'm not a math expert and is a well know math problem, so... More details can be found in the internet 👍
Last active
October 14, 2021 04:52
-
-
Save Miqueas/86f0fd0d4d8e25523a7db5dee569c4f3 to your computer and use it in GitHub Desktop.
The 3x + 1 problem
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
#include <stdlib.h> | |
int problem(int n) { | |
if (n == 1) { | |
printf("End\n"); | |
return n; | |
} else if ((n % 2) == 0) { | |
printf("Even: %d\n", n); | |
return problem(n / 2); | |
} else { | |
printf("Odd: %d\n", n); | |
return problem((n * 3) + 1); | |
} | |
} | |
int main(int argc, char **argv) { | |
problem(atoi(argv[1])); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"flag", | |
"fmt" | |
) | |
func Problem(n int) int { | |
if n == 0 { | |
fmt.Println("End.\n") | |
return n; | |
} else if (n % 2) == 0 { | |
fmt.Printf("Even: %d\n", n) | |
return Problem(n / 2) | |
} else { | |
fmt.Printf("Odd: %d\n", n) | |
return Problem((n * 3) + 1) | |
} | |
} | |
func main() { | |
flag.Parse() | |
Problem(int(flag.Args()[0])) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- The functions takes a positive integer and does the math (prints in time), | |
--- but ends if the number is 1 (to prevent and infinite loop). | |
function Problem(n) | |
if n == 1 then | |
print("End") | |
return n | |
else | |
if n % 2 == 0 then | |
print("Even: " .. tostring(n)) | |
return Problem(n / 2) | |
else | |
print("Odd: " .. tostring(n)) | |
return Problem((n * 3) + 1) | |
end | |
end | |
end | |
Problem(argv[0]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from os import commandLineParams | |
from strutils import parseInt | |
# The functions takes a positive integer and does the math (prints in time), | |
# but ends if the number is 1 (to prevent and infinite loop). | |
proc problem(n: int): int = | |
if n == 1: | |
echo "End" | |
return n | |
elif (n mod 2) == 0: | |
echo "Even: ", n | |
return problem(int(n / 2)) | |
else: | |
echo "Odd: ", n | |
return problem((n * 3) + 1) | |
discard problem(parseInt(commandLineParams()[0])) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import argv | |
# The functions takes a positive integer and does the math (prints in time), | |
# but ends if the number is 1 (to prevent and infinite loop). | |
def Problem(n): | |
if n == 1: | |
print("End") | |
return n | |
elif (n % 2) == 0: | |
print(f"Even: {n}") | |
return Problem(int(n / 2)) | |
else: | |
print(f"Odd: {n}") | |
return Problem((n * 3) + 1) | |
Problem(argv[1]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
int problem(int n) { | |
if (n == 1) { | |
print("End\n"); | |
return n; | |
} else if ((n % 2) == 0) { | |
print(@"Even: $(n)"); | |
return problem(n / 2); | |
} else { | |
print(@"Odd: $(n)"); | |
return problem((n * 3) + 1); | |
} | |
} | |
int main(string[] args) { | |
problem(int.parse(args[1])); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment