क्या यह स्मिथ संख्या है?


28

चुनौती का वर्णन

एक स्मिथ संख्या एक संयुक्त संख्या है जिसका अंकों का योग इसके प्रमुख कारकों के अंकों के योग के बराबर है। पूर्णांक को देखते हुए N, यह निर्धारित करें कि यह स्मिथ संख्या है या नहीं।

पहले कुछ स्मिथ नंबर दिए गए हैं 4, 22, 27, 58, 85, 94, 121, 166, 202, 265, 274, 319, 346, 355, 378, 382, 391, 438(अनुक्रम A006753 OEIS में)।

नमूना इनपुट / आउटपुट

18: False (sum of digits: 1 + 8 = 9; factors: 2, 3, 3; sum of digits of factors: 2 + 3 + 3 = 8)
22: True
13: False (meets the digit requirement, but is prime)
666: True (sum of digits: 6 + 6 + 6 = 18; factors: 2, 3, 3, 37; sum of digits of factors: 2 + 3 + 3 + 3 + 7 = 18)
-265: False (negative numbers can't be composite)
0: False (not composite)
1: False (not composite)
4937775: True

टिप्पणियाँ

  • आपका कोड एक फ़ंक्शन (विधि) या पूर्ण कार्य कार्यक्रम हो सकता है,
  • इसके बजाय जैसे शब्दों की Trueऔर False, आप किसी भी truthy और falsy मूल्यों का उपयोग कर सकते हैं, जब तक यह स्पष्ट है के रूप में वे क्या कर रहे हैं,
  • यह एक चुनौती है, इसलिए अपने कोड को यथासंभव छोटा बनाएं!

6
मुझे इसे पढ़ना था: "अंकों का योग इसके प्रमुख कारकों के अंकों के योग के बराबर है" कुछ समय: पी
स्टीवी ग्रिफिन

@StewieGriffin: हाँ, यह एक बहुत ही जटिल वाक्य है, लेकिन मुझे ऐसा लगा जैसे मुझे उदाहरणों पर पूरी तरह भरोसा करने के बजाय एक उचित परिभाषा देने की आवश्यकता है :)
shooqie

2
यह उन सवालों में से एक है जहां मैंने सोचा था कि "जावा + यह = नहीं", मैं इस विचार के लिए तैयार था: पी
शॉन वाइल्ड

3
मैं कभी-कभी अंकों में पैटर्न, अंकों के योग आदि पर ध्यान देता हूं, लेकिन क्या वास्तव में, लोग इस तरह से सामान नोटिस करते हैं: "अल्बर्ट विल्स्की ने स्मिथ संख्या शब्द गढ़ा जब उन्होंने अपने बहनोई के फोन नंबर में परिभाषित संपत्ति देखी" ?
स्टीवी ग्रिफिन

1
@StewieGriffin: हाँ, यह रामानुजन और 1729 की तरह है, हमेशा मुझे भी चकित करता है।
शौकी

जवाबों:


9

जेली , 12 11 बाइट्स

Æfḟȯ.DFżDSE

स्मिथ संख्याओं के लिए 1 रिटर्न और 0 अन्यथा। इसे ऑनलाइन आज़माएं! या सभी परीक्षण मामलों को सत्यापित करें

पृष्ठभूमि

Æf(अभाज्य गुणनखंड) और D(पूर्णांक-से-दशमलव) को लागू किया जाता है ताकि P(उत्पाद) और (दशमलव-से-पूर्णांक) बाएँ व्युत्क्रम का निर्माण करें।

पूर्णांक -4 से 4 के लिए , Æfनिम्नलिखित रिटर्न देता है।

-4 -> [-1, 2, 2]
-3 -> [-1, 3]
-2 -> [-1, 2]
-1 -> [-1]
 0 -> [0]
 1 -> []
 2 -> [2]
 3 -> [3]
 4 -> [2, 2]

संख्या -10, -1, -0.5, 0, 0.5, 1, 10 के लिए , Dनिम्नलिखित रिटर्न देता है।

-11   -> [-1, -1]
-10   -> [-1, 0]
 -1   -> [-1]
 -0.5 -> [-0.5]
  0   -> [0]
  0.5 -> [0.5]
  1   -> [1]
 10   -> [1, 0]
 11   -> [1, 1]

