gaussian_integers_sum.pl 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. #!/usr/bin/perl
  2. # Calculate the terms of the sequences A281964 (real part) and A282132 (imaginary part), using
  3. # Math::AnyNum and Math::GComplex, but without doing any floating-point arithmetic operations.
  4. # See also:
  5. # https://oeis.org/A281964
  6. # https://oeis.org/A282132
  7. # https://en.wikipedia.org/wiki/Gaussian_integer
  8. use 5.020;
  9. use strict;
  10. use warnings;
  11. use experimental qw(signatures);
  12. use Math::GComplex qw(cplx);
  13. use Math::AnyNum qw(:overload factorial);
  14. sub A281964_A282132 ($n) {
  15. my @i = (1, cplx(0, 1), -1, cplx(0, -1));
  16. my $sum = cplx(0, 0);
  17. foreach my $k (1 .. $n) {
  18. $sum += $i[($k - 1) % 4] / $k;
  19. }
  20. return $sum * factorial($n);
  21. }
  22. foreach my $n (1 .. 40) {
  23. printf("%50s %s\n", A281964_A282132($n)->reals);
  24. }
  25. __END__
  26. 1 0
  27. 2 1
  28. 4 3
  29. 16 6
  30. 104 30
  31. 624 300
  32. 3648 2100
  33. 29184 11760
  34. 302976 105840
  35. 3029760 1421280
  36. 29698560 15634080
  37. 356382720 147692160
  38. 5111976960 1919998080
  39. 71567677440 33106993920
  40. 986336870400 496604908800
  41. 15781389926400 6638004172800
  42. 289206418636800 112846070937600
  43. 5205715535462400 2386916704972800
  44. 92506221468057600 45351417394483200
  45. 1850124429361152000 785383247480832000
  46. 41285515024760832000 16493048197097472000
  47. 908281330544738304000 413938002507853824000
  48. 19766469874751373312000 9520574057680637952000
  49. 474395276994032959488000 202641760645450334208000
  50. 12480330326584063426560000 5066044016136258355200000
  51. 324488588491185649090560000 147228354462873703219200000
  52. 8357900428135406889861120000 3975165570497589986918400000
  53. 234021211987791392916111360000 100415766523514167472947200000
  54. 7091503492257664255068733440000 2912057229181910856715468800000
  55. 212745104767729927652062003200000 96203478869197027656007680000000
  56. 6329845387987436698577613619200000 2982307844945107857336238080000000
  57. 202555052415597974354483635814400000 87211012384065528617034055680000000
  58. 6947447566648426683865177994035200000 2877963408674162444362123837440000000
  59. 236213217266046507251416051797196800000 106534073513733409603830404874240000000
  60. 7972229805272023612951943203258368000000 3728692572980669336134064170598400000000
  61. 287000272989792850066269955317301248000000 123899784660917951171159658804019200000000
  62. 10991003427412236669919987794890981376000000 4584292032453964193332907375748710400000000
  63. 417658130241664993456959536205857292288000000 187966850324476984392966459860031897600000000
  64. 15765644461958333633061414687928360108032000000 7330707162654602391325691934541244006400000000
  65. 630625778478333345322456587517134404321280000000 272830404424986652294387395641746862899200000000