16
\$\begingroup\$

Background

The number 1729 is the Hardy-Ramanujan number. An amazing property of it was discovered by S. Ramanujan (who is widely regarded as the greatest Indian mathematician1), when G.H. Hardy paid a visit to him in a hospital. In Hardy's own words:

I remember once going to see him when he was ill at Putney. I had ridden in taxi cab number 1729 and remarked that the number seemed to me rather a dull one, and that I hoped it was not an unfavorable omen. "No," he replied, "it is a very interesting number; it is the smallest number expressible as the sum of two cubes in two different ways."

Besides that, it has many other amazing properties. One such property is that it's a Harshad Number, i.e the sum of its digits (1+7+2+9=19) is a factor of it. That too, a special one. As Masahiko Fujiwara showed, 1729 is a positive integer which, when its digits are added together, produces a sum which, when multiplied by its reversal, yields the original number:

1+7+2+9 = 19

19 × 91 = 1729

A positive integer having such property is what I define as Hardy-Ramanujan-ish Harshad Number, for the purpose of this post. (There might be a technical term for it, but I couldn't find it, unless it's member of A110921)


The Task

Given a positive integer n as input, output a truthy or falsey value based on whether the input n is a Hardy-Ramanujan-ish Harshad Number. Output truthy, if it is. Otherwise, output falsey.

Note that only four Hardy-Ramanujan-ish Harshad Numbers exist (1,81,1458 and 1729), and you can write code which checks for equivalence with them. But I don't think that will be fun.


Input

Your program should take a positive integer (a natural number, in other words). It may take it in any way except assuming it to be present in a variable. Reading from modal window, input box, command line, file etc. is allowed. Taking input as function argument is allowed as well.


Output

Your program should output a truthy or falsey value. They need not be consistent. Your program may output in any way except writing the output to a variable. Writing to screen, command line, file etc. is allowed. Outputting with function return is allowed as well.


Additional Rules

  • You must not use a built-in to accomplish the task (I wonder any language will have such built-in, but then Mathematica...)

  • Standard Loopholes apply.


Test Cases

Input        Output
1            Truthy (because 1 × 1 (reverse of 1) = 1)
2            Falsey
3            Falsey
4            Falsey
5            Falsey
81           Truthy (because 9 (8 + 1) × 9 (reverse of 9) = 81)
1458         Truthy (because 18 (1 + 4 + 5 + 8) × 81 (reverse of 18) = 1458)
1729         Truthy (because 19 (1 + 7 + 2 + 9) × 91 (reverse of 19) = 1729)
1730         Falsey
2017         Falsey

Winning Criterion

This is , so the shortest code in bytes wins!


1Every year, on 22nd December, the birthday of Srinivasa Ramanujan, National Mathematics Day is observed in India. His colleagues, those in Cambridge, compared him to Jacobi, Euler, and even Newton. Besides being so great, he had almost no formal training in Pure Mathematics, but still, he made important contributions to mathematical analysis, number theory, infinite series, and continued fractions. Unfortunately, he died at an early age of 32 with thousands of mathematical discoveries in his mind. A film was also made on him, which was based on his biography, The Man Who Knew Infinity.

\$\endgroup\$
  • 4
    \$\begingroup\$ "but you must not write code which checks for equivalence with them." This is a non-observable program requirement. \$\endgroup\$ – Martin Ender Jun 20 '17 at 17:05
  • \$\begingroup\$ @MartinEnder But then it will just be does the number equal 1729, 1458, 81 or 1. I don't think that will be any fun. \$\endgroup\$ – Arjun Jun 20 '17 at 17:08
  • 2
    \$\begingroup\$ Why the downvotes? \$\endgroup\$ – Arjun Jun 20 '17 at 17:11
  • \$\begingroup\$ Proof: the maximum digital sum of a number with n digits is 9n. The reverse of 9n would be at most 90n. So, the product would be at most 810n^2, which must have n digits, so it must be at least 10^(n-1). When n=7, it's pretty much done, so one only has to check until 999999. \$\endgroup\$ – Leaky Nun Jun 20 '17 at 17:18
  • 6
    \$\begingroup\$ I think you should just allow checking for equivalence with them. Those sort of answers would get downvotes anyway, and will probably be longer in some cases. \$\endgroup\$ – Okx Jun 20 '17 at 17:20

