Write a function that takes a positive integer n, sums all the cubed values from 1 to n (inclusive), and returns that sum.
Assume that the input n will always be a positive integer.
Examples: (Input --> output)
2 --> 9 (sum of the cubes of 1 and 2 is 1 + 8)
3 --> 36 (sum of the cubes of 1, 2, and 3 is 1 + 8 + 27)
import "package:test/test.dart";
import "package:solution/solution.dart";
void main() {
group('Fixed tests', () {
test('Testing for 1', () => expect(sumCubes(1), equals(1)));
test('Testing for 2', () => expect(sumCubes(2), equals(9)));
test('Testing for 3', () => expect(sumCubes(3), equals(36)));
test('Testing for 4', () => expect(sumCubes(4), equals(100)));
test('Testing for 10', () => expect(sumCubes(10), equals(3025)));
test('Testing for 123', () => expect(sumCubes(123), equals(58155876)));
test('Testing for 77000', () => expect(sumCubes(77000), equals(8788488517982250000)));
});
}
import 'dart:math';
int sumCubes(int n) {
int total = 0;
for (int i = 1; i <= n; i++) {
total += pow(i, 3).toInt();
}
return total;
}
import 'dart:math' as math;
int sumCubes(int n) {
return List.generate(n,(index)=>math.pow(index+1,3)).reduce((n1,n2)=>n1+n2);
}