Home » Detect Hardy-Ramanujan Numbers

Detect Hardy-Ramanujan Numbers

A taxicab number (also known as Hardy Ramanujan Number) is a positive integer that can be expressed as the sum of two positive cubes in more than one way. Most famous is 1729. 1729 = 1^3 + 12^3 = 9^3 + 10^3 List Y against a number if it is Taxicab number otherwise N.

📌 Challenge Details and Links
ExcelBI Excel Challenge Number: 481
Challenge Difficulty: ⭐️⭐️
📥Download Sample File
📥Link to the solutions on LinkedIn

Solving the challenge of Detect Hardy-Ramanujan Numbers with Power Query

Power Query solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Aditya Kumar Darak 🇮🇳:
let
  Source = Excel.CurrentWorkbook(){[Name = "data"]}[Content], 
  Return = Table.AddColumn(
    Source, 
    "Answer", 
    each [
      CR = Int64.From(Number.Power([Numbers], 1 / 3)), 
      G = List.Transform(
        {1 .. CR}, 
        (f) =>
          [
            c1 = Number.Power(f, 3), 
            c2 = Number.Power([Numbers] - c1, 1 / 3), 
            tf = Number.Mod(Number.Round(c2, 8), 1) = 0, 
            r  = Number.From(tf)
          ][r]
      ), 
      R = List.Sum(G) / 2 > 1
    ][R]
  )
in
  Return
Power Query solution 2 for Detect Hardy-Ramanujan Numbers, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Sol = Table.AddColumn(
    Source, 
    "Answer", 
    (z) =>
      let
        a = Int64.From(Number.Power(z[Numbers], 1 / 3)), 
        b = List.Select(
          {1 .. a}, 
          (x) =>
            List.AnyTrue(
              List.Transform({1 .. a}, (y) => Number.Power(x, 3) + Number.Power(y, 3) = z[Numbers])
            )
        ), 
        c = if List.Count(b) > 1 then "Y" else "N"
      in
        c
  )
in
  Sol
Power Query solution 3 for Detect Hardy-Ramanujan Numbers, proposed by Alejandro Simón 🇵🇦 🇪🇸:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Sol = Table.AddColumn(
    Source, 
    "Answer", 
    (z) =>
      let
        a = Int64.From(Number.Power(z[Numbers], 1 / 3)), 
        b = List.Transform(
          {1 .. a}, 
          (x) =>
            List.Transform(
              {1 .. a}, 
              (y) => {x, y} & {Number.Power(x, 3) + Number.Power(y, 3) = z[Numbers]}
            )
        ), 
        c = List.Select(List.Combine(b), each _{2} = true), 
        d = List.Distinct(List.Transform(c, each List.Sort(List.FirstN(_, 2)))), 
        e = if List.Count(d) > 1 then "Y" else "N"
      in
        e
  )
in
  Sol
Power Query solution 4 for Detect Hardy-Ramanujan Numbers, proposed by Luan Rodrigues:
let
  Fonte = Tabela1, 
  res = Table.AddColumn(
    Fonte, 
    "Personalizar", 
    each 
      let
        a = List.Transform(
          {1 .. Int64.From(Number.Power([Numbers], 1 / 3))}, 
          (x) => Number.Power(([Numbers] - Number.Power(x, 3)), 1 / 3)
        ), 
        b = List.Select(a, each Number.Round(_, 2) = Int64.From(_)), 
        c = if (try List.Count(b) otherwise 0) = 0 then "N" else "Y"
      in
        c
  )
in
  res
Power Query solution 5 for Detect Hardy-Ramanujan Numbers, proposed by Abdallah Ally:
let
  Source = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  f = (n) =>
    let
      a = {1 .. Number.RoundDown(Number.Power(n, 1 / 3))}, 
      b = List.Accumulate(
        a, 
        {}, 
        (x, y) =>
          let
            c = Number.Round(Number.Power(n - Number.Power(y, 3), 1 / 3), 6), 
            d = if List.Contains(a, c) then x & {y, c} else x
          in
            List.Distinct(d)
      )
    in
      if List.Count(b) > 2 then "Y" else "N", 
  g = Table.AddColumn(Source, "My Answer", each f([Numbers])), 
  Result = Table.AddColumn(g, "Check", each [Answer Expected] = [My Answer])
in
  Result
Power Query solution 6 for Detect Hardy-Ramanujan Numbers, proposed by Ramiro Ayala Chávez:
let
  S = Excel.CurrentWorkbook(){[Name = "Table1"]}[Content], 
  Sol = Table.AddColumn(
    S, 
    "Answer Expected", 
    each 
      let
        A = [Numbers], 
        B = {1 .. Number.RoundDown(Number.Power(A, 1 / 3))}, 
        C = List.TransformMany(B, (x) => B, (x, y) => {x, y}), 
        D = List.Select(C, each _{0} <> _{1} and _{0} < _{1}), 
        E = List.Transform(D, each Number.Power(_{0}, 3) + Number.Power(_{1}, 3)), 
        F = List.Select(E, each _ = A), 
        G = if List.Count(F) <> 0 then "Y" else "N"
      in
        G
  )
