You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
functenth() {
// 10! = 1*2*3*4*5*6*7*8*9*10// don't be stupid like me to use zero because 0 * everything = zer0varmultipleint=1fori:=1; i<=10; i++ {
multiple*=iprintln(multiple)
}
}
7. Calculate the sum of even numbers greater than 10 and less than 30
15. Create a function that will find the nth Fibonacci number using recursion
funcnthFibonacciNumber(numberint) int {
ifnumber<=0 {
return0
} elseifnumber==1 {
return1
}
// based on INDEXvarpreviousint=0varcurrentint=1// starting from 2fori:=2; i<=number; i++ {
// changing placesvarnextint=previous+currentprevious=currentcurrent=next
}
fmt.Printf("The %dth Fibonacci number is: %d\n", number, current)
returncurrent
}
16. Create a function that will return a Boolean specifying if a number is prime
funcprimeNumberOrNot(numberint) bool {
// a prime number is a number that is only divisible by 1 and itself, without any remainder.ifnumber<2 {
returnfalse
}
fori:=2; i<int(sqrt(float64(number))); i++ {
ifnumber%i==0 {
fmt.Printf("%d is not a prime number.\n", number)
returnfalse
}
}
fmt.Printf("%d is a prime number.\n", number)
returntrue
}
funcsqrt(xfloat64) float64 {
ifx==0 {
return0
}
// Initial guessguess:=x/2// Iterate until desired precision is achievedfori:=0; i<10; i++ {
guess= (guess+x/guess) /2
}
returnguess
}
17. Calculate the sum of digits of a positive integer number
funcsumOfDigitsOfPositiveInteger(numberint) int {
varsumint=0// just to print the entered numbervaroriginalNumberint=numberfornumber>0 {
// the last valuevardigitint=number%10// assign that value for sumsum+=digit// until reach zeronumber=number/10
}
fmt.Printf("Sum of digits of %d is: %d\n", originalNumber, sum)
returnsum
}
18. Print the first 100 prime numbers
funcfirstHundredPrimeNumbers(numberint) []int {
// Algorithm := Sieve Of Eratosthenesvarprime []bool=make([]bool, number+1)
// all are primesfori:=0; i<=number; i++ {
prime[i] =true
}
// starting from 2 other are not prime 0, 1varprimeDigitint=2forprimeDigit*primeDigit<=number {
ifprime[primeDigit] ==true {
// eliminating the multiple of twoforindex:=primeDigit*primeDigit; index<=number; index+=primeDigit {
// each index which is not multiple is eliminatedprime[index] =false
}
}
// moving forwardprimeDigit++
}
// adding to slice if the prime is true starting from zeroprimes:= []int{}
fori:=2; i<=number; i++ {
ifprime[i] ==true {
primes=append(primes, i)
}
}
// First Hundred Prime Numbers - You can use even thousand or millions LOLfori:=0; i<=len(primes); i++ {
println("Prime:", primes[i])
}
returnprimes
}
19. Create a function that will return in an array the first “p” prime numbers greater than “n”
funcisPrime(numberint) bool {
ifnumber<=1 {
returnfalse
}
ifnumber==2||number==3 {
returntrue
}
ifnumber%2==0||number%3==0 {
returnfalse
}
variint=5varwint=2fori*i<=number {
ifnumber%i==0 {
returnfalse
}
i+=ww=6-w
}
returntrue
}
funcprimeNumberGreaterThanN(array []int, nint) int {
for_, num:=rangearray {
ifnum>n&&isPrime(num) {
fmt.Printf("The first prime number greater than %d in the array is: %d\n", n, num)
returnnum
}
}
fmt.Printf("No prime number greater than %d is found in the array.\n", n)
return-1
}
20. Rotate an array to the left 1 position
funcrotateAnArrayLeftOnePosition(array []int) {
fmt.Println("Original Array", array)
// first valuevarfirstint=array[0]
// shifting to the left whole arrayforindex:=1; index<len(array); index++ {
array[index-1] =array[index]
}
// assigning last to firstarray[len(array)-1] =firstfmt.Println("Rotated Array:", array)
}
21. Rotate an array to the right 1 position
funcrotateAnArrayRightOnePosition(array []int) {
fmt.Println("Original Array", array)
// last valuevarlastint=array[len(array)-1]
// index shifting from backwards index--forindex:=len(array) -1; index>0; index-- {
array[index] =array[index-1]
}
// assigning first to lastarray[0] =lastfmt.Println("Rotated Array:", array)
}
25. Create a function that will receive two arrays of numbers as arguments and return an array composed of all the numbers that are either in the first array or second array but not in both
26. Create a function that will receive two arrays and will return an array with elements that are in the first array but not in the second
funcuniqueElements(array1 []int, array2 []int) []int {
result:= []int{}
elements:=make(map[int]bool)
// Store all elements from array1 in the mapfor_, element:=rangearray1 {
elements[element] =true
}
// Check each element in array2 and add it to the result if it's not in the mapfor_, element:=rangearray2 {
if_, ok:=elements[element]; !ok {
result=append(result, element)
}
}
returnresult
}
27. Create a function that will receive an array of numbers as argument and will return a new array with distinct elements
30. Create a function that will add two positive numbers of indefinite size. The numbers are received as strings and the result should be also provided as string.
40. Create a function to calculate the distance between two points defined by their x, y coordinates
funcdistanceBetweenTwoPoints(x1, y1, x2, y2float64) float64 {
distance:=sqrt((x2-x1)*(x2-x1) + (y2-y1)*(y2-y1))
fmt.Printf("The distance between the two points is %v", distance)
returndistance
}
41. Create a function that will return a Boolean value indicating if two circles defined by center coordinates and radius are intersecting
42. Create a function that will receive a bi-dimensional array as argument and a number and will extract as a UniDimensional array the column specified by the number
44. Create a function to calculate the sum of all the numbers in a jagged array (contains numbers or other arrays of numbers on an unlimited number of levels)
47. Create a function to return the longest word in a string
funclongestWord(strstring) string {
longestWord:=""currentWord:=""maxLength:=0for_, char:=rangestr {
ifisAlphanumeric(char) {
currentWord+=string(char)
} else {
iflen(currentWord) >maxLength {
longestWord=currentWordmaxLength=len(currentWord)
}
currentWord=""
}
}
// Check the last word in case the string ends without a non-alphanumeric characteriflen(currentWord) >maxLength {
longestWord=currentWord
}
fmt.Printf("Longest Word: %v", longestWord)
returnlongestWord
}
funcisAlphanumeric(charrune) bool {
return (char>='a'&&char<='z') ||
(char>='A'&&char<='Z') ||
(char>='0'&&char<='9')
}
48. Shuffle an array of strings
funcshuffleString(arr []string) {
fmt.Println("Original array:", arr)
n:=len(arr)
// Seed value for randomizationseed:=1fori:=n-1; i>0; i-- {
// Generate a random index between 0 and i (inclusive)seed= (seed*1103515245+12345) % (1<<31)
j:=seed% (i+1)
arr[i], arr[j] =arr[j], arr[i]
}
fmt.Println("Original array:", arr)
}
49. Create a function that will receive n as argument and return an array of n random numbers from 1 to n. The numbers should be unique inside the array.
funcrandomNumbers(nint) []int {
// Create a slice to store the generated numbersnumbers:=make([]int, n)
// Initialize the slice with numbers from 1 to nfori:=0; i<n; i++ {
numbers[i] =i+1
}
// Shuffle the slice to randomize the order of the numbersseed:=1fori:=n-1; i>0; i-- {
// Generate a random index between 0 and i (inclusive)seed= (seed*1103515245+23545) % (1<<31)
j:=seed% (i+1)
numbers[i], numbers[j] =numbers[j], numbers[i]
}
fmt.Printf("Random Numbers: %d", numbers)
returnnumbers
}
50. Find the frequency of letters inside a string. Return the result as an array of arrays. Each subArray has 2 elements: letter and number of occurrences.
funcletterFrequency(strstring) [][]interface{} {
frequency:=make(map[rune]int)
// Count the occurrences of each letterfor_, char:=rangestr {
frequency[char]++
}
// Convert the frequency map to an array of arraysresult:=make([][]interface{}, 0, len(frequency))
forchar, count:=rangefrequency {
subArray:= []interface{}{string(char), count}
result=append(result, subArray)
}
fmt.Printf("Frequency: %d", result)
returnresult
}
51. Calculate Fibonacci(500) with high precision (all digits)
funcfibonacci(nint) string {
a:="0"b:="1"fori:=2; i<=n; i++ {
// Perform additionsum:=add(a, b)
// Update a and b for the next iterationa=bb=sum
}
fmt.Printf("Fibonacci Of %v is %v", n, a)
returna
}
// Custom addition function to handle large numbersfuncadd(a, bstring) string {
// Pad zeros to align the numbersmaxLen:=len(a)
iflen(b) >maxLen {
maxLen=len(b)
}
a=padZeros(a, maxLen)
b=padZeros(b, maxLen)
// Perform digit-wise additioncarry:=0result:=make([]byte, maxLen)
fori:=maxLen-1; i>=0; i-- {
digitA:=int(a[i] -'0')
digitB:=int(b[i] -'0')
sum:=digitA+digitB+carryresult[i] =byte(sum%10+'0')
carry=sum/10
}
// Handle any remaining carryifcarry>0 {
result=append([]byte{byte(carry+'0')}, result...)
}
returnstring(result)
}
// Custom function to pad zeros to the left of a stringfuncpadZeros(strstring, lengthint) string {
forlen(str) <length {
str="0"+str
}
returnstr
}
52. Calculate 70! with high precision (all digits)
funcfactorial(nuint64) string {
ifn==0 {
return"1"
}
fact:=make([]byte, 1, 3*n/2) // Allocate an initial capacity for the resultfact[0] =1fori:=uint64(2); i<=n; i++ {
fact=multiply(fact, i)
}
// Convert the result to a stringresult:=""for_, digit:=rangefact {
result=string(digit+'0') +result
}
fmt.Printf("Factorial of %v is %v", n, result)
returnresult
}
// Custom multiplication function to handle large numbersfuncmultiply(num []byte, multiplieruint64) []byte {
carry:=byte(0)
fori:=0; i<len(num); i++ {
product:=num[i]*byte(multiplier) +carrynum[i] =product%10carry=product/10
}
forcarry>0 {
num=append(num, carry%10)
carry/=10
}
returnnum
}