Write a script that reads the current time and converts it to a time of day in hours, minutes, and seconds, plus the number of days since the epoch.
In :function recurse(n, s) if n == 0 println(s) else recurse(n-1, n+s) end end recurse(3, 0)
In :function draw(t, length, n) if n == 0 return end angle = 50 Forward(t, length*n) Turn(t, angle) draw(t, length, n-1) Turn(t, -2*angle) draw(t, length, n-1) Turn(t, angle) Forward(t, -length*n) end
Out:draw (generic function with 1 method)
In :using Luxor 🐢 = Turtle() @svg begin draw(🐢, 5, 1) end
The Koch curve is a fractal (see https://en.wikipedia.org/wiki/Koch_snowflake). To draw a Koch curve with length
x, all you have to do is
The exception is if
x is less than 3: in that case, you can just draw a straight line with length
Write a function called
koch that takes a turtle and a length as parameters, and that uses the turtle to draw a Koch curve with the given length.
Write a function called
snowflake that draws three Koch curves to make the outline of a snowflake.
Fermat’s Last Theorem says that there are no positive integers $a$, $b$, and $c$ such that $$a^n + b^n = c^n$$ for any values of $n$ greater than 2.
check_fermatthat takes four parameters—
n—and checks to see if Fermat’s theorem holds. If
nis greater than 2 anWrite a function named
check_fermatthat takes four parameters—a, b, c and n—and checks to see if Fermat’s theorem holds. If n is greater than 2 and $$a^n + b^n = c^n$$ the program should print,
“Holy smokes, Fermat was wrong!”Otherwise the program should print,
“No, that doesn’t work.”
n, converts them to integers, and uses
check_fermatto check whether they violate Fermat’s theorem.
If you are given three sticks, you may or may not be able to arrange them in a triangle. For example, if one of the sticks is 12 cm long and the other two are one cm long, you will not be able to get the short sticks to meet in the middle. For any three lengths, there is a simple test to see if it is possible to form a triangle:
If any of the three lengths is greater than the sum of the other two, then you cannot form a triangle. Otherwise, you can. (If the sum of two lengths equals the third, they form what is called a “degenerate” triangle.)
Write a function named
is_triangle that takes three integers as arguments, and that prints either
“No”, depending on whether you can or cannot form a triangle from sticks with the given lengths.
Write a function that prompts the user to input three stick lengths, converts them to integers, and uses
is_triangle to check whether sticks with the given lengths can form a triangle.