Create a reusable Dart package called math_utils that provides mathematical functions. You'll learn package creation, exports, and how to structure a library properly.
Requirements
Create a package using dart create -t package math_utils
Implement functions in lib/src/ (private implementation)
Export only the public API from lib/math_utils.dart
Include functions for: factorial, isPrime, gcd (greatest common divisor), lcm (least common multiple), and fibonacci
Create a test application that imports and uses your package
// ============================================
// FILE: math_utils/lib/src/validation.dart
// ============================================
/// Validation utility functions for mathematical operations.
library;
/// Checks if a number is prime.
/// Returns true if [n] is a prime number, false otherwise.
bool isPrime(int n) {
if (n <= 1) return false;
if (n <= 3) return true;
if (n % 2 == 0 || n % 3 == 0) return false;
for (int i = 5; i * i <= n; i += 6) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
}
return true;
}
/// Checks if a number is even.
bool isEven(int n) => n % 2 == 0;
/// Checks if a number is odd.
bool isOdd(int n) => n % 2 != 0;
/// Checks if a number is a perfect square.
bool isPerfectSquare(int n) {
if (n < 0) return false;
int sqrt = n.toDouble().sqrt().round();
return sqrt * sqrt == n;
}
// ============================================
// FILE: math_utils/lib/src/arithmetic.dart
// ============================================
/// Core arithmetic operations and calculations.
library;
/// Calculates the factorial of [n].
/// Throws [ArgumentError] if n is negative.
int factorial(int n) {
if (n < 0) {
throw ArgumentError('Factorial is not defined for negative numbers.');
}
if (n <= 1) return 1;
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
/// Finds the greatest common divisor of [a] and [b]
/// using the Euclidean algorithm.
int gcd(int a, int b) {
a = a.abs();
b = b.abs();
while (b != 0) {
int temp = b;
b = a % b;
a = temp;
}
return a;
}
/// Finds the least common multiple of [a] and [b].
int lcm(int a, int b) {
if (a == 0 || b == 0) return 0;
return (a * b).abs() ~/ gcd(a, b);
}
/// Calculates [base] raised to the power of [exponent].
num power(num base, int exponent) {
if (exponent == 0) return 1;
if (exponent < 0) return 1 / power(base, -exponent);
num result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
// ============================================
// FILE: math_utils/lib/src/sequences.dart
// ============================================
/// Sequence generators for mathematical series.
library;
/// Generates the first [n] Fibonacci numbers.
List fibonacci(int n) {
if (n <= 0) return [];
if (n == 1) return [0];
if (n == 2) return [0, 1];
List sequence = [0, 1];
for (int i = 2; i < n; i++) {
sequence.add(sequence[i - 1] + sequence[i - 2]);
}
return sequence;
}
/// Generates prime numbers up to [max].
List generatePrimes(int max) {
List primes = [];
for (int i = 2; i <= max; i++) {
if (_isPrimeSimple(i)) {
primes.add(i);
}
}
return primes;
}
bool _isPrimeSimple(int n) {
if (n < 2) return false;
for (int i = 2; i <= n ~/ 2; i++) {
if (n % i == 0) return false;
}
return true;
}
/// Generates a list of divisors for [n].
List divisors(int n) {
if (n == 0) return [];
List result = [];
n = n.abs();
for (int i = 1; i <= n; i++) {
if (n % i == 0) {
result.add(i);
}
}
return result;
}
// ============================================
// FILE: math_utils/lib/math_utils.dart
// ============================================
/// A comprehensive math utilities package.
///
/// Provides functions for arithmetic, sequences, and validation.
library math_utils;
// Export only the public API
export 'src/arithmetic.dart';
export 'src/sequences.dart';
export 'src/validation.dart';
// ============================================
// FILE: math_utils/pubspec.yaml
// ============================================
name: math_utils
description: A reusable math utilities package.
version: 1.0.0
environment:
sdk: ^3.12.0
dev_dependencies:
test: ^1.24.0
lints: ^5.0.0
// ============================================
// FILE: test_app/bin/main.dart (Consumer app)
// ============================================
import 'package:math_utils/math_utils.dart';
void main() {
print('āāāāāāāāāāāāāāāāāāāāāāāāāāāā');
print('ā Math Utils Demo ā');
print('āāāāāāāāāāāāāāāāāāāāāāāāāāāā\n');
// Test factorial
print('š Factorial:');
for (int i = 0; i <= 10; i++) {
print(' $i! = ${factorial(i)}');
}
// Test prime checking
print('\nš¢ Prime Numbers:');
final testNumbers = [2, 3, 4, 17, 21, 97, 100];
for (var n in testNumbers) {
print(' $n is prime? ${isPrime(n)}');
}
// Test GCD and LCM
print('\nš GCD and LCM:');
print(' gcd(48, 18) = ${gcd(48, 18)}');
print(' lcm(12, 18) = ${lcm(12, 18)}');
print(' gcd(100, 75) = ${gcd(100, 75)}');
print(' lcm(15, 20) = ${lcm(15, 20)}');
// Test Fibonacci
print('\nš Fibonacci Sequence:');
print(' First 10: ${fibonacci(10)}');
// Test divisors
print('\nš Divisors:');
print(' Divisors of 28: ${divisors(28)}');
print(' Divisors of 36: ${divisors(36)}');
// Test power
print('\nšŖ Power:');
print(' 2^10 = ${power(2, 10)}');
print(' 3^5 = ${power(3, 5)}');
print(' 10^-2 = ${power(10, -2)}');
// Statistics
print('\nš Statistics:');
final numbers = [12, 45, 67, 23, 89, 34, 56];
print(' Numbers: $numbers');
print(' Even count: ${numbers.where(isEven).length}');
print(' Odd count: ${numbers.where(isOdd).length}');
print(' Perfect squares: ${numbers.where(isPerfectSquare).toList()}');
}