23 Answers 23

12
\$\begingroup\$

Neim, 5 bytes

𝐬D𝐫𝕋𝔼

Explanation:

Example input: 1729
𝐬      Implicitly convert to digit list and 𝐬um the digits [19]
 D     Duplicate [19, 19]
  𝐫    𝐫everse [19, 91]
   𝕋   mul𝕋iply [1729]
    𝔼  check for 𝔼quality with input [1]
Implicit output: 1

Try it!

\$\endgroup\$
15
\$\begingroup\$

ArnoldC, 888 Bytes

IT'S SHOWTIME
HEY CHRISTMAS TREE i
YOU SET US UP 0
GET YOUR ASS TO MARS i
DO IT NOW
I WANT TO ASK YOU A BUNCH OF QUESTIONS AND I WANT TO HAVE THEM ANSWERED IMMEDIATELY
HEY CHRISTMAS TREE a
YOU SET US UP 0
GET TO THE CHOPPER a
HERE IS MY INVITATION 1
YOU ARE NOT YOU YOU ARE ME i
ENOUGH TALK
HEY CHRISTMAS TREE b
YOU SET US UP 0
GET TO THE CHOPPER b
HERE IS MY INVITATION 81
YOU ARE NOT YOU YOU ARE ME i
ENOUGH TALK
HEY CHRISTMAS TREE c
YOU SET US UP 0
GET TO THE CHOPPER c
HERE IS MY INVITATION 1458
YOU ARE NOT YOU YOU ARE ME i
ENOUGH TALK
HEY CHRISTMAS TREE d
YOU SET US UP 0
GET TO THE CHOPPER d
HERE IS MY INVITATION 1729
YOU ARE NOT YOU YOU ARE ME i
ENOUGH TALK
HEY CHRISTMAS TREE res
YOU SET US UP 0
GET TO THE CHOPPER res
HERE IS MY INVITATION a
CONSIDER THAT A DIVORCE b
CONSIDER THAT A DIVORCE c
CONSIDER THAT A DIVORCE d
ENOUGH TALK
TALK TO THE HAND res
YOU HAVE BEEN TERMINATED

I know, I just check for equality, but that shouldn't be the fun part of the program.

Enjoy reading it. :)

Added some newlines there for easier readability:

Try it online

\$\endgroup\$
  • 6
    \$\begingroup\$ I like you. That's why I'm going to kill you last. \$\endgroup\$ – David Conrad Jun 20 '17 at 22:13
12
\$\begingroup\$

x86 Assembly, 55 35 33 31 bytes:

Assumes an ABI where the return value is in EAX and parameters are pushed on the stack... so almost all of them.

00000000: 8B 44 24 04        mov         eax,dword ptr [esp+4]
00000004: 48                 dec         eax
00000005: 74 16              je          0000001D
00000007: 83 E8 50           sub         eax,50h
0000000A: 74 11              je          0000001D
0000000C: 2D 61 05 00 00     sub         eax,561h
00000011: 74 0A              je          0000001D
00000013: 2D 0F 01 00 00     sub         eax,10Fh
00000018: 74 03              je          0000001D
0000001A: 33 C0              xor         eax,eax
0000001C: C3                 ret
0000001D: 40                 inc         eax
0000001E: C3                 ret
\$\endgroup\$
4
\$\begingroup\$

Brachylog, 8 bytes

ẹ+S↔;S×?

Try it online!

\$\endgroup\$
4
\$\begingroup\$

Haskell, 56 55 bytes

f n|m<-sum$read.pure<$>show n=n==m*(read.reverse.show)m

Try it online!


Pointfree: (56 bytes)

(==)<*>((*)=<<read.reverse.show).sum.map(read.pure).show

Try it online!


Boring: (24 bytes)

(`elem`[1,81,1458,1729])

Try it online!

\$\endgroup\$
4
\$\begingroup\$

JavaScript ES6, 59 57 bytes

x=>(q=eval([...x].join`+`)+'')*[...q].reverse().join``==x

Try it online!

Basically splits into digit array, and joins with + and evals that expression to basically sum the digits. string*string will automatically convert strings into ints. Takes input as a string

