Ruth-Aaron numbers

Revision as of 11:45, 23 January 2022 by PureFox (talk | contribs) (→‎{{header|Wren}}: Added code to find first triple based on prime divisors.)

A Ruth–Aaron pair consists of two consecutive integers (e.g., 714 and 715) for which the sums of the prime divisors of each integer are equal. So called because 714 is Babe Ruth's lifetime home run record; Hank Aaron's 715th home run broke this record and 714 and 715 have the same prime divisor sum.

Ruth-Aaron numbers is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.


A Ruth–Aaron triple consists of three consecutive integers with the same properties.


There is a second variant of Ruth–Aaron numbers, one which uses prime factors rather than prime divisors. The difference; divisors are unique, factors may be repeated. The 714, 715 pair appears in both, so the name still fits.


It is common to refer to each Ruth–Aaron group by the first number in it.


Task
  • Find and show, here on this page, the first 30 Ruth-Aaron numbers (factors).
  • Find and show, here on this page, the first 30 Ruth-Aaron numbers (divisors).


Stretch
  • Find and show the first Ruth-Aaron triple (factors).
  • Find and show the first Ruth-Aaron triple (divisors).


See also


Perl

<lang perl>#!/usr/bin/perl

use strict; use warnings; use ntheory qw( factor vecsum ); use List::AllUtils qw( uniq );

  1. use Data::Dump 'dd'; dd factor(6); exit;

my $n = 1; my @answers; while( @answers < 30 )

 {
 vecsum(factor($n)) == vecsum(factor($n+1)) and push @answers, $n;
 $n++;
 }

print "factors:\n\n@answers\n\n" =~ s/.{60}\K /\n/gr;

$n = 1; @answers = (); while( @answers < 30 )

 {
 vecsum(uniq factor($n)) == vecsum(uniq factor($n+1)) and push @answers, $n;
 $n++;
 }

print "divisors:\n\n@answers\n" =~ s/.{60}\K /\n/gr;</lang>

Output:
factors:

5 8 15 77 125 714 948 1330 1520 1862 2491 3248 4185 4191 5405
5560 5959 6867 8280 8463 10647 12351 14587 16932 17080 18490
20450 24895 26642 26649

divisors:

5 24 49 77 104 153 369 492 714 1682 2107 2299 2600 2783 5405
6556 6811 8855 9800 12726 13775 18655 21183 24024 24432 24880
25839 26642 35456 40081

Phix

Library: Phix/online

You can run this online here.

with javascript_semantics
procedure ruth_aaron(bool d, integer n=30, l=2, i=1)
    string ns = iff(n=1?"":sprintf(" %d",n)),
           nt = iff(l=2?"numbers":"triple"),
           fd = iff(d?"divisors":"factors")
    printf(1,"First%s Ruth-Aaron %s (%s):\n",{ns,nt,fd})
    integer prev = -1, k = i, c = 0
    while n do
        sequence f = prime_factors(k,true,-1)
        if d then f = unique(f) end if
        integer s = sum(f)
        if s and s=prev then
            c += 1
            if c=l-1 then
                printf(1,"%d ",k-c)
                n -= 1
            end if
        else
            c = 0
        end if
        prev = s
        k += 1
    end while
    printf(1,"\n\n")
end procedure
ruth_aaron(false)   -- https://oeis.org/A039752
ruth_aaron(true)    -- https://oeis.org/A006145
ruth_aaron(false, 1, 3)
-- give this one a little leg-up :-) ...
ruth_aaron(true, 1, 3, 89460000)
Output:
First 30 Ruth-Aaron numbers (factors):
5 8 15 77 125 714 948 1330 1520 1862 2491 3248 4185 4191 5405 5560 5959 6867 8280 8463 10647 12351 14587 16932 17080 18490 20450 24895 26642 26649

First 30 Ruth-Aaron numbers (divisors):
5 24 49 77 104 153 369 492 714 1682 2107 2299 2600 2783 5405 6556 6811 8855 9800 12726 13775 18655 21183 24024 24432 24880 25839 26642 35456 40081

First Ruth-Aaron triple (factors):
417162

First Ruth-Aaron triple (divisors):
89460294

Raku

<lang perl6>use Prime::Factor;

my @pf = lazy (^∞).hyper(:1000batch).map: *.&prime-factors.sum; my @upf = lazy (^∞).hyper(:1000batch).map: *.&prime-factors.unique.sum;

  1. Task: < 1 second

