PowerShell v3+, 183 bytes
param($n)$b=@();for($a=$n;$a-gt1){2..$a|?{'1'*$_-match'^(?!(..+)\1+$)..'-and!($a%$_)}|%{$b+=$_;$a/=$_}}$n-notin$b-and(([char[]]"$n")-join'+'|iex)-eq(($b|%{[char[]]"$_"})-join'+'|iex)
No built-in prime checking. No built-in factoring. No built-in digit-sum. Everything's hand made. :D
Takes input $n
as an integer, sets $b
equal to an empty array. Here, $b
is our collection of prime factors.
Next is a for
loop. We first set $a
equal to our input number, and the conditional is until $a
is less-than-or-equal-to 1. This loop is going to find our prime factors.
We loop from 2
up to $a
, uses Where-Object
(|?{...}
) to pull out primes that are also factors !($a%$_)
. Those are fed into an inner-loop |%{...}
that places the factor into $b
and divides $a
(thus we'll eventually get to 1
).
So, now we have all of our prime factors in $b
. Time to formulate our Boolean output. We need to verify that $n
is -notin
$b
, because if it is that means that $n
is prime, and so isn't a Smith number. Additionally, (-and
) we need to make sure that our two sets of digit sums are -eq
ual. The resulting Boolean is left on the pipeline and output is implicit.
NB - Requires v3 or newer for the -notin
operator. I'm still running the input for 4937775
(this is slow to calculate), so I'll update this when that finishes. After 3+ hours, I got a stackoverflow error. So, there's some upper-bound somewhere. Oh well.
This will work for negative input, zero, or one, because the right-hand of the -and
will barf out an error while it tries to calculate the digit sums (shown below), which will cause that half to go to $false
when evaluated. Since STDERR is ignored by default, and the correct output is still displayed, this is fine.
Test cases
PS C:\Tools\Scripts\golfing> 4,22,27,58,85,94,18,13,666,-265,0,1|%{"$_ -> "+(.\is-this-a-smith-number.ps1 $_)}
4 -> True
22 -> True
27 -> True
58 -> True
85 -> True
94 -> True
18 -> False
13 -> False
666 -> True
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string.
At C:\Tools\Scripts\golfing\is-this-a-smith-number.ps1:1 char:179
+ ... "$_"})-join'+'|iex)
+ ~~~
+ CategoryInfo : InvalidData: (:String) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
-265 -> False
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string.
At C:\Tools\Scripts\golfing\is-this-a-smith-number.ps1:1 char:179
+ ... "$_"})-join'+'|iex)
+ ~~~
+ CategoryInfo : InvalidData: (:String) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
0 -> False
Invoke-Expression : Cannot bind argument to parameter 'Command' because it is an empty string.
At C:\Tools\Scripts\golfing\is-this-a-smith-number.ps1:1 char:179
+ ... "$_"})-join'+'|iex)
+ ~~~
+ CategoryInfo : InvalidData: (:String) [Invoke-Expression], ParameterBindingValidationException
+ FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.InvokeExpressionCommand
1 -> False
6I had to read this: "sum of digits is equal to the sum of sums of digits of its prime factors" a few times :P – Stewie Griffin – 2016-09-02T14:29:08.277
@StewieGriffin: Yes, it's a rather complicated sentence, but I felt like I needed to give a proper definition instead of relying solely on the examples :) – shooqie – 2016-09-02T14:30:45.610
2This is one of those questions where I thought "Java+this=no", I upvoted for the idea though :P – Shaun Wild – 2016-09-02T15:03:57.060
3I sometimes notice patterns in numbers, sum of digits etc., but really, do people notice stuff like this: "Albert Wilansky coined the term Smith number when he noticed the defining property in the phone number of his brother-in-law"? – Stewie Griffin – 2016-09-02T15:11:21.163
1@StewieGriffin: Yeah, it's like Ramanujan and 1729, always baffled me as well. – shooqie – 2016-09-02T15:16:18.313
@BasicallyAlanTuring I should have listened to you..>.> ;P (posted a very, very long Java 7 answer) – Kevin Cruijssen – 2016-09-06T11:26:08.080
@KevinCruijssen I just pissed myself laughing hahahaaha Great attempt man – Shaun Wild – 2016-09-06T11:39:40.220
@BasicallyAlanTuring Thanks. It works, but with that everything is said.. XD Java & prime-check + prime-factors + sum-of-digits = 50x more bytes used than the current best answer, lmao. I knew you were right with your "Java+this=no", but couldn't resist trying.. ;) – Kevin Cruijssen – 2016-09-06T11:43:07.230
@BasicallyAlanTuring Ok, managed to more than halve the amount of bytes now that I had some time to think about it.. Could probably be tweaked some more here-and-there, but at least it's lowered by a large amount (although it's still higher than any other answer..) – Kevin Cruijssen – 2016-09-06T15:07:55.573
Do the results have to be truthy/falsy in the relevant language? If not, what determines truthiness/falsiness? – dfeuer – 2019-03-05T06:23:21.077