यह काम किस प्रकार करता है

Æfḟȯ.DFżDSE  Main link. Argument: n (integer)

Æf           Yield the prime factorization of n.
  ḟ          Filter; remove n from its prime factorization.
             This yields an empty array if n is -1, 0, 1, or prime.
   ȯ.        If the previous result is an empty array, replace it with 0.5.
     D       Convert all prime factors to decimal.
      F      Flatten the result.
        D    Yield n in decimal.
       ż     Zip the results to both sides, creating a two-column array.
         S   Compute the sum of each column.
             If n is -1, 0, 1, or prime, the sum of the prime factorization's
             digits will be 0.5, and thus unequal to the sum of the decimal array.
             If n < -1, the sum of the prime factorization's digits will be
             positive, while the sum of the decimal array will be negative.
          E  Test both sums for equality.

2
यह एक गंभीर रूप से अच्छा समाधान है जो मुझे कहना है!
एमिग्ना

@ ईमनिगा - यह मैंने क्या किया है, लेकिन एक बेहतर तरीके से कार्यान्वित किया गया है: डी
जोनाथन एलन

@JonathanAllan दुर्भाग्य से मैं जेली नहीं बोलता, इसलिए मुझे पता नहीं है कि आपका कोड क्या करता है :)
18

1
@Emigna - हाँ, मैंने काम करने की योजना बनाई थी कि यह कैसे काम करता है इसे जोड़ने से पहले इसे नीचे गोल्फ कैसे करें।
जोनाथन एलन

9

पायथन 2, 122 115 110 106 बाइट्स

n=m=input()
s=0
for d in range(2,n):
 while n%d<1:n/=d;s+=sum(map(int,`d`))
print n<m>s==sum(map(int,`m`))

डेनिस की बदौलत 4 बाइट बच गईं

इसे ideone.com पर आज़माएँ

व्याख्या

स्टड और आउटपुट पर एक नंबर पढ़ता है Trueयदि संख्या एक स्मिथ संख्या है या Falseयदि यह नहीं है।

n=m=input()                  # stores the number to be checked in n and in m
s=0                          # initializes s, the sum of the sums of digits of prime factors, to 0
for d in range(2,n):         # checks all numbers from 2 to n for prime factors
 while n%d<1:                # while n is divisible by d
                             #   (to include the same prime factor more than once)
  n/=d                       # divide n by d
  s+=sum(map(int,`d`))       # add the sum of the digits of d to s
print                        # print the result: "True" if and only if
      n<m                    #   n was divided at least once, i.e. n is not prime
      >                      #   and m>s (always true) and
      s==sum(map(int,`m`))   #   s is equal to the sum of digits of m (the input)

1
मतदाता - यह समझाने के लिए एक टिप्पणी जोड़ना उपयोगी हो सकता है कि क्यों
जोनाथन एलन

6
@JonathanAllan डाउनवोट को स्वचालित रूप से समुदाय उपयोगकर्ता द्वारा डाला गया था जब जवाब संपादित किया गया था। मैं इसे एक बग मानता हूं
डेनिस

1
अंतिम पंक्ति को फिर से लिखा जा सकता है print n<m>s==sum(map(int,`m`))
डेनिस

@ डेनिस यह जंजीर तुलना का एक बड़ा उपयोग है!
लेविटिलेटिंग

8

ब्रेकीलॉग , 19 बाइट्स

@e+S,?$pPl>1,P@ec+S

इसे ऑनलाइन आज़माएं!

व्याख्या

@e+S,                 S is the sum of the digits of the input.
     ?$pP             P is the list of prime factors of the input.
        Pl>1,         There are more than 1 prime factors.
             P@e      Split each prime factor into a list of digits.
                c     Flatten the list.
                 +S   The sum of this list of digits must be S.

2
@JonathanAllan यह करता है । ब्रेजलॉग में संख्याओं के लिए ऋणात्मक चिन्ह _(तथाकथित कम ऋण ) है।
16


5

PowerShell v3 +, 183 बाइट्स

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)

