spanish_numbers.pl 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. #!/usr/bin/perl
  2. # a(n) is the smallest number such that with the letters of the name of that number we can spell the name of n numbers smaller than a(n) in Spanish.
  3. # https://oeis.org/A317423
  4. # First 50 terms:
  5. # 16, 18, 23, 34, 44, 45, 42, 84, 128, 116, 54, 133, 132, 159, 196, 136, 134, 124, 250, 145, 144, 149, 261, 153, 143, 148, 147, 154, 142, 263, 275, 273, 146, 252, 233, 269, 278, 236, 369, 224, 286, 237, 238, 574, 241, 242, 398, 257, 258, 353
  6. use 5.010;
  7. use strict;
  8. use warnings;
  9. use Lingua::SPA::Numeros;
  10. my (%c1, %c2);
  11. my $obj = Lingua::SPA::Numeros->new(GENERO => 'o', ACENTOS => 0);
  12. sub count_letters {
  13. my ($n) = @_;
  14. return $c1{$n} if exists($c1{$n});
  15. my $s = $obj->cardinal($n);
  16. $s =~ tr/a-z//dc;
  17. my %h; ++$h{$_} for split(//, $s); $c1{$n} = \%h;
  18. }
  19. sub check_letters {
  20. my ($n, $k) = @_;
  21. my $key = "$n $k";
  22. return $c2{$key} if exists($c2{$key});
  23. my $h = count_letters($n);
  24. my $t = count_letters($k);
  25. foreach my $k (keys %$t) {
  26. if (not(exists($h->{$k}) and $h->{$k} >= $t->{$k})) {
  27. $c2{$key} = 0;
  28. return 0;
  29. }
  30. }
  31. $c2{$key} = 1;
  32. return 1;
  33. }
  34. sub a {
  35. my ($i) = @_;
  36. for (my $n = 1 ; ; ++$n) {
  37. my $c = 0;
  38. my @numbers;
  39. foreach my $k (1 .. $n - 1) {
  40. if (check_letters($n, $k)) {
  41. push @numbers, [$k, $obj->cardinal($k)];
  42. last if ++$c > $i;
  43. }
  44. }
  45. if ($c == $i) {
  46. say "a($i) = $n, where the letters of $n (", $obj->cardinal($n), ") can form the following numbers:\n\t",
  47. join("\n\t", map { "$_->[0] -> $_->[1]" } @numbers), "\n";
  48. }
  49. return $n if ($c == $i);
  50. }
  51. }
  52. my @terms;
  53. foreach my $n (1 .. 50) {
  54. my $t = a($n);
  55. #print "a($n) = $t\n";
  56. push @terms, $t;
  57. }
  58. print "\nFirst ", scalar(@terms), " terms: ";
  59. print join(', ', @terms), "\n";