This is an over-engineered Factorial desktop app for Windows.
It's available on here
It features both a Desktop App and an API at the same time
- 📖 Main Functionalities
- 🛠️ How it works?
- 🖥️ Desktop App
- 🌌 API
- 🚀 Deploy with Fyne-Cross on Windows
- 📦 Project Installation
- Single number factorial calculation
- Range number factorial calculation
- Concurrent factorial calculus
- Usage of BigInt in factorial calculus to calculate big numbers
- Usage of Sqlite3 database to store results and retrieve them later
- Export to CSV
- Fyne Desktop App
- Light/Dark Mode
- Txt formatted logs with levels
- Fully functional local API
The factorial calculation in this application is designed to be efficient
and scalable, leveraging Go's concurrency features to handle large numbers.
The core logic is implemented in the Factorial
function within the logic
package.
The Factorial
function computes the factorial of a given number using concurrent goroutines.
This approach divides the computation into smaller chunks, which are then processed in parallel,
significantly improving performance for large inputs.
- Input Validation:
- If the input number is less than or equal to 1, the function immediately returns
1
as the factorial of such numbers.
- Goroutine Allocation:
- The number of goroutines is determined based on the input number. By default, it uses 4 goroutines. If the input number is less than 4, it adjusts the number of goroutines to match the input number.
- Chunk Division:
- The input number is divided into chunks, each assigned to a separate goroutine. The size of each chunk is calculated by dividing the input number by the number of goroutines.
- Concurrent Calculation:
- Each goroutine calculates the factorial of its assigned chunk concurrently. The results are sent to a shared channel (
results
).
- Result Aggregation:
- After all goroutines complete their calculations, the main function reads from the
results
channel and multiplies the partial results to obtain the final factorial.
package logic
import (
"math/big"
"sync"
)
func Factorial(number int) *big.Int {
if number <= 1 {
return big.NewInt(1)
}
numGoroutines := 4
if number < numGoroutines {
numGoroutines = number
}
results := make(chan *big.Int, numGoroutines)
var wg sync.WaitGroup
wg.Add(numGoroutines)
chunkSize := number / numGoroutines
for i := 0; i < numGoroutines; i++ {
start := i*chunkSize + 1
end := (i + 1) * chunkSize
if i == numGoroutines-1 {
end = number
}
go func(start, end int) {
defer wg.Done()
partial := big.NewInt(1)
for j := start; j <= end; j++ {
partial.Mul(partial, big.NewInt(int64(j)))
}
results <- partial
}(start, end)
}
go func() {
wg.Wait()
close(results)
}()
result := big.NewInt(1)
for partial := range results {
result.Mul(result, partial)
}
return result
}
- Concurrency: Utilizes Go's concurrency model to divide the workload and process it in parallel.
- Efficiency: By breaking down the problem into smaller parts, the function can handle large inputs more efficiently.
- Scalability: The number of goroutines can be adjusted based on the input size, ensuring optimal performance.
The Desktop App is a feature-rich application designed to provide users with a seamless experience for calculating factorials and managing factorial results. Built using the Fyne toolkit in Go, it offers a modern and intuitive user interface. The app integrates with a backend API to handle calculations and data storage, ensuring efficient performance and reliability.
- Factorial Calculation:
- Users can input a number to calculate its factorial.
- The app leverages concurrent processing to handle large numbers efficiently.
- Result Management:
- Users can view a list of previously calculated factorial results.
- Results can be deleted or exported to a CSV file.
- Range Calculation:
- Users can specify a range of numbers to calculate factorials for all numbers within that range.
- User-Friendly Interface:
- The app provides a clean and intuitive interface, making it easy for users to navigate and interact with the functionalities.
- Launch the App:
- Run the compiled binary to start the Desktop App.
- Calculate Factorial:
- Enter a number in the input field and click "Calculate" to compute the factorial.
- View Results:
- Click on the "Results" tab to view a list of previously calculated factorials.
- Manage Results:
- Use the "Delete" button to clear all results.
- Use the "Export" button to save results to a CSV file.
- Calculate Range:
- Enter the lower and upper bounds of a range and click "Calculate Range" to compute factorials for all numbers within the range.
- Home Screen
-
Common Issues:
- API Connection: Ensure the API server is running and accessible.
- Permissions: : Ensure that the application has been granted the requisite permissions to write files, which is essential for exporting results. Additionally, verify that the API has the necessary internet permissions to facilitate seamless communication with the backend services. Lastly, confirm that the application is permitted to execute on your Windows device, adhering to any applicable security policies and user settings.
-
Support:
- For any issues or feature requests, please open an issue on the GitHub repository.
The API provides a set of endpoints to interact with the
Factorial App, allowing users to retrieve, delete, and export
factorial results, as well as calculate factorials and factorial ranges.
The API is built using Go and runs on localhost:2999
.
- URL:
/results
- Method:
GET
- Description: Retrieves all stored factorial results from the database.
- Response:
- 200 OK: Returns a JSON array of factorial results.
- 500 Internal Server Error: If there is an error retrieving the results.
- URL:
/delete
- Method:
DELETE
- Description: Deletes all stored factorial results from the database.
- Response:
- 200 OK: Returns a confirmation message.
- 500 Internal Server Error: If there is an error deleting the results.
- URL:
/export
- Method:
GET
- Description: Exports all stored factorial results to a CSV file.
- Response:
- 200 OK: Returns a confirmation message and creates a
results.csv
file. - 500 Internal Server Error: If there is an error exporting the results.
- 200 OK: Returns a confirmation message and creates a
- URL:
/factorial
- Method:
GET
- Description: Calculates the factorial of a given number.
- Query Parameters:
number
: The number for which to calculate the factorial.
- Response:
- 200 OK: Returns the factorial result as a JSON string.
- 400 Bad Request: If the provided number is invalid.
- 500 Internal Server Error: If there is an error calculating the factorial.
- URL:
/range
- Method:
GET
- Description: Calculates the factorials of a range of numbers.
- Query Parameters:
lower
: The lower bound of the range.upper
: The upper bound of the range.
- Response:
- 200 OK: Returns a confirmation message.
- 400 Bad Request: If the provided range is invalid.
- 500 Internal Server Error: If there is an error calculating the factorials.
- Concurrency: Utilizes Go's concurrency model to handle multiple requests efficiently.
- Error Handling: Provides detailed error messages and status codes for better debugging and user feedback.
- Data Serialization: Uses JSON and CSV formats for data exchange, ensuring compatibility and ease of use.
The app is built on windows with for arm64 arq, and it's available here
- Prerequisites:
- Go (version 1.19 or higher)
- Fyne toolkit
-
Clone the Repository:
git clone https://github.com/EduardoProfe666/Factorial-App.git cd Factorial-App
-
Build the App:
go build -o factorial-app
-
Run the App:
./factorial-app