कोई अंतर्निहित प्रधान जाँच नहीं। कोई बिल्ट-इन फैक्टरिंग नहीं। कोई अंतर्निहित अंक-योग नहीं। सब कुछ हाथ से बनाया। : डी

$nएक पूर्णांक के रूप में इनपुट लेता है , $bएक खाली सरणी के बराबर सेट करता है । यहाँ, $bहमारे प्रमुख कारकों का संग्रह है।

अगला एक forलूप है। हम पहले $aअपने इनपुट नंबर के बराबर सेट करते हैं, और सशर्त तब तक $aकम-से-या-बराबर-से 1. है। यह लूप हमारे प्रमुख कारकों को खोजने वाला है।

हम से पाश 2करने के लिए $a, का उपयोग करता है Where-Object( |?{...}) बाहर निकलने के लिए अभाज्य संख्या है कि यह भी कारक हैं !($a%$_)। उन लोगों को एक आंतरिक-लूप में खिलाया जाता है जो |%{...}कारक को $bविभाजित करता है और विभाजित करता है $a(इस प्रकार हम अंततः प्राप्त करेंगे 1)।

तो, अब हमारे पास हमारे सभी प्रमुख कारक हैं $b। हमारे बूलियन आउटपुट तैयार करने का समय। हमें यह सत्यापित करने की आवश्यकता $nहै -notin $b, क्योंकि यदि यह है कि इसका मतलब है कि $nअभाज्य है, और इसलिए स्मिथ संख्या नहीं है। साथ ही, ( -and) हम यह सुनिश्चित करें कि के बारे में हमारी दो सेट बनाने की जरूरत अंकों रकम हैं -eqUAL। परिणामस्वरूप बूलियन को पाइप लाइन पर छोड़ दिया जाता है और आउटपुट निहित होता है।

NB - -notinऑपरेटर के लिए v3 या नए की आवश्यकता है । मैं अभी भी इनपुट के लिए चल रहा हूं 4937775(यह गणना करने के लिए धीमा है), इसलिए मैं इसे पूरा करूंगा जब वह पूरा हो जाएगा। 3+ घंटे के बाद, मुझे स्टैकओवरफ़्लो त्रुटि मिली। तो, कहीं न कहीं ऊपरी सीमा है। ओह अच्छा।

यह नकारात्मक इनपुट, शून्य, या एक के लिए काम करेगा, क्योंकि -andवसीयत का दाहिना हाथ एक त्रुटि को रोक देगा, जबकि यह अंकों की गणना (नीचे दिखाया गया है) की गणना करने की कोशिश करता है, जो कि $falseमूल्यांकन किए जाने पर आधे जाने का कारण होगा । चूंकि STDERR को डिफ़ॉल्ट रूप से अनदेखा किया जाता है , और सही आउटपुट अभी भी प्रदर्शित होता है, यह ठीक है।


परीक्षण के मामलों

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

3

MATL, 17 बाइट्स

YftV!UsGV!Us=wnqh

सत्य या गलत तरीके से आउटपुट देता है जहां एक सत्य आउटपुट के लिए आवश्यक है कि सभी तत्व गैर-शून्य हों।

इसे ऑनलाइन आज़माएं


@JonathanAllan हाँ। मैं सच्चाई और झूठी की परिभाषा के बारे में थोड़ा जोड़ रहा हूं।
Suever

3

जेली , २ 27 २५ २३ बाइट्स

(आगे गोल्फ शायद निश्चित रूप से संभव है)

ḢDS×
ÆFÇ€SḢ
DS=Ça<2oÆP¬

0झूठे या 1सच्चे के लिए लौटाता है

TryItOnline पर सभी परीक्षण मामले

कैसे?

DS=Ça<2oÆP¬ - main link takes an argument, n
DS          - transform n to a decimal list and sum up
   Ç        - call the previous link (ÆFÇ€SḢ)
  =         - test for equality
     <2     - less than 2?
    a       - logical and
        ÆP  - is prime?
       o    - logical or
          ¬ - not
            - all in all tests if the result of the previous link is equal to the digit
              sum if the number is composite otherwise returns 0.

ÆFÇ€SḢ - link takes an argument, n again
ÆF     - list of list of n's prime factors and their multiplicities
  Ç€   - apply the previous link (ḢDS×) for each
    S  - sum up
     Ḣ - pop head of list (there will only be one item)

