Linear Program: for-loops and functions
-
Given integer
nas an input, write a program that determines ifnis a power of 2. In this case, print the power value.Implement a function
func power2(n uint) int, which returns the power value, or-1, ifnis not the power of 2.Example:
power2(8) = 3 -
Given integers
nandkas an input, write a program that determins ifnis a power ofk. In this case, print the power value.Implement a function
func powerK(n, k uint) in, which returns the power value, or-1, ifnis not the power ofK.Example:
powerK(64, 4) = 3 -
Given integer
nas an input, write a program that determines ifnis a prime number.Implement a function
func prime(n uint) uintthat returns0if the number is not prime, and1if it is.Example:
prime(2) = 1 prime(4) = 0 -
Given integer
nas an input, write a program that determines ifnis a perfect square, i.e. there exists an integerk, such that $k^2 = N$.Implement a function
func perfectSquare(n uint) (uint, int). The first value returned from the function is1ifnis the perfect square and, otherwise,0. The second value is the square root ofn, which is relevant only ifnis the square root. In other words, ifnis the perfect square, then(1, square root of n)is returned; otherwise,(0, any value)is returned.Example:
perfectSquare(9) = (1, 3) perfectSquare(8) = (0, 0) -
Given integer
nas an input, write a program that prints the first digit ofn.Write a function
func firstDigit(n int) int.Example:
firstDigit(2147) = 2 -
Given integer
nas an input, write a program that reverses digits inn.Write a function
func reverseNumber(n uint) uint.Example:
reverseNumber(123) = 321 -
Given integer
nas an input, write a program that determines if all digits innare even.Write a function
func allDigitsEven(n int) uint, which returns1, if all digits are even and, otherwise,0.Example:
allDigitsEven(2860) = 1 allDigitsEven(2816) = 0 -
Given integer
nand floating point valuesa,d, whereais the initial term of an arithmetic progression, anddis the common difference of successive members of the arithmetic progression, write a program that calculates thenth term of the arithmetic progression and the sum of the firstnterms.Write a function
func apNthTerm(a float64, d float64, n int) (float64, float64).Example:
apNthTerm(2, 3, 5) = (14, 40) -
Given two integers
xandy, such that1 <= x, y <= 8, which represent a chess board position, write a program that calculates how many moves can a bishop (♗), a queen (♕) and a knight (♔) make from this position.Write three functions
func bishopMoves(x, y uint) uint,func queenMoves(x, y uint64)andfunc knightMoves(x, y uint64).Example:
bishopMoves(1, 1) = 7 queenMoves(1, 1) = 21 knightMoves(1, 1) = 2 -
Given four integers
x1,y1,x2,y2, where(x1, y1)and(x2, y2)represent two position on a chess board, write a program that calculates whether a bishop (♗), a rook (♖), a knight (♘), a king (♔), and a queen (♕) can make a turn from one position to another.Write five functions
func canBishopMove(x1, y1, x2, y2 uint) uint,func canRookMove(x1, y1, x2, y2 uint) uint,func canKnightMove(x1, y1, x2, y2 uint) uint,func canKingMove(x1, y1, x2, y2 uint) uint, andfunc canBQueenMove(x1, y1, x2, y2 uint) uint, which return1if the piece can make a turn between the positions, and0if it cannot.Example:
canBishopMove(1, 1, 3, 3) = 1 canRookMove(1, 1, 3, 3) = 0 canKnightMove(1, 1, 3, 3) = 0 canKingMove(1, 1, 3, 3) = 0 canQueenMove(1, 1, 3, 3) = 1