-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfibonnaci.go
More file actions
64 lines (51 loc) · 1.75 KB
/
fibonnaci.go
File metadata and controls
64 lines (51 loc) · 1.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package main
/*
#include <stdlib.h>
*/
import "C" // Importing the C package to use C code features
import (
"fmt" // Importing the unsafe package to work with memory pointers
"unsafe"
)
// Importing fmt for printing
// Fibonacci is a C-exported function that calculates the Fibonacci sequence
// up to n terms and returns a pointer to an array of integers.
//
//export Fibonacci
func Fibonacci(n C.int) *C.int {
// Allocate memory for the result array in C
// The size of the memory allocated is n * size of int
result := C.malloc(C.size_t(n) * C.size_t(unsafe.Sizeof(C.int(0))))
// Typecast the result pointer to a Go slice to populate it
// This allows us to treat the allocated memory as an array of C.int
resultSlice := (*[1 << 30]C.int)(result)[:n:n]
// Initialize the Fibonacci sequence
if n > 0 {
resultSlice[0] = 0 // The first Fibonacci number is 0
}
if n > 1 {
resultSlice[1] = 1 // The second Fibonacci number is 1
}
// Calculate the Fibonacci sequence for the remaining terms
for i := 2; i < int(n); i++ {
resultSlice[i] = resultSlice[i-1] + resultSlice[i-2] // Each number is the sum of the two preceding ones
}
// Return a pointer to the C array
return (*C.int)(result) // The caller is responsible for freeing this memory
}
func main() {
// Define the number of Fibonacci terms you want
n := 10
// Call the Fibonacci function
result := Fibonacci(C.int(n))
// Typecast the result pointer back to a Go slice for printing
resultSlice := (*[1 << 30]C.int)(unsafe.Pointer(result))[:n:n]
// Print "This is the Fibonacci sequence"
fmt.Println("This is the Fibonacci sequence:")
// Print the Fibonacci sequence
for _, value := range resultSlice {
fmt.Println(value)
}
// Free the allocated C memory
C.free(unsafe.Pointer(result))
}