ḢDS× - link takes an argument, a factor, multiplicity pair
Ḣ    - pop head, the prime factor - modifies list leaving the multiplicity
 DS  - transform n to a decimal list and sum up
   × - multiply the sum with the multiplicity

3

दरअसल, 18 बाइट्स

दुर्भाग्य से, वास्तव में एक गुणनखंड निर्मित नहीं होता है जो किसी संख्या के प्रमुख कारकों को बहुलता प्रदान करता है, इसलिए मुझे एक साथ हैक करना पड़ा। गोल्फ सुझाव का स्वागत करते हैं। इसे ऑनलाइन आज़माएं!

;w`i$n`MΣ♂≈Σ@$♂≈Σ=

Ungolfing

         Implicit input n.
;w       Duplicate n and get the prime factorization of a copy of n.
`...`M   Map the following function over the [prime, exponent] lists of w.
  i        Flatten the list. Stack: prime, exponent.
  $n       Push str(prime) to the stack, exponent times.
            The purpose of this function is to get w's prime factors to multiplicity.
Σ        sum() the result of the map.
          On a list of strings, this has the same effect as "".join()
♂≈Σ      Convert every digit to an int and sum().
@        Swap the top two elements, bringing other copy of n to TOS.
$♂≈Σ     Push str(n), convert every digit to an int, and sum().
=        Check if the sum() of n's digits is equal 
          to the sum of the sum of the digits of n's prime factors to multiplicity.
         Implicit return.

3

हास्केल, 120 105 बाइट्स

1%_=[];a%x|mod a x<1=x:div a x%x|0<1=a%(x+1)
p z=sum[read[c]|c<-show z]
s x|z<-x%2=z<[x]&&sum(p<$>z)==p x

2

ऑक्टेव, 80 78 बाइट्स

t=num2str(factor(x=input('')))-48;disp(any(t<0)&~sum([num2str(x)-48 -t(t>0)]))

स्पष्टीकरण:

factor(x=input(''))                 % Take input, store as x and factor it
num2str(factor(x=input('')))-48     % Convert it to an array (123 -> [1 2 3]) 
                                    % and store as t
any(t<0)                            % Check if there are several prime factors
                                    % [2 3] -> [2 -16 3]
sum([num2str(x)-48 -t(t>0)])        % Check if sum of prime factor
                                    % is equal the sum of digits

इसे ऑनलाइन आज़माएं ।


1
यही कारण है कि any(t<0)गैर के लिए primality बहुत चालाक है
लुइस Mendo

2

पायथ, 21 बाइट्स

&&>Q1!P_QqsjQTssmjdTP

एक प्रोग्राम जो एक पूर्णांक और प्रिंट Trueया Falseसंबंधित के रूप में इनपुट लेता है ।

इसे ऑनलाइन आज़माएं

यह काम किस प्रकार करता है

&&>Q1!P_QqsjQTssmjdTP  Program. Input: Q
           jQT         Yield digits of the base-10 representation of Q as a list
          s            Add the digits
                    P  Yield prime factors of Q (implicit input fill)
                mjdT   Map base-10 representation across the above, yielding digits of each
                       factor as a list of lists
               s       Flatten the above
              s        Add up the digits
         q             Those two sums are equal
&                      and
  >Q1                  Q>1
 &                     and
     !P_Q              Q is not prime
                       Implicitly print

2

पर्ल 6 , 92 88 87 बाइट्स

{sub f(\i){my \n=first i%%*,2..i-1;n??n~f i/n!!i}
!.is-prime&&$_>1&&.comb.sum==.&f.comb.sum}

{sub f(\i){my \n=first i%%*,2..^i;n??[n,|f i/n]!!|i}
$_>.&f>1&&.comb.sum==.&f.comb.sum}

एक अनाम फ़ंक्शन जो एक बूल लौटाता है।

  • अब 100% मैनुअल फैक्टराइजेशन और प्राइमलिटी चेकिंग करता है।
  • M> । (M) के बाद से, एक जंजीर तुलना के साथ "इनपुट> 1" और "कारकों की संख्या> 1" दोनों का परीक्षण करके कुछ बाइट्स को बचाया ।