in
  Sol

Solving the challenge of Detect Hardy-Ramanujan Numbers with Excel

Excel solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Bo Rydobon 🇹🇭:
=MAP(A2:A10,
    LAMBDA(a,
    LET(q,
    1/3,
    b,
    (a-SEQUENCE(
        a^q
    )^3)^q,
    IF(
        SUM(
            N(
                INT(
                    b
                )=b
            )
        )>2,
        "Y",
        "N"
    ))))
Excel solution 2 for Detect Hardy-Ramanujan Numbers, proposed by Rick Rothstein:
=IF(MAP(A2:A10,
    LAMBDA(x,
    SUM(0+(x=SEQUENCE(
        500
    )^3+SEQUENCE(
        ,
        500
    )^3))>2)),
    "Y",
    "N")
Excel solution 3 for Detect Hardy-Ramanujan Numbers, proposed by John V.:
=MAP(
    A2:A10,
    LAMBDA(
        x,
        LET(
            s,
            ROW(
                1:500
            )^3,
            IF(
                SUM(
                    N(
                        s+TOROW(
                            s
                        )=x
                    )
                )>1,
                "Y",
                "N"
            )
        )
    )
)

✅=MAP(A2:A10,
    LAMBDA(x,
    LET(s,
    SEQUENCE(x^(1/3))^3,
    IF(
        SUM(
                    N(
                        s+TOROW(
                            s
                        )=x
                    )
                )>1,
        "Y",
        "N"
    ))))
Excel solution 4 for Detect Hardy-Ramanujan Numbers, proposed by Kris Jaganah:
=MAP(A2:A10,
    LAMBDA(x,
    LET(a,
    SEQUENCE(x^(1/3)),
    IF(
        SUM(
            N(
                a^3+TOROW(
                    a
                )^3=x
            )
        )>3,
        "Y",
        "N"
    ))))
Excel solution 5 for Detect Hardy-Ramanujan Numbers, proposed by Julian Poeltl:
=MAP(A2:A10,
    LAMBDA(N,
    LET(M,
    MAX(N^(1/3)),
    S,
    SEQUENCE(
        M
    )^3,
    IF(SUM(--(N=(S+TOROW(
        S
    ))))>2,
    "Y",
    "N"))))
Excel solution 6 for Detect Hardy-Ramanujan Numbers, proposed by Timothée BLIOT:
=MAP(A2:A10,
    LAMBDA(z,
    IF(SUM(--(z=MAKEARRAY(
        999,
        999,
        LAMBDA(
            x,
            y,
            x^3+y^3
        )
    )))>1,
    "Y",
    "N")))
Excel solution 7 for Detect Hardy-Ramanujan Numbers, proposed by Oscar Mendez Roca Farell:
=MAP(A2:A10,
     LAMBDA(a,
     LET(c,
     1/3,
     s,
     (a-SEQUENCE(
         a^c
     )^3)^c,
     IF(
         OR(
             s=INT(
                 s
             )
         ),
          "Y",
          "N"
     ))))
Excel solution 8 for Detect Hardy-Ramanujan Numbers, proposed by Sunny Baggu:
=IF(
 MAP(
 A2:A10,
    
 LAMBDA(a,
    
 LET(
 b,
     SEQUENCE(a ^ (1 / 3)) ^ 3,
    
 c,
     TOROW(
         b
     ),
    
 SUM(N((b + c) = a)) > 1
 )
 )
 ),
    
 "Y",
    
 "N"
)
Excel solution 9 for Detect Hardy-Ramanujan Numbers, proposed by Abdallah Ally:
=MAP(A2:A10,
    LAMBDA(x,
    LET(a,
    x,
    b,
    SEQUENCE(a^(1/3)),
    c,
    DROP(
         REDUCE(
             "",
             b,
             LAMBDA(
                 x,
                 y,
                 IF(
                     OR(
                         a-y^3=FILTER(
                             b,
                             b<>y
                         )^3
                     ),
                     VSTACK(
                          x,
                         y
                     ),
                     x
                 )
             )
         ),
        1
    ),
    IF(
        COUNT(
            UNIQUE(
                c
            )
        )>2,
        "Y",
        "N"
    ))))
