{"id":5314,"date":"2023-01-05T12:25:58","date_gmt":"2023-01-05T04:25:58","guid":{"rendered":"https:\/\/badgameshow.com\/steven\/?p=5314"},"modified":"2023-01-05T12:25:58","modified_gmt":"2023-01-05T04:25:58","slug":"%e4%bd%bf%e7%94%a8typescript%e9%99%a3%e5%88%97%e6%aa%a2%e6%b8%ac%e8%b3%aa%e6%95%b8%ef%bc%9aisprime%e5%87%bd%e6%95%b8","status":"publish","type":"post","link":"https:\/\/badgameshow.com\/steven\/typescript\/%e4%bd%bf%e7%94%a8typescript%e9%99%a3%e5%88%97%e6%aa%a2%e6%b8%ac%e8%b3%aa%e6%95%b8%ef%bc%9aisprime%e5%87%bd%e6%95%b8\/","title":{"rendered":"\u4f7f\u7528TypeScript\u9663\u5217\u6aa2\u6e2c\u8cea\u6578\uff1aisPrime\u51fd\u6578"},"content":{"rendered":"<p><meta name=\"keywords\" content=\"TypeScript, \u9663\u5217, \u8cea\u6578, isPrime\"><\/p>\n<h1>\u4f7f\u7528 TypeScript \u9663\u5217\u6c42\u8cea\u6578(isPrime)<\/h1>\n<p>\u8cea\u6578\u662f\u4e00\u500b\u6578\u5b78\u6982\u5ff5\uff0c\u5b83\u662f\u4e00\u500b\u53ea\u80fd\u88ab1\u548c\u5b83\u672c\u8eab\u6574\u9664\u7684\u6b63\u6574\u6578\u3002\u8cea\u6578\u7684\u6982\u5ff5\u5728\u6578\u5b78\u4e2d\u975e\u5e38\u91cd\u8981\uff0c\u5b83\u5011\u88ab\u7528\u65bc\u8a31\u591a\u61c9\u7528\uff0c\u4f8b\u5982\u52a0\u5bc6\u6280\u8853\u548c\u8a08\u7b97\u6a5f\u79d1\u5b78\u3002<\/p>\n<p>TypeScript\u662f\u4e00\u7a2e\u5f37\u578b\u5225\u7684JavaScript\u8173\u672c\u8a9e\u8a00\uff0c\u5b83\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u64b0\u5beb\u7a0b\u5f0f\u78bc\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u3002\u5728\u672c\u6587\u4e2d\uff0c\u6211\u5011\u5c07\u4f7f\u7528TypeScript\u4f86\u64b0\u5beb\u4e00\u500b\u51fd\u5f0f\uff0c\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\u3002<\/p>\n<h2>TypeScript \u9663\u5217\u6c42\u8cea\u6578(isPrime)<\/h2>\n<p>\u9996\u5148\uff0c\u6211\u5011\u9700\u8981\u5b9a\u7fa9\u4e00\u500b\u51fd\u5f0f\uff0c\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\u3002\u6211\u5011\u5c07\u4f7f\u7528TypeScript\u7684\u9663\u5217\u529f\u80fd\u4f86\u5be6\u73fe\u9019\u500b\u51fd\u5f0f\uff1a<\/p>\n<pre class=\"brush: typescript\">\nfunction isPrime(num: number): boolean {\n    \/\/ \u5982\u679c\u6578\u5b57\u5c0f\u65bc2\uff0c\u5247\u4e0d\u662f\u8cea\u6578\n    if (num < 2) {\n        return false;\n    }\n\n    \/\/ \u5f9e2\u958b\u59cb\u6aa2\u67e5\uff0c\u76f4\u5230num\u7684\u5e73\u65b9\u6839\n    for (let i = 2; i <= Math.sqrt(num); i++) {\n        \/\/ \u5982\u679cnum\u53ef\u4ee5\u88abi\u6574\u9664\uff0c\u5247\u4e0d\u662f\u8cea\u6578\n        if (num % i === 0) {\n            return false;\n        }\n    }\n\n    \/\/ \u5982\u679c\u6c92\u6709\u627e\u5230\u53ef\u4ee5\u6574\u9664\u7684\u6578\uff0c\u5247\u662f\u8cea\u6578\n    return true;\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u51fd\u5f0f\u63a5\u53d7\u4e00\u500b\u6578\u5b57\u4f5c\u70ba\u53c3\u6578\uff0c\u4e26\u8fd4\u56de\u4e00\u500b\u5e03\u6797\u503c\uff0c\u8868\u793a\u8a72\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\u3002\u51fd\u5f0f\u7684\u7b2c\u4e00\u884c\u6aa2\u67e5\u8a72\u6578\u5b57\u662f\u5426\u5c0f\u65bc2\uff0c\u5982\u679c\u662f\uff0c\u5247\u8fd4\u56defalse\uff0c\u8868\u793a\u8a72\u6578\u5b57\u4e0d\u662f\u8cea\u6578\u3002<\/p>\n<p>\u63a5\u4e0b\u4f86\uff0c\u51fd\u5f0f\u6703\u5f9e2\u958b\u59cb\u6aa2\u67e5\uff0c\u76f4\u5230\u8a72\u6578\u5b57\u7684\u5e73\u65b9\u6839\u70ba\u6b62\u3002\u5982\u679c\u5728\u9019\u500b\u904e\u7a0b\u4e2d\u627e\u5230\u53ef\u4ee5\u6574\u9664\u8a72\u6578\u5b57\u7684\u6578\uff0c\u5247\u8fd4\u56defalse\uff0c\u8868\u793a\u8a72\u6578\u5b57\u4e0d\u662f\u8cea\u6578\u3002\u5982\u679c\u6c92\u6709\u627e\u5230\u53ef\u4ee5\u6574\u9664\u7684\u6578\uff0c\u5247\u8fd4\u56detrue\uff0c\u8868\u793a\u8a72\u6578\u5b57\u662f\u8cea\u6578\u3002<\/p>\n<p>\u73fe\u5728\uff0c\u6211\u5011\u53ef\u4ee5\u4f7f\u7528\u9019\u500b\u51fd\u5f0f\u4f86\u6aa2\u67e5\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\uff1a<\/p>\n<pre class=\"brush: typescript\">\nlet num = 11;\nlet isPrime = isPrime(num);\n\nif (isPrime) {\n    console.log(`<span class=\"katex math inline\">{num} is a prime number.`);\n} else {\n    console.log(`<\/span>{num} is not a prime number.`);\n}\n<\/pre>\n<p>\u4e0a\u9762\u7684\u7a0b\u5f0f\u78bc\u6703\u5c07\u4e00\u500b\u6578\u5b57\u50b3\u905e\u7d66isPrime\u51fd\u5f0f\uff0c\u4e26\u6aa2\u67e5\u8a72\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\u3002\u5982\u679c\u662f\uff0c\u5247\u6703\u5728\u63a7\u5236\u53f0\u4e2d\u986f\u793a\u4e00\u500b\u6d88\u606f\uff0c\u8868\u793a\u8a72\u6578\u5b57\u662f\u8cea\u6578\uff1b\u5982\u679c\u4e0d\u662f\uff0c\u5247\u6703\u986f\u793a\u4e00\u500b\u6d88\u606f\uff0c\u8868\u793a\u8a72\u6578\u5b57\u4e0d\u662f\u8cea\u6578\u3002<\/p>\n<p><!--more--><\/p>\n<p>\u4f7f\u7528TypeScript\u7684\u9663\u5217\u529f\u80fd\uff0c\u6211\u5011\u53ef\u4ee5\u5f88\u5bb9\u6613\u5730\u5be6\u73fe\u4e00\u500b\u51fd\u5f0f\uff0c\u7528\u65bc\u6aa2\u67e5\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\u3002\u9019\u500b\u51fd\u5f0f\u53ef\u4ee5\u8b93\u958b\u767c\u8005\u66f4\u5bb9\u6613\u5730\u6aa2\u67e5\u4e00\u500b\u6578\u5b57\u662f\u5426\u70ba\u8cea\u6578\uff0c\u4e26\u63d0\u4f9b\u66f4\u591a\u7684\u529f\u80fd\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u672c\u6587\u5c07\u4ecb\u7d39\u5982\u4f55\u4f7f\u7528TypeScript\u9663\u5217\u7684isPrime\u51fd\u6578\u4f86\u6aa2\u6e2c\u8cea\u6578\uff0c\u4e26\u63d0\u4f9b\u4e00\u4e9b\u7c21\u55ae\u7684\u7bc4\u4f8b\u4f86\u89e3\u91cb\u5982\u4f55\u4f7f\u7528\u8a72\u51fd\u6578\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[187],"tags":[186],"class_list":["post-5314","post","type-post","status-publish","format-standard","hentry","category-typescript","tag-typescript"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack-related-posts":[],"jetpack_shortlink":"https:\/\/wp.me\/pcFK27-1nI","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5314","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/comments?post=5314"}],"version-history":[{"count":1,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5314\/revisions"}],"predecessor-version":[{"id":5315,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/posts\/5314\/revisions\/5315"}],"wp:attachment":[{"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/media?parent=5314"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/categories?post=5314"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/badgameshow.com\/steven\/wp-json\/wp\/v2\/tags?post=5314"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}