\$\endgroup\$
2
\$\begingroup\$

Mathematica, 42 bytes

(s=Tr@IntegerDigits@#)IntegerReverse@s==#&
\$\endgroup\$
2
\$\begingroup\$

Ruby, 69 Bytes

First try, with integer as input:

->i{(x=i.to_s.split'').inject(0){|s,a|s+a.to_i}*(x[-1]+x[0]).to_i==i}

Second try, with string as input:

->i{(x=i.split('').map &:to_i).inject(0,&:+)*(x[-1]*10+x[0])==i.to_i}
\$\endgroup\$
  • \$\begingroup\$ .split('') can be made .chars \$\endgroup\$ – Conor O'Brien Jul 12 '18 at 21:25
2
\$\begingroup\$

Batch, 164 bytes

@set/an=%1,s=0
:s
@set/as+=n%%10,n/=10
@if %n% gtr 0 goto s
@set/an=s,r=0
:r
@set/ar=r*10+n%%10,n/=10
@if %n% gtr 0 goto r
@set/an=%1-r*s
@if %n%==0 echo 1

Prints 1 on success, no output on failure.

\$\endgroup\$
2
\$\begingroup\$

JavaScript (ES6), 72 bytes

This is valid ES6 submission. Add f= at the start and invoke like f(arg).

n=>(y=[...`${n}`].reduce((c,p)=>+c+ +p))*[...`${y}`].reverse().join``==n

Test Snippet:

let f =

n=>(y=[...`${n}`].reduce((c,p)=>+c+ +p))*[...`${y}`].reverse().join``==n

console.log(1 + " -> " + f(1))
console.log(81 + " -> " + f(81))
console.log(1458 + " -> " + f(1458))
console.log(1729 + " -> " + f(1729))
console.log((randomNum = Math.floor(Math.random() * 10000) + 1) + " -> " + f(randomNum))

\$\endgroup\$
  • \$\begingroup\$ As I learned recently, immediately answering your own challenge is frowned upon. \$\endgroup\$ – Shaggy Jun 20 '17 at 17:05
  • \$\begingroup\$ @Shaggy But valid. \$\endgroup\$ – Okx Jun 20 '17 at 17:07
  • 3
    \$\begingroup\$ And I usually downvote those who do not care about the community. \$\endgroup\$ – Leaky Nun Jun 20 '17 at 17:08
  • 1
    \$\begingroup\$ @Okx, not according to the people who immediately downvoted and berated me for doing so. We need to be consistent as to whether or not this practice is allowed. \$\endgroup\$ – Shaggy Jun 20 '17 at 17:08
  • 3
    \$\begingroup\$ I think it because it gives the challenge poster an unfair time advantage as it's possible they can make up a challenge, solve it and then post it. \$\endgroup\$ – totallyhuman Jun 20 '17 at 17:20
2
\$\begingroup\$

Kotlin, 111 108 bytes

fun main(a:Array<String>)=print(a[0].sumBy{c->"$c".toInt()}.run{"${this*"$this".reversed().toInt()}"}==a[0])

Try it online!

As is typical for statically compiled JVM solutions, a lot of bytes are lost on just the main function declaration and calling print(). The meat of the function is 60ish bytes, which is not bad at all for a general purpose statically typed language like Kotlin.

Kotlin, boring solution, 69 bytes

fun main(a:Array<String>)=print(a[0].toInt()in setOf(1,81,1458,1729))

Try it online!

\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes

SOÂ*Q

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Bifurcate, of course. DSODR*Q was what I had before looking. \$\endgroup\$ – Magic Octopus Urn Jun 21 '17 at 14:57
  • \$\begingroup\$ @carusocomputing Not sure why you'd need the first D. \$\endgroup\$ – Erik the Outgolfer Jun 21 '17 at 15:15
1
\$\begingroup\$

Python 2, 55 bytes

def f(n):x=sum(map(int,`n`));return x*int(`x`[::-1])==n

Try it online!

Explanation

def f(n):                           # define a function f that takes an argument n
    x = sum(                        # assign to x the sum of...
            map(int, `n`))          # ...the integer conversion of all elements in stringified n
    return x * int(                 # return True if x times the integer conversion of...
                   `x`[::-1])       # ...the stringified x reversed...
                              == n  # ...equals n

An eval() solution is a bit 2 bytes longer...

def f(n):x=eval('+'.join(`n`));return x*int(`x`[::-1])==n

Alternate (invalid?) solution, 42 29 bytes

This solution checks for equality against all of the numbers.

lambda n:n in[1,81,1458,1729]

Try it online!

\$\endgroup\$
  • \$\begingroup\$ Alternate alternate solution, same length: [1,81,1458,1729].__contains__ \$\endgroup\$ – musicman523 Jun 25 '17 at 3:09
1
\$\begingroup\$

Cheddar, 60 bytes

(n,g=x->x?g(x/10|0)+x%10:0)->n==g(n)*number::("%d"%g(n)).rev

Try it online!

\$\endgroup\$
1
\$\begingroup\$

NewStack, 16 bytes

ḟᵢ¹f YΣ©Eᴙx| ∏=f

The breakdown:

Using 1729 as example

ḟᵢ                 Define new function equal to input.               []
  ¹                Add 1 to stack.                                   [1]
   f               Multiply stack by the function.                   [1729]
     Y             Split the stack into digits.                      [1,7,2,9]
      Σ            Sum the stack.                                    [19]
       ©           Duplicate stack.                                  [19,19]
        E  |       Define new value for the first element            [19,19]
         ᴙx        Reverse first element.                            [91,19]
             ∏     Take the product.                                 [1729]
              =f   Remove from stack if not equal to the function.   [1729]

Prints nothing if false, and the original input if true.

\$\endgroup\$
  • \$\begingroup\$ Multiply the stack by the function. I didn't get that. Does that mean the input? \$\endgroup\$ – Arjun Jun 28 '17 at 7:27
  • \$\begingroup\$ Yes and no, after ¹, the stack consists of [1]. And since we defined f or f(x) to equal your input, multiplying every element in the stack by the function f is essentially replacing the 1 with our input. (Because [1] * f(x) = [f]) \$\endgroup\$ – Graviton Jun 28 '17 at 21:40
1
\$\begingroup\$

MATL, 11 bytes

tV!UstVPU*=

Try it online!

t - take input and duplicate it

V!U - split it into individual digits

s - sum those digits

t - duplicate that sum

VP - turn that into a string, flip it left to right

U - turn that back into a number

* - multiply the last two values (the digit-sum and its left-to-right flipped version)

= - check if this is equal to the original input (which is the only other value in stack)

\$\endgroup\$
1
\$\begingroup\$

Japt, 8 bytes

¬x
*sÔ¥N

Run it online

\$\endgroup\$
0
\$\begingroup\$

Jelly, 8 bytes

DS×ṚḌ$$=

Try it online!

\$\endgroup\$
  • \$\begingroup\$ DS×Ṛ$Ḍ= saves a byte. \$\endgroup\$ – Dennis Jun 20 '17 at 18:47
  • \$\begingroup\$ @Dennis that is... wat witchkraft. \$\endgroup\$ – Leaky Nun Jun 20 '17 at 19:05
  • \$\begingroup\$ Just distributivity. (1000a + 100b + 10c + d)y = 1000ay + 100by + 10cy + dy \$\endgroup\$ – Dennis Jun 20 '17 at 19:10
0
\$\begingroup\$

Perl 6, 30 bytes

{my \n=sum .comb;$_==n*n.flip}
\$\endgroup\$
0
\$\begingroup\$

PHP, 52 bytes

<?=strrev($x=array_sum(str_split($argn)))*$x==$argn;

Try it online!

PHP, 36 bytes

<?=in_array($argn,[1,81,1458,1729]);

Try it online!

\$\endgroup\$
0
\$\begingroup\$

APL (Dyalog), 18 bytes

{(⍵=⍎∘⌽×⍎)⍕+/⍎¨⍕⍵}

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Pari/GP, 56 52 bytes

n->n==(s=sumdigits(n))*fromdigits(Vecrev(digits(s)))

Try it online!

\$\endgroup\$
0
\$\begingroup\$

MathGolf, 5 bytes

Σ_x*=

Try it online!

Pretty much the same as the Neim solution byte for byte.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.