( इसे ऑनलाइन आज़माएं )

EDIT: -1 बाइट के लिए धन्यवाद b2gills


2..i-1के रूप में बेहतर वर्तनी है 2..^i
ब्रैड गिलबर्ट

2

जावा 7, 509 506 435 426 419 230 बाइट्स

boolean c(int n){return n<2|p(n)?0>1:d(n)==f(n);}int d(int n){return n>9?n%10+d(n/10):n;}int f(int n){int r=0,i;for(i=1;++i<=n;)for(;n%i<1;n/=i,r+=i>9?d(i):i);return r;}boolean p(int n){int i=2;while(i<n)n=n%i++<1?0:n;return n>1;}

मुझे @BasicallyAlanTuring की टिप्पणी सुननी चाहिए थी ..

यह उन सवालों में से एक है जहां मैंने सोचा कि "जावा + यह = नहीं", मैं इस विचार के लिए अपवित्र था: पी

आह ठीक है .. कुछ प्रोग्रामिंग लैंग्वेज प्राइम-फैक्टर या प्राइम-चेक के लिए सिंगल बाइट का इस्तेमाल करती हैं, लेकिन जावा उनमें से एक नहीं है।

संपादित करें: बाइट्स की मात्रा को अभी रोक दिया है कि मेरे पास इसके बारे में सोचने के लिए कुछ समय था।

अनगुल्ड (सॉर्ट-बंद ..) और परीक्षण के मामले:

इसे यहाँ आज़माएँ।

class M{
  static boolean c(int n){
    return n < 2 | p(n)
            ? 0 > 1 //false
            : d(n) == f(n);
  }

  // Sums digits of int
  static int d(int n) {
    return n > 9
            ? n%10 + d(n/10)
            : n;
  }

  // Convert int to sum of prime-factors
  static int f(int n) {
    int r = 0,
        i;
    for(i = 1; ++i <= n; ){
      for( ; n % i < 1; n /= i,
                        r += i > 9 ? d(i) : i);
    }
    return r;
  }

  // Checks if the int is a prime
  static boolean p(int n){
    int i = 2;
    while(i < n){
      n = n % i++ < 1
           ? 0
           : n;
    }
    return n > 1;
  }

  public static void main(String[] a){
    System.out.println(c(18));
    System.out.println(c(22));
    System.out.println(c(13));
    System.out.println(c(666));
    System.out.println(c(-256));
    System.out.println(c(0));
    System.out.println(c(1));
    System.out.println(c(4937775));
  }
}

आउटपुट:

false
true
false
true
false
false
false
true

2

ब्रेकीलॉग (नया) , 11 बाइट्स

¬ṗ&ẹ+.&ḋcẹ+

इसे ऑनलाइन आज़माएं!

यदि इनपुट एक स्मिथ संख्या है, तो सफल हो जाता है और यदि यह नहीं है तो विफल हो जाता है।

               The input
¬ṗ             is not prime,
  &            and the input's 
   ẹ           digits
    +          sum to
     .         the output variable,
      &        and the input's 
       ḋ       prime factors' (getting prime factors of a number < 1 fails)
        c      concatenated
         ẹ     digits
          +    sum to
               the output variable.



1

पाइके, 16 बाइट्स

