Solutions for Muetzilla's Advent Calendar
Content
Problem 1
Hello World in a new Language: Go
package main
func main() {
println("Hello World!")
}
Problem 2
Calculate the LCM from numbers 1 - 10
- The Function
size_t lcm(size_t a, size_t b)
takes 2 numbers as parameters. - The Bigger number of the two is used as a start.
- Then the program counts up until the number is divisible by both numbers.
size_t lcm(size_t a, size_t b) {
int max = (a > b) ? a : b;
while (1) {
if (max % a == 0 && max % b == 0) {
return max;
}
++max;
}
}
Problem 3
Calculate the GCD of all numbers from 1 - 10
- The Function
private int findGCD(int a, int b)
takes 2 numbers as parameters. - This Function will be called recursively with the args
(b, a % b)
untilb == 0
anda
is returned.
private int findGCD(int a, int b) {
if (b == 0) return a;
return findGCD(b, a % b);
}
Problem 4
Create a Calculator using Python which can do (+-*/) operations.
- First the calculation is split into it's parts (numbers & operators)
- 15 + 53 * 10 =>
['15', '+', '53', '*', '10']
- 15 + 53 * 10 =>
- Then the operations * and / are performed
def point_calc(calc):
=> ['15', '+', '530']
- Finally the operations + and - are performed
def dash_calc(calc):
=> [545]
split = split_calculation(calculation)
try:
after_point = point_calc(split)
after_dash = dash_calc(after_point)
print(f"The result is: {after_dash}")
except:
print("The calculation you entered is not Valid!")
Problem 5
Make a Program to calculate the circumference and area of a Shape
Enum with functions for shapes enum class Shape(val circumerence: () -> Double, val area: () -> Double)
Problem 6
Create a Program in any JVM Language, which takes user input and converts it to the nth Primenumber
Language used: Scala
private def isPrime(n: Int): Boolean = {
for (i <- 2 to (n / 2)) {
if (n % i == 0) return false
}
true
}
How To Run The Code
- Go
- Install Golang on your local machine
- Run the following command in a terminal:
go run .\ProblemXX.go
- C
- Install a C compiler (Ex. GCC)
- Run the following command:
gcc ProblemXX.c -o ProblemXX
- Run the created .exe / .o file
- Java
- Install JDK & JRE
- Run the following commands in a terminal:
javac .\ProblemXX.java; & java ProblemXX
- Python
- Install Python on your system
- Run the following command:
python ProblemXX.py
- Kotlin
- Install Kotlin
- Run the following command:
kotlinc ProblemXX.kt -include-runtime -d ProblemXX.jar
- To Run the Problem, run:
java -jar ProblemXX.kt
- Scala
- Install Scala
- Run the following commands:
- Compile:
scalac ProblemXX.scala
- Run:
scala ProblemXX
- Compile: