Is this a Hardy-Ramanujan-Number-ish Harshad Number?

16

3

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.

Arjun

Posted 2017-06-20T17:00:08.547

Reputation: 4 544

4

"but you must not write code which checks for equivalence with them." This is a non-observable program requirement.

– Martin Ender – 2017-06-20T17:05:05.243

@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. – Arjun – 2017-06-20T17:08:12.657

2Why the downvotes? – Arjun – 2017-06-20T17:11:41.443

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. – Leaky Nun – 2017-06-20T17:18:03.053

6I 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. – Okx – 2017-06-20T17:20:19.997

@Okx Too late now, I guess – Arjun – 2017-06-20T17:27:32.787

@Arjun Not really. That's why you're getting downvotes. – Okx – 2017-06-20T17:28:46.900

@Okx Done. Thanks for the help. :) – Arjun – 2017-06-20T17:32:06.473

Was this inspired by my Sandbox post?

– NoOneIsHere – 2017-06-20T18:40:55.447

@NoOneIsHere No, I have been knowing about Ramanujan and 1729 for last 4 years. BTW, I used to be Sting. It was I who pointed out that property of its. ;) Can't remember where, though. – Arjun – 2017-06-20T18:56:25.553

@Arjun Oh, cool! It was on a deleted question of mine. – NoOneIsHere – 2017-06-20T19:04:50.753

@NoOneIsHere Can you provide a link? – Arjun – 2017-06-20T19:06:40.033

@Arjun My mistake, it wasn't deleted. https://codegolf.stackexchange.com/q/80761/48922

– NoOneIsHere – 2017-06-20T19:13:49.500

Answers

12

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]
      muliply [1729]
      check for quality with input [1]
Implicit output: 1

Try it!

Okx

Posted 2017-06-20T17:00:08.547

Reputation: 15 025

Are the other 4 bytes unprintable? I count 1 byte – GamrCorps – 2017-06-20T18:39:19.220

1

@GamrCorps It seems they are not supported by your browser. Here is an image: https://puu.sh/wpETt/9f92af18e0.png

– Okx – 2017-06-20T18:47:17.503

1

Nice, but 5 bytes in what encoding? :-) Of the 5 characters here, (U+1D42C MATHEMATICAL BOLD SMALL S), (U+1D42B MATHEMATICAL BOLD SMALL R), (U+1D54B MATHEMATICAL DOUBLE-STRUCK CAPITAL T) and (U+1D53C MATHEMATICAL DOUBLE-STRUCK CAPITAL E) each take up 4 bytes in UTF-8 (also in UTF-16 and in (obviously) UTF-32). So this is actually 17 bytes, though I guess one could define a custom encoding optimized for ASCII + those characters.

– ShreevatsaR – 2017-06-20T19:38:05.363

3

@ShreevatsaR Neim seems to use its own code page

– The_Lone_Devil – 2017-06-20T20:04:09.173

@The_Lone_Devil Ah that would explain it, thanks! – ShreevatsaR – 2017-06-20T20:09:50.723

@Arjun Done, don't worry about it – Timtech – 2017-06-20T22:53:17.180

15

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

Domii

Posted 2017-06-20T17:00:08.547

Reputation: 219

6I like you. That's why I'm going to kill you last. – David Conrad – 2017-06-20T22:13:19.067

12

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

Govind Parmar

Posted 2017-06-20T17:00:08.547

Reputation: 828

4

Brachylog, 8 bytes

ẹ+S↔;S×?

Try it online!

Leaky Nun

Posted 2017-06-20T17:00:08.547

Reputation: 45 011

4

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!

Laikoni

Posted 2017-06-20T17:00:08.547

Reputation: 23 676

4

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

Downgoat

Posted 2017-06-20T17:00:08.547

Reputation: 27 116

2

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))

Arjun

Posted 2017-06-20T17:00:08.547

Reputation: 4 544

As I learned recently, immediately answering your own challenge is frowned upon. – Shaggy – 2017-06-20T17:05:57.127

