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 code-golf, 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.
-
4\$\begingroup\$ "but you must not write code which checks for equivalence with them." This is a non-observable program requirement. \$\endgroup\$Martin Ender– Martin Ender2017年06月20日 17:05:05 +00:00Commented Jun 20, 2017 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– Arjun2017年06月20日 17:08:12 +00:00Commented Jun 20, 2017 at 17:08
-
3\$\begingroup\$ Why the downvotes? \$\endgroup\$Arjun– Arjun2017年06月20日 17:11:41 +00:00Commented Jun 20, 2017 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– Leaky Nun2017年06月20日 17:18:03 +00:00Commented Jun 20, 2017 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– Okx2017年06月20日 17:20:19 +00:00Commented Jun 20, 2017 at 17:20
23 Answers 23
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:
-
6\$\begingroup\$ I like you. That's why I'm going to kill you last. \$\endgroup\$David Conrad– David Conrad2017年06月20日 22:13:19 +00:00Commented Jun 20, 2017 at 22:13
Neim, 5 bytes
sDrTE
Explanation:
Example input: 1729
s Implicitly convert to digit list and sum the digits [19]
D Duplicate [19, 19]
r reverse [19, 91]
T mulTiply [1729]
E check for Equality with input [1]
Implicit output: 1
-
\$\begingroup\$ Are the other 4 bytes unprintable? I count 1 byte \$\endgroup\$GamrCorps– GamrCorps2017年06月20日 18:39:19 +00:00Commented Jun 20, 2017 at 18:39
-
1\$\begingroup\$ @GamrCorps It seems they are not supported by your browser. Here is an image: puu.sh/wpETt/9f92af18e0.png \$\endgroup\$Okx– Okx2017年06月20日 18:47:17 +00:00Commented Jun 20, 2017 at 18:47
-
1\$\begingroup\$ Nice, but 5 bytes in what encoding? :-) Of the 5 characters here, s (U+1D42C MATHEMATICAL BOLD SMALL S), r (U+1D42B MATHEMATICAL BOLD SMALL R), T (U+1D54B MATHEMATICAL DOUBLE-STRUCK CAPITAL T) and E (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. \$\endgroup\$ShreevatsaR– ShreevatsaR2017年06月20日 19:38:05 +00:00Commented Jun 20, 2017 at 19:38
-
3
-
\$\begingroup\$ @Arjun Done, don't worry about it \$\endgroup\$Timtech– Timtech2017年06月20日 22:53:17 +00:00Commented Jun 20, 2017 at 22:53
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
Haskell, (削除) 56 (削除ここまで) 55 bytes
f n|m<-sum$read.pure<$>show n=n==m*(read.reverse.show)m
Pointfree: (56 bytes)
(==)<*>((*)=<<read.reverse.show).sum.map(read.pure).show
Boring: (24 bytes)
(`elem`[1,81,1458,1729])
JavaScript ES6, (削除) 59 (削除ここまで) 57 bytes
x=>(q=eval([...x].join`+`)+'')*[...q].reverse().join``==x
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
Mathematica, 42 bytes
(s=Tr@IntegerDigits@#)IntegerReverse@s==#&
-
\$\begingroup\$ Bifurcate, of course.
DSODR*Qwas what I had before looking. \$\endgroup\$Magic Octopus Urn– Magic Octopus Urn2017年06月21日 14:57:54 +00:00Commented Jun 21, 2017 at 14:57 -
\$\begingroup\$ @carusocomputing Not sure why you'd need the first
D. \$\endgroup\$Erik the Outgolfer– Erik the Outgolfer2017年06月21日 15:15:56 +00:00Commented Jun 21, 2017 at 15:15
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}
-
\$\begingroup\$
.split('')can be made.chars\$\endgroup\$Conor O'Brien– Conor O'Brien2018年07月12日 21:25:21 +00:00Commented Jul 12, 2018 at 21:25
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.
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))
-
\$\begingroup\$ As I learned recently, immediately answering your own challenge is frowned upon. \$\endgroup\$Shaggy– Shaggy2017年06月20日 17:05:57 +00:00Commented Jun 20, 2017 at 17:05
-
\$\begingroup\$ @Shaggy But valid. \$\endgroup\$Okx– Okx2017年06月20日 17:07:39 +00:00Commented Jun 20, 2017 at 17:07
-
3\$\begingroup\$ And I usually downvote those who do not care about the community. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月20日 17:08:39 +00:00Commented Jun 20, 2017 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– Shaggy2017年06月20日 17:08:53 +00:00Commented Jun 20, 2017 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– totallyhuman2017年06月20日 17:20:11 +00:00Commented Jun 20, 2017 at 17:20
Kotlin, (削除) 111 (削除ここまで) 108 bytes
fun main(a:Array<String>)=print(a[0].sumBy{c->"$c".toInt()}.run{"${this*"$this".reversed().toInt()}"}==a[0])
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))
Python 2, 55 bytes
def f(n):x=sum(map(int,`n`));return x*int(`x`[::-1])==n
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]
-
\$\begingroup\$ Alternate alternate solution, same length:
[1,81,1458,1729].__contains__\$\endgroup\$musicman523– musicman5232017年06月25日 03:09:14 +00:00Commented Jun 25, 2017 at 3:09
NewStack, 16 bytes
ḟi1f YΣ©Eᴙx| ∏=f
The breakdown:
Using 1729 as example
ḟi Define new function equal to input. []
1 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.
-
\$\begingroup\$ Multiply the stack by the function. I didn't get that. Does that mean the input? \$\endgroup\$Arjun– Arjun2017年06月28日 07:27:36 +00:00Commented Jun 28, 2017 at 7:27
-
\$\begingroup\$ Yes and no, after
¹, the stack consists of[1]. And since we definedforf(x)to equal your input, multiplying every element in the stack by the functionfis essentially replacing the1with our input. (Because[1] * f(x)=[f]) \$\endgroup\$Graviton– Graviton2017年06月28日 21:40:56 +00:00Commented Jun 28, 2017 at 21:40
MATL, 11 bytes
tV!UstVPU*=
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)
-
\$\begingroup\$
DS×Ṛ$Ḍ=saves a byte. \$\endgroup\$Dennis– Dennis2017年06月20日 18:47:45 +00:00Commented Jun 20, 2017 at 18:47 -
\$\begingroup\$ @Dennis that is... wat witchkraft. \$\endgroup\$Leaky Nun– Leaky Nun2017年06月20日 19:05:44 +00:00Commented Jun 20, 2017 at 19:05
-
\$\begingroup\$ Just distributivity. (1000a + 100b + 10c + d)y = 1000ay + 100by + 10cy + dy \$\endgroup\$Dennis– Dennis2017年06月20日 19:10:38 +00:00Commented Jun 20, 2017 at 19:10
PHP, 52 bytes
<?=strrev($x=array_sum(str_split($argn)))*$x==$argn;
PHP, 36 bytes
<?=in_array($argn,[1,81,1458,1729]);
Pari/GP, (削除) 56 (削除ここまで) 52 bytes
n->n==(s=sumdigits(n))*fromdigits(Vecrev(digits(s)))
Explore related questions
See similar questions with these tags.