Pm[`mbs(sQ[qRlt*

यहाँ यह कोशिश करो!


1
बिना परिणाम के त्रुटिपूर्ण परिणाम से कम2
जोनाथन एलन

@JonathanAllan stdout के लिए कोई आउटपुट गलत नहीं है। यदि चेतावनियों को निष्क्रिय कर दिया जाता है, तो उसे भी नजरअंदाज कर दिया जाता है
ब्लू

मुझे पता था कि हम stderr को अनदेखा कर सकते हैं, लेकिन कोई भी आउटपुट थोड़ा अजीब नहीं लगता ... लेकिन अगर यह स्वीकार्य है तो स्वीकार्य है।
जोनाथन एलन

व्यक्तिगत रूप से मुझे यकीन नहीं है कि यह स्वीकार्य है लेकिन मैं कह सकता हूं कि यह सही है?
ब्लू


1

एपीएल (डायलॉग एक्सटेंडेड) , 36 29 बाइट्स एसबीसीएस

यह जवाब एक नंबर के प्रमुख कारकों को लौटाने के लिए विस्तारित गढ़ के लिए अपनी गोल्फ का श्रेय देता है , और वह है Dyalog यूनिकोड की तुलना में आधार-रूपांतरण पर बेहतर है।

संपादित करें: -7 बाइट्स dzaima के लिए धन्यवाद।

{2>⍵:0⋄(⊃=+/-⊃×2<≢)+⌿10⊤⍵,⍭⍵}

इसे ऑनलाइन आज़माएं!

व्याख्या

{1⋄(3)2}  A dfn, a function in brackets.  is a statement separator.
          The numbers signify the sections in the order they are explained.

2>⍵:0  If we have a number less than 2,
       we immediately return 0 to avoid a DOMAIN ERROR.

+⌿10⊤⍵,⍭⍵
        ⍭⍵  We take the factors of ⍵, our input as our right argument,
      ⍵,    and append it to our input again.
   10      before converting the input and its factors into a matrix of their base-10 digits
            (each row is the places, units, tens, hundreds, etc.)
+⌿         And taking their sum across the columns of the resulting matrix,
            to give us the sum of their digits, their digit-sums.

(⊃=+/-⊃×2<≢)  We run this section over the list of sums of digits above.
 ⊃=+/-⊃       We check if the main digit-sum (of our input)
               Is equal to the sum of our digit-sums
               (minus our main digit-sum that is also still in the list)
        ×2<≢   The trick here is that we can sneak in our composite check
               (if our input is prime there will be only two numbers, 
               the digit-sum of the prime,
               and the digit-sum of its sole prime factor, itself)
               So if we have a prime, we zero our (minus our main sum)
               in the calculation above, so that primes will not succeed in the check.
               We return the result of the check.

29 बाइट्स -{2>⍵:0⋄(⊃=+/-⊃×2<≢)+⌿10⊤⍵,⍭⍵}
dizima


1

सी (जीसीसी) , 139 136 बाइट्स

S(m,i,t,h,_){t=m=m<2?2:m;for(_=h=i=1;m>1;h=1){while(m%++h);for(m/=h;i+=h%10,h/=10;);}while(t%++h);for(m=t;_+=m%10,m/=10;);m=t-h?i==_:0;}

इसे ऑनलाइन आज़माएं!

सीट्स के लिए -3 बाइट्स धन्यवाद

स्पष्टीकरण:

/* 
 * Variable mappings:
 *  is_smith      => S
 *  argument      => m
 *  factor_digits => i
 *  arg_copy      => t
 *  least_factor  => h
 *  digit_sum     => _    
 */
int is_smith(int argument){                     /* S(m,i,t,h,_){ */
    int factor_digits;
    int arg_copy;
    int least_factor;
    int digit_sum;

    /* 
     * The cases of 0 and 1 are degenerate. 
     * Mapping them to a non-degenerate case with the right result.
     */
    if (argument < 2) {                         /* t=m=m<2?2:m; */
        argument = 2;
    }
    arg_copy = argument;

    /* 
     * Initializing these to 1 instead of zero is done for golf reasons.
     * In the end we just compare them, so it doesn't really matter.
     */
    factor_digits = 1;                          /* for(_=h=i=1; */
    digit_sum = 1;

    /* Loop over each prime factor of argument */
    while (argument > 1) {                      /* m>1; */

        /*
         * Find the smallest factor 
         * Note that it is initialized to 1 in the golfed version since prefix
         * increment is used in the modulus operation.
         */
        least_factor = 2;                       /* h=1){ */
        while (argument % least_factor != 0)    /* while(m% */
            least_factor++;                     /* ++h); */
        argument /= least_factor;               /* for(m/=h; */

        /* Add its digit sum to factor_digits */
        while (least_factor > 0) {
            factor_digits += least_factor % 10; /* i+=h%10, */
            least_factor /= 10;                 /* h/=10;) */
        }                                       /* ; */

    }                                           /* } */

    /* In the golfed version we get this for free in the for loop. */
    least_factor = 2;
    while (arg_copy % least_factor != 0)        /* while(t% */
        least_factor++;                         /* ++h); */

    /* Restore the argument */
    argument = arg_copy;                        /* for(m=t; */

    /* Compute the arguments digit sum */
    while (argument > 0) {
        digit_sum += argument % 10;             /* _+=m%10, */
        argument /= 10;                         /* m/=10;) */
    }                                           /* ; */

    /* This return is done by assigning to first argument when golfed. */
                                                /* m= */
    if (arg_copy == least_factor) {             /* t==h? */
        return 0; /* prime input */             /* 0 */
    } else {                                    /* : */
        return digit_sum == factor_digits;      /* i == _ */
    }                                           /* ; */
}                                               /* } */

इसने कुछ बग (जैसे 2 और 3) पेश किए, लेकिन मुझे लगता है कि यह अभी भी प्राप्त होना चाहिए।
लैम्ब्डाटा

के t-h&&i==_बजाय सुझावt-h?i==_:0
छत

0

रैकेट 176 बाइट्स

(define(sd x)(if(= x 0)0(+(modulo x 10)(sd(/(- x(modulo x 10))10)))))
(require math)(define(f N)
(if(=(for/sum((i(factorize N)))(*(sd(list-ref i 0))(list-ref i 1)))(sd N))1 0))

1 रिटर्न अगर सच है और 0 झूठे हैं तो:

(f 27)
1
(f 28)
0
(f 85)
1
(f 86)
0

विस्तृत संस्करण:

(define (sd x)   ; fn to find sum of digits
  (if (= x 0)
      0
      (+ (modulo x 10)
         (sd (/ (- x (modulo x 10)) 10)))))

(require math)
(define (f N)
  (if (= (for/sum ((i (factorize N)))
           (* (sd (list-ref i 0))
              (list-ref i 1)))
         (sd N)) 1 0))


0

एपीएल (एनएआरएस), 33 चार, 66 बाइट्स

{1≥≢k←π⍵:0⋄s←{+/⍎¨⍕⍵}⋄(s⍵)=+/s¨k}

"" "रिटर्न लिस्ट कारक of, मान लें कि इनपुट एक धनात्मक पूर्णांक> = 1 है; परीक्षा:

  h←{1≥≢k←π⍵:0⋄s←{+/⍎¨⍕⍵}⋄(s⍵)=+/s¨k}
  (h¨1..100)/1..100
4 22 27 58 85 94 

0

सी (जीसीसी), 177 बाइट्स

एक फ़ंक्शन को परिभाषित करता है Qजो smith संख्याओं के लिए 0 और गैर-smith संख्याओं के लिए nonzero देता है

#define r return
O(D,i){for(i=0;D>0;i+=D%10,D-=D%10,D/=10);r i;}D(O,o){for(o=1;o<O;)if(O%++o<1)r o;r O;}Q(p,q,i,j){if(p^(q=D(i=p))){for(j=0;p>1;q=D(p/=q))j+=O(q);r j^O(i);}r 1;}

इसे ऑनलाइन आज़माएं!

स्पष्टीकरण:

// Return the sum of digits of D if D > 0, otherwise 0
O(D,i){
    // While D is greater than 0:
    // Add the last digit of D to i, and remove the last digit from D
    for(i=0;D>0;i+=D%10,D-=D%10,D/=10);
    return i;
}
// Return the smallest prime factor of O if O>1 else O
D(O,o){
    // Iterate over numbers less than O
    for(o=1;o<O;)
        // If O is divisible by o return o
        if(O%++o<1)
            return o;
    // Otherwise return O
    return O;
}
Q(p,q,i,j){
    // Set q to D(p) and i to p
    // If p != D(p) (i.e, p is composite and > 0)
    if(p^(q=D(i=p))){
        // Iterate over the prime factors of p and store their digit sum in j
        for(j=0;p>1;q=D(p/=q))
            j+=O(q);
        // i is the original value of p. If O(i)^j == 0, O(i) == j
        return j^O(i);
    }
    // If p was composite or < 0, return 1
    return 1;
}


हमारी साइट का प्रयोग करके, आप स्वीकार करते हैं कि आपने हमारी Cookie Policy और निजता नीति को पढ़ और समझा लिया है।
Licensed under cc by-sa 3.0 with attribution required.