Excel solution 10 for Detect Hardy-Ramanujan Numbers, proposed by Tyler Cameron:
=LET(z,
    500,
    a,
    MUNIT(
        z
    ),
    b,
    SEQUENCE(
        z
    )^3,
    MAP(A2:A10,
    LAMBDA(x,
    IF(SUM(--(IF(
        a,
        "",
        b+TOROW(
            b
        )
    )=x))>2,
    "Y",
    "N"))))

Solving the challenge of Detect Hardy-Ramanujan Numbers with Python

Python solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Konrad Gryczan, PhD:
Code longer because of time measuring.
0.012 sec
import pandas as pd
import itertools
import time
path = '481 Taxicab Numbers.xlsx'
input = pd.read_excel(path, usecols="A")
test  = pd.read_excel(path, usecols="B")
start_time = time.time()
def is_taxicab(num):
 croot = round(num**(1/3) + 0.5)
 seq = range(1, croot)
 comb = [x for x in itertools.combinations(seq, 2) if x[0]**3 + x[1]**3 == num]
 
 if len(comb) >= 2:
 return "Y"
 else:
 return "N"
result = input.copy()
result['Answer Expected'] = result['Numbers'].apply(is_taxicab)
end_time = time.time()
execution_time = end_time - start_time
print("Execution time:", execution_time, "seconds")
# Execution time: 0.012981891632080078 seconds
print(result['Answer Expected'].equals(test['Answer Expected'])) # True
                    
                  

Solving the challenge of Detect Hardy-Ramanujan Numbers with Python in Excel

Python in Excel solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Abdallah Ally:
import pandas as pd
from itertools import product
def is_taxicab_number(number):
 numbers = []
 values = range(1, int(number ** (1/3)) + 1)
 for num1, num2 in product(values, values):
 if num1 != num2 and num1 ** 3 + num2 ** 3 == number:
 numbers.extend([num1, num2])
 return 'Y' if len(set(numbers)) > 2 else 'N'
file_path = 'Excel_Challenge_481 - Taxicab Numbers.xlsx'
df = pd.read_excel(file_path)
# Perform data wrangling
df['My Answer'] = df['Numbers'].map(is_taxicab_number)
df['check'] = df['Answer Expected'] == df['My Answer']
df
                    
                  

Solving the challenge of Detect Hardy-Ramanujan Numbers with R

R solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Konrad Gryczan, PhD:
library(tidyverse)
library(readxl)
library(tictoc)
path = "Excel/481 Taxicab Numbers.xlsx"
input = read_excel(path, range = "A1:A10")
test = read_excel(path, range = "B1:B10")
tic()
is_taxicab = function(number) {
 x = ceiling(number^(1/3))
 df = tibble(a = 1:x, b = 1:x) %>%
 expand.grid() %>%
 filter(a <= b,
 a^3 + b^3 == number)
 check = ifelse(nrow(df) >= 2, "Y", "N")
 return(check)
}
result = input %>%
 mutate(`Answer Expected` = map_chr(Numbers, is_taxicab)) 
toc()
identical(result$`Answer Expected`, test$`Answer Expected`)
# [1] TRUE
                    
                  
R solution 2 for Detect Hardy-Ramanujan Numbers, proposed by Anil Kumar Goyal:
Fast enough
df <- data.frame(
 Numbers = c(4104L,8435L,13832L,28939L,
 60625L,65728L,314496L,1392000L,87539319L)
)
check_hardy <- function(n){
 map_lgl(seq(floor(n^(1/3))), ~ dplyr::near(
 round((n - .x^3)^(1/3)),
 (n - .x^3)^(1/3)
 )) %>% {sum(.) > 1}
}
df %>% 
 mutate(ans = map_lgl(Numbers, check_hardy))
                    
                  

Solving the challenge of Detect Hardy-Ramanujan Numbers with Excel VBA

Excel VBA solution 1 for Detect Hardy-Ramanujan Numbers, proposed by Vasin Nilyok:
Sub TaxicabNum()
LastRow = Cells(Rows.Count, 1).End(xlUp).Row
For r = 2 To LastRow
 QNum = Cells(r, 1)
 LoopNum = Fix(WorksheetFunction.Power(Cells(r, 1) - 1, 1 / 3))
 For a = 1 To LoopNum
 LeftOne = WorksheetFunction.Power(a, 3)
 For b = a + 1 To LoopNum
 GoTo nextb
 Else
 GoTo CheckRight
 End If
nextb:
 Next b
 Next a
CheckRight:
 For c = a + 1 To LoopNum
 RightOne = WorksheetFunction.Power(c, 3)
 For d = c + 1 To LoopNum
 GoTo nextd
 Else
 If AnsLeft = AnsRight Then
 Cells(r, 3) = "Y"
 GoTo NextQNum
 End If
 End If
nextd:
 Next d
 Next c
 Cells(r, 3) = "N"
NextQNum:
Next r
End Sub
                    
                  

&&&

Leave a Reply