put "First 30 Ruth-Aaron numbers (Factors):\n" ~ (1..∞).grep( { @pf[$_] == @pf[$_ + 1] } )[^30];

put "\nFirst 30 Ruth-Aaron numbers (Divisors):\n" ~ (1..∞).grep( { @upf[$_] == @upf[$_ + 1] } )[^30];

  1. Stretch: ~ 5 seconds

put "\nFirst Ruth-Aaron triple (Factors):\n" ~ (1..∞).first: { @pf[$_] == @pf[$_ + 1] == @pf[$_ + 2] }

  1. Really, really, _really_ slow. 186(!) minutes... but with no cheating or "leg up".

put "\nFirst Ruth-Aaron triple (Divisors):\n" ~ (1..∞).first: { @upf[$_] == @upf[$_ + 1] == @upf[$_ + 2] }</lang>

Output:
First 30 Ruth-Aaron numbers (Factors):
5 8 15 77 125 714 948 1330 1520 1862 2491 3248 4185 4191 5405 5560 5959 6867 8280 8463 10647 12351 14587 16932 17080 18490 20450 24895 26642 26649

First 30 Ruth-Aaron numbers (Divisors):
5 24 49 77 104 153 369 492 714 1682 2107 2299 2600 2783 5405 6556 6811 8855 9800 12726 13775 18655 21183 24024 24432 24880 25839 26642 35456 40081

First Ruth-Aaron triple (Factors):
417162

First Ruth-Aaron triple (Divisors):
89460294

Wren

Library: Wren-math
Library: Wren-seq
Library: Wren-fmt

To find the first thirty Ruth-Aaron pairs and the first triple based on factors takes around 2.2 seconds.

However, with nearly 90 million trios of numbers to slog through, it takes around 68 minutes to find the first triple based on divisors. <lang ecmascript>import "./math" for Int, Nums import "./seq" for Lst import "./fmt" for Fmt

var resF = [] var resD = [] var resT = [] // factors only var n = 2 var factors1 = [] var factors2 = [2] var factors3 = [3] var sum1 = 0 var sum2 = 2 var sum3 = 3 var countF = 0 var countD = 0 var countT = 0 while (countT < 1 || countD < 30 || countF < 30) {

   factors1 = factors2
   factors2 = factors3
   factors3 = Int.primeFactors(n+2)
   sum1 = sum2
   sum2 = sum3
   sum3 = Nums.sum(factors3)
   if (countF < 30 && sum1 == sum2) {
       resF.add(n)
       countF = countF + 1
   }
   if (sum1 == sum2 && sum2 == sum3) {
       resT.add(n)
       countT = countT + 1
   }
   if (countD < 30) {
       var factors4 = factors1.toList
       var factors5 = factors2.toList
       Lst.prune(factors4)
       Lst.prune(factors5)
       if (Nums.sum(factors4) == Nums.sum(factors5)) {
           resD.add(n)
           countD = countD + 1
       }
   }
   n = n + 1

}

System.print("First 30 Ruth-Aaron numbers (factors):") System.print(resF.join(" ")) System.print("\nFirst 30 Ruth-Aaron numbers (divisors):") System.print(resD.join(" ")) System.print("\nFirst Ruth-Aaron triple (factors):") System.print(resT[0])

resT = [] // divisors only n = 2 factors1 = [] factors2 = [2] factors3 = [3] sum1 = 0 sum2 = 2 sum3 = 3 countT = 0 while (countT < 1) {

   factors1 = factors2
   factors2 = factors3
   factors3 = Int.primeFactors(n+2)
   Lst.prune(factors3)
   sum1 = sum2
   sum2 = sum3
   sum3 = Nums.sum(factors3)
   if (sum1 == sum2 && sum2 == sum3) {
       resT.add(n)
       countT = countT + 1
   }
   n = n + 1

}

System.print("\nFirst Ruth-Aaron triple (divisors):") System.print(resT[0])</lang>

Output:
First 30 Ruth-Aaron numbers (factors):
5 8 15 77 125 714 948 1330 1520 1862 2491 3248 4185 4191 5405 5560 5959 6867 8280 8463 10647 12351 14587 16932 17080 18490 20450 24895 26642 26649

First 30 Ruth-Aaron numbers (divisors):
5 24 49 77 104 153 369 492 714 1682 2107 2299 2600 2783 5405 6556 6811 8855 9800 12726 13775 18655 21183 24024 24432 24880 25839 26642 35456 40081

First Ruth-Aaron triple (factors):
417162

First Ruth-Aaron triple (divisors):
89460294