@Shaggy But valid. – Okx – 2017-06-20T17:07:39.450

3And I usually downvote those who do not care about the community. – Leaky Nun – 2017-06-20T17:08:39.310

1@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. – Shaggy – 2017-06-20T17:08:53.177

@Shaggy It's valid but frowned upon. – Leaky Nun – 2017-06-20T17:10:40.293

@LeakyNun Why is it frowned upon? – Arjun – 2017-06-20T17:14:42.493

@Shaggy Why so? – Arjun – 2017-06-20T17:15:01.073

@Okx Why is it frowned upon? – Arjun – 2017-06-20T17:15:33.153

I have no idea. – Okx – 2017-06-20T17:17:31.457

3I 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. – totallyhuman – 2017-06-20T17:20:11.487

2

Mathematica, 42 bytes

(s=Tr@IntegerDigits@#)IntegerReverse@s==#&

J42161217

Posted 2017-06-20T17:00:08.547

Reputation: 15 931

2

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}

reitermarkus

Posted 2017-06-20T17:00:08.547

Reputation: 291

.split('') can be made .chars – Conor O'Brien – 2018-07-12T21:25:21.240

2

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.

Neil

Posted 2017-06-20T17:00:08.547

Reputation: 95 035

2

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!

Matej

Posted 2017-06-20T17:00:08.547

Reputation: 121

1

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!

totallyhuman

Posted 2017-06-20T17:00:08.547

Reputation: 15 378

Alternate alternate solution, same length: [1,81,1458,1729].__contains__ – musicman523 – 2017-06-25T03:09:14.423

1

05AB1E, 5 bytes

SOÂ*Q

Try it online!

Erik the Outgolfer

Posted 2017-06-20T17:00:08.547

Reputation: 38 134

Bifurcate, of course. DSODR*Q was what I had before looking. – Magic Octopus Urn – 2017-06-21T14:57:54.913

@carusocomputing Not sure why you'd need the first D. – Erik the Outgolfer – 2017-06-21T15:15:56.010

1

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!

Leaky Nun

Posted 2017-06-20T17:00:08.547

Reputation: 45 011

1

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.

Graviton

Posted 2017-06-20T17:00:08.547

Reputation: 2 295

Multiply the stack by the function. I didn't get that. Does that mean the input? – Arjun – 2017-06-28T07:27:36.257

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]) – Graviton – 2017-06-28T21:40:56.623

1

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)

sundar - Reinstate Monica

Posted 2017-06-20T17:00:08.547

Reputation: 5 296

1

Japt, 8 bytes

¬x
*sÔ¥N

Run it online

Oliver

Posted 2017-06-20T17:00:08.547

Reputation: 7 160

0

Jelly, 8 bytes

DS×ṚḌ$$=

Try it online!

Leaky Nun

Posted 2017-06-20T17:00:08.547

Reputation: 45 011

DS×Ṛ$Ḍ= saves a byte. – Dennis – 2017-06-20T18:47:45.377

@Dennis that is... wat witchkraft. – Leaky Nun – 2017-06-20T19:05:44.313

Just distributivity. (1000a + 100b + 10c + d)y = 1000ay + 100by + 10cy + dy – Dennis – 2017-06-20T19:10:38.780

0

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!

Jörg Hülsermann

Posted 2017-06-20T17:00:08.547

Reputation: 13 026

0

Perl 6, 30 bytes

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

Sean

Posted 2017-06-20T17:00:08.547

Reputation: 4 136

0

APL (Dyalog), 18 bytes

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

Try it online!

user41805

Posted 2017-06-20T17:00:08.547

Reputation: 16 320

0

Pari/GP, 56 52 bytes

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

Try it online!

alephalpha

Posted 2017-06-20T17:00:08.547

Reputation: 23 988

0

MathGolf, 5 bytes

Σ_x*=

Try it online!

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

maxb

Posted 2017-06-20T17:00:08.547

Reputation: 5 754