{"id":781,"date":"2024-11-07T13:51:05","date_gmt":"2024-11-07T05:51:05","guid":{"rendered":"https:\/\/fwq.ai\/blog\/781\/"},"modified":"2024-11-07T13:51:05","modified_gmt":"2024-11-07T05:51:05","slug":"%e5%af%bb%e6%89%be%e5%8f%8c%e9%87%8d%e5%ad%98%e5%9c%a8%e5%b9%b6%e5%ba%94%e7%94%a8-luhn-%e7%ae%97%e6%b3%95","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/781\/","title":{"rendered":"\u5bfb\u627e\u53cc\u91cd\u5b58\u5728\u5e76\u5e94\u7528 Luhn \u7b97\u6cd5"},"content":{"rendered":"<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-1125\" src=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172873123170068.jpg\" width=\"800\" height=\"320\" srcset=\"https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172873123170068.jpg 800w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172873123170068-300x120.jpg 300w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172873123170068-768x307.jpg 768w, https:\/\/fwq.ai\/blog\/wp-content\/uploads\/2024\/11\/172873123170068-670x268.jpg 670w\" sizes=\"auto, (max-width: 800px) 100vw, 800px\" title=\"\u5bfb\u627e\u53cc\u91cd\u5b58\u5728\u5e76\u5e94\u7528 Luhn \u7b97\u6cd5\u63d2\u56fe\" alt=\"\u5bfb\u627e\u53cc\u91cd\u5b58\u5728\u5e76\u5e94\u7528 Luhn \u7b97\u6cd5\u63d2\u56fe\" \/><\/p>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u51b3 perl weekly challenge #290 \u4e2d\u7684\u4e24\u4e2a\u5f15\u4eba\u5165\u80dc\u7684\u4efb\u52a1\uff1a\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u53cc\u91cd\u5b58\u5728\u5e76\u5b9e\u73b0 luhn \u7b97\u6cd5\u8fdb\u884c\u9a8c\u8bc1\u3002\u6211\u4eec\u5c06\u5728 perl \u548c go \u4e2d\u5b9e\u73b0\u89e3\u51b3\u65b9\u6848\u3002<\/p>\n<h2> \u76ee\u5f55 <\/h2>\n<ul>\n<li>\u53cc\u91cd\u5b58\u5728<\/li>\n<li>\u5362\u6069\u7b97\u6cd5<\/li>\n<li>\u7ed3\u8bba<\/li>\n<\/ul>\n<h2> \u53cc\u91cd\u5b58\u5728 <\/h2>\n<p>\u7b2c\u4e00\u4e2a\u4efb\u52a1\u6d89\u53ca\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e24\u4e2a\u7d22\u5f15 $i \u548c $j \u4f7f\u5f97\uff1a<\/p>\n<pre>1. $i != $j\n2. 0 &lt;= ($i, $j) &lt; scalar @ints\n3. $ints[i] = 2 * $ints[j]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u4efb\u52a1\u63cf\u8ff0 <\/h3>\n<p><strong>\u8f93\u5165\uff1a<\/strong> \u6574\u6570\u6570\u7ec4\uff0c@ints\u3002<\/p>\n<p><strong>\u8f93\u51fa\uff1a<\/strong> \u5982\u679c\u6ee1\u8db3\u6761\u4ef6\u5219\u4e3a true\uff1b\u5426\u5219\uff0c\u9519\u8bef\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>input: @ints = (6, 2, 3, 3)\noutput: true\n\nfor $i = 0, $j = 2\n$ints[$i] = 6 =&gt; 2 * 3 =&gt;  2 * $ints[$j]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>input: @ints = (3, 1, 4, 13)\noutput: false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>input: @ints = (2, 1, 4, 2)\noutput: true\n\nfor $i = 2, $j = 3\n$ints[$i] = 4 =&gt; 2 * 2 =&gt;  2 * $ints[$j]\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u89e3\u51b3\u65b9\u6848 <\/h3>\n<p><strong>perl \u5b9e\u73b0<\/strong><br \/> \u5728 perl \u5b9e\u73b0\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528\u54c8\u5e0c\u6765\u8ddf\u8e2a\u770b\u5230\u7684\u6574\u6570\uff0c\u5e76\u68c0\u67e5\u54c8\u5e0c\u4e2d\u662f\u5426\u5b58\u5728\u5f53\u524d\u6570\u5b57\u7684\u4e00\u534a\u6216\u4e24\u500d\u3002<\/p>\n<pre>sub double_exist {\n    my %seen;\n\n    foreach my $num (@_) {\n        return 1 if exists $seen{$num \/ 2} || exists $seen{$num * 2};\n        $seen{$num} = 1;\n    }\n\n    return 0;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>go \u5b9e\u65bd<\/strong><br \/> go \u5b9e\u73b0\u9075\u5faa\u7c7b\u4f3c\u7684\u903b\u8f91\uff0c\u4f7f\u7528\u6620\u5c04\u6765\u8ddf\u8e2a\u552f\u4e00\u6574\u6570\u3002<\/p>\n<pre>func doubleexist(ints []int) bool {\n    seen := make(map[int]bool)\n\n    for _, num := range ints {\n        if (num%2 == 0 &amp;&amp; seen[num\/2]) || seen[num*2] {\n            return true\n        }\n        seen[num] = true\n    }\n\n    return false\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u5362\u6069\u7b97\u6cd5 <\/h2>\n<p>\u7b2c\u4e8c\u4e2a\u4efb\u52a1\u6d89\u53ca\u5b9e\u65bd luhn \u7b97\u6cd5\u6765\u9a8c\u8bc1\u6570\u5b57\u5b57\u7b26\u4e32\uff0c\u5ffd\u7565\u975e\u6570\u5b57\u5b57\u7b26\u3002\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u88ab\u5355\u72ec\u89c6\u4e3a\u6709\u6548\u8d1f\u8f7d\u3002<\/p>\n<h3> \u4efb\u52a1\u63cf\u8ff0 <\/h3>\n<p>\u7ed9\u4f60\u4e00\u4e2a\u5b57\u7b26\u4e32 str \uff0c\u5176\u4e2d\u5305\u542b\u6570\u5b57\uff08\u53ef\u80fd\u8fd8\u6709\u5176\u4ed6\u53ef\u4ee5\u5ffd\u7565\u7684\u5b57\u7b26\uff09\u3002\u6700\u540e\u4e00\u4f4d\u6570\u5b57\u88ab\u89c6\u4e3a\u6709\u6548\u8d1f\u8f7d\u5e76\u5355\u72ec\u5904\u7406\u3002<\/p>\n<ol>\n<li>\u4ece\u53f3\u6570\u8d77\uff0c\u5c06\u5269\u4f59\u6570\u5b57\u7684\u7b2c\u4e00\u4e2a\u3001\u7b2c\u4e09\u4e2a\u7b49\u7684\u503c\u52a0\u500d\u3002<\/li>\n<li>\u5bf9\u4e8e\u73b0\u5728\u5927\u4e8e 9 \u7684\u6bcf\u4e2a\u503c\uff0c\u5c06\u5176\u6570\u5b57\u76f8\u52a0\u3002<\/li>\n<li>\u6b63\u786e\u7684\u6821\u9a8c\u4f4d\u662f\u5f53\u6dfb\u52a0\u5230\u6240\u6709\u503c\u7684\u603b\u548c\u65f6\uff0c\u4f7f\u603b\u6a21 10 \u4e3a\u96f6\u7684\u4f4d\u3002<\/li>\n<\/ol>\n<p>\u5982\u679c\u6709\u6548\u8d1f\u8f7d\u7b49\u4e8e\u6b63\u786e\u7684\u6821\u9a8c\u4f4d\uff0c\u5219\u8fd4\u56de true\uff1b\u5426\u5219\uff0c\u8fd4\u56de false\u3002<\/p>\n<p><strong>\u793a\u4f8b\uff1a<\/strong><\/p>\n<pre>input: \"17893729974\"\noutput: true\n\npayload is 4.\n\ndigits from the right:\n\n7 * 2 = 14, sum = 5\n9 = 9\n9 * 2 = 18, sum = 9\n2 = 2\n7 * 2 = 14, sum = 5\n3 = 3\n9 * 2 = 18, sum = 9\n8 = 8\n7 * 2 = 14, sum = 5\n1 = 1\n\nsum of all values = 56, so 4 must be added to bring the total mod 10 to zero. the payload is indeed 4.\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>input: \"4137 8947 1175 5904\"\noutput: true\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<pre>input: \"4137 8974 1175 5904\"\noutput: false\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h3> \u89e3\u51b3\u65b9\u6848 <\/h3>\n<p><strong>perl \u5b9e\u73b0<\/strong><br \/> perl \u5b9e\u73b0\u5904\u7406\u8f93\u5165\u5b57\u7b26\u4e32\u4ee5\u5ffd\u7565\u975e\u6570\u5b57\u5b57\u7b26\uff0c\u7136\u540e\u5e94\u7528 luhn \u7b97\u6cd5\u6765\u9a8c\u8bc1\u6570\u5b57\u3002<\/p>\n<pre>sub luhn_check {\n    my ($str) = @_;\n    $str =~ s\/[^0-9]\/\/g;\n\n    my $payload = substr($str, -1);\n    my $sum = 0;\n    my $length = length($str);\n\n    for (my $i = 0; $i &lt; $length - 1; $i++) {\n        my $digit = substr($str, $length - 2 - $i, 1);\n        if ($i % 2 == 0) {\n            $digit *= 2;\n            $digit -= 9 if $digit &gt; 9;\n        }\n        $sum += $digit;\n    }\n\n    my $check_digit = (10 - ($sum % 10)) % 10;\n\n    return $payload == $check_digit ? 1 : 0;\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p><strong>go \u5b9e\u65bd<\/strong><br \/> go \u7248\u672c\u5b9e\u73b0\u4e86\u76f8\u540c\u7684\u903b\u8f91\uff0c\u5229\u7528 unicode \u5305\u8fc7\u6ee4\u6389\u975e\u6570\u5b57\u5b57\u7b26\u3002<\/p>\n<pre>func luhnCheck(str string) bool {\n    sum := 0\n    payload := 0\n    digits := []int{}\n\n    for _, char := range str {\n        if unicode.IsDigit(char) {\n            digit := int(char - '0')\n            digits = append(digits, digit)\n        }\n    }\n\n    if len(digits) == 0 {\n        return false\n    }\n\n    payload = digits[len(digits)-1]\n\n    for i := 0; i &lt; len(digits)-1; i++ {\n        digit := digits[i]\n        if (len(digits)-2-i)%2 == 0 {\n            digit *= 2\n            if digit &gt; 9 {\n                digit -= 9\n            }\n        }\n        sum += digit\n    }\n\n    checkDigit := (10 - (sum % 10)) % 10\n\n    return payload == checkDigit\n}\n<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<h2> \u7ed3\u8bba <\/h2>\n<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u63a2\u8ba8\u4e86\u4e24\u4e2a\u6709\u8da3\u7684\u7f16\u7a0b\u6311\u6218\uff1a\u67e5\u627e\u6570\u7ec4\u4e2d\u7684\u53cc\u91cd\u5b58\u5728\u4ee5\u53ca\u5b9e\u73b0 luhn \u7b97\u6cd5\u8fdb\u884c\u9a8c\u8bc1\u3002\u8fd9\u4e9b\u4efb\u52a1\u5f3a\u8c03\u4e86\u4e0d\u540c\u7684\u7f16\u7a0b\u8bed\u8a00\u5982\u4f55\u7528\u81ea\u5df1\u72ec\u7279\u7684\u65b9\u6cd5\u89e3\u51b3\u7c7b\u4f3c\u7684\u95ee\u9898\u3002\u6211\u5e0c\u671b\u8fd9\u4e9b\u4f8b\u5b50\u80fd\u591f\u6fc0\u52b1\u60a8\u66f4\u6df1\u5165\u5730\u7814\u7a76 perl \u548c go\uff01<\/p>\n<p>\u60a8\u53ef\u4ee5\u5728 github \u4e0a\u627e\u5230\u5b8c\u6574\u7684\u4ee3\u7801\uff0c\u5305\u62ec\u6d4b\u8bd5\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u5bfb\u627e\u53cc\u91cd\u5b58\u5728\u5e76\u5e94\u7528 Luhn \u7b97\u6cd5\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728\u672c\u6587\u4e2d\uff0c\u6211\u4eec\u5c06\u89e3\u51b3 perl weekly challenge #290 \u4e2d\u7684\u4e24\u4e2a\u5f15\u4eba\u5165\u80dc\u7684\u4efb\u52a1\uff1a\u68c0\u67e5\u6570\u7ec4\u4e2d\u662f\u5426\u5b58\u5728\u53cc\u91cd\u5b58\u5728\u5e76\u5b9e\u73b0 luhn \u7b97\u6cd5\u8fdb\u884c\u9a8c\u8bc1\u3002\u6211\u4eec\u5c06\u5728 perl \u548c go \u4e2d\u5b9e\u73b0\u89e3\u51b3\u65b9\u6848\u3002 \u76ee\u5f55 \u53cc\u91cd\u5b58\u5728 \u5362\u6069\u7b97\u6cd5 \u7ed3\u8bba \u53cc\u91cd\u5b58\u5728 \u7b2c\u4e00\u4e2a\u4efb\u52a1\u6d89\u53ca\u67e5\u627e\u662f\u5426\u5b58\u5728\u4e24\u4e2a\u7d22\u5f15 $i \u548c $j \u4f7f\u5f97\uff1a 1. $i != $j 2. 0 &lt;= ($i, $j) &lt; scalar @ints 3. $ints[i] = 2 * $ints[j] \u767b\u5f55\u540e\u590d\u5236 \u4efb\u52a1\u63cf\u8ff0 \u8f93\u5165\uff1a \u6574\u6570\u6570\u7ec4\uff0c@ints\u3002 \u8f93\u51fa\uff1a \u5982\u679c\u6ee1\u8db3\u6761\u4ef6\u5219\u4e3a true\uff1b\u5426\u5219\uff0c\u9519\u8bef\u3002 \u793a\u4f8b\uff1a input: @ints = (6, 2, 3, 3) [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-781","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/781","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=781"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/781\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=781"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=781"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=781"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}