sum_of_natural_powers_in_constant_base.pl 460 B

123456789101112131415161718192021222324252627
  1. #!/usr/bin/perl
  2. # Daniel "Trizen" Șuteu
  3. # License: GPLv3
  4. # Date: 17 September 2016
  5. # Website: https://github.com/trizen
  6. # Sum of increasing powers in constant base.
  7. # Example:
  8. # ∑b^i for 0 ≤ i ≤ n == cf(b, n)
  9. #
  10. # where `b` can be any real number != 1.
  11. use 5.010;
  12. use strict;
  13. use warnings;
  14. sub cf {
  15. my ($base, $n) = @_;
  16. ($base ** ($n+1) - 1) / ($base-1);
  17. }
  18. say cf(3, 13);
  19. say cf(-10.5, 4);
  20. say cf(3.1415926535897932384626433832795, 10);