โปรแกรมเมอร์หลายคนชอบที่จะแก้ปัญหาทางคณิตศาสตร์ที่ยุ่งยากโดยใช้โค้ด ช่วยฝึกฝนจิตใจและพัฒนาทักษะการแก้ปัญหา ในบทความนี้ คุณจะได้เรียนรู้วิธีค้นหาสี่เหลี่ยมและลูกบาศก์ที่สมบูรณ์แบบ n-digit ที่เล็กที่สุดและใหญ่ที่สุดโดยใช้ Python, C++ และ JavaScript แต่ละตัวอย่างยังมีเอาต์พุตตัวอย่างสำหรับค่าต่างๆ
N-Digit Perfect Squares ที่เล็กที่สุดและใหญ่ที่สุด
คำชี้แจงปัญหา
คุณได้รับจำนวนเต็มnและคุณต้องหาตัวเลข n หลักที่เล็กที่สุดและใหญ่ที่สุดที่เป็นกำลังสองสมบูรณ์
ตัวอย่างที่ 1 : ให้ n = 2
กำลังสองสมบูรณ์ 2 หลักที่เล็กที่สุดคือ 16 และกำลังสองสมบูรณ์ 2 หลักที่ใหญ่ที่สุดคือ 81
ดังนั้นผลลัพธ์ที่ได้คือ:
สี่เหลี่ยมจัตุรัสสมบูรณ์ 2 หลักที่เล็กที่สุด: 16
สี่เหลี่ยมจัตุรัสสมบูรณ์ 2 หลักที่ใหญ่ที่สุด: 81
ตัวอย่างที่ 2 : ให้ n = 3
กำลังสองสมบูรณ์ 3 หลักที่เล็กที่สุดคือ 100 และกำลังสองสมบูรณ์ 3 หลักที่ใหญ่ที่สุดคือ 961
ดังนั้นผลลัพธ์ที่ได้คือ:
กำลังสองสมบูรณ์ 3 หลักที่เล็กที่สุด: 100
สี่เหลี่ยมจัตุรัสสมบูรณ์ 3 หลักที่ใหญ่ที่สุด: 961
แนวทางการแก้ปัญหา
คุณสามารถหากำลังสองสมบูรณ์เลข n ที่เล็กที่สุดได้โดยใช้สูตรต่อไปนี้:
pow(ceil(sqrt(pow(10, n – 1))), 2)
และในการหากำลังสองสมบูรณ์ n หลักที่ใหญ่ที่สุด ให้ใช้สูตรต่อไปนี้:
pow(ceil(sqrt(pow(10, n))) – 1, 2)
โปรแกรม C++ เพื่อค้นหา N-Digit Perfect Squares ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรม C++ เพื่อค้นหากำลังสองสมบูรณ์ n หลักที่เล็กที่สุดและใหญ่ที่สุด:
// C++ program to find the smallest and largest // n-digit perfect squares #include using namespace std; void findPerfectSquares(int n) { cout < "smallest="">< n="">< "-digit="" perfect="" square:="" "="">< pow(ceil(sqrt(pow(10,="" n="" -="" 1))),="" 2)=""><> cout < "largest="" "="">< n="">< "-digit="" perfect="" square:="" "="">< pow(ceil(sqrt(pow(10,="" n)))="" -="" 1,="" 2)=""><> } int main() { int n1 = 1; cout < "number="" of="" digits:="" "="">< n1=""><> findPerfectSquares(n1); int n2 = 2; cout < "number="" of="" digits:="" "="">< n2=""><> findPerfectSquares(n2); int n3 = 3; cout < "number="" of="" digits:="" "="">< n3=""><> findPerfectSquares(n3); int n4 = 4; cout < "number="" of="" digits:="" "="">< n4=""><> findPerfectSquares(n4); return 0; }
เอาท์พุต :
Number of digits: 1 Smallest 1-digit perfect square: 1 Largest 1-digit perfect square: 9 Number of digits: 2 Smallest 2-digit perfect square: 16 Largest 2-digit perfect square: 81 Number of digits: 3 Smallest 3-digit perfect square: 100 Largest 3-digit perfect square: 961 Number of digits: 4 Smallest 4-digit perfect square: 1024 Largest 4-digit perfect square: 9801
ที่เกี่ยวข้อง: วิธีการคำนวณมูลค่าของ nCr
โปรแกรม Python เพื่อค้นหา N-Digit Perfect Squares ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรม Python เพื่อค้นหากำลังสองสมบูรณ์ n-digit ที่เล็กที่สุดและใหญ่ที่สุด:
# Python program to find the smallest and largest # n-digit perfect squares import math def findPerfectSquares(n): print("Smallest ", n,"-digit perfect square:", pow(math.ceil(math.sqrt(pow(10, n - 1))), 2)) print("Largest ", n,"-digit perfect square:", pow(math.ceil(math.sqrt(pow(10, n))) - 1, 2)) n1 = 1 print("Number of digits:", n1) findPerfectSquares(n1) n2 = 2 print("Number of digits:", n2) findPerfectSquares(n2) n3 = 3 print("Number of digits:", n3) findPerfectSquares(n3) n4 = 4 print("Number of digits:", n4) findPerfectSquares(n4)
เอาท์พุต :
Number of digits: 1 Smallest 1 -digit perfect square: 1 Largest 1 -digit perfect square: 9 Number of digits: 2 Smallest 2 -digit perfect square: 16 Largest 2 -digit perfect square: 81 Number of digits: 3 Smallest 3 -digit perfect square: 100 Largest 3 -digit perfect square: 961 Number of digits: 4 Smallest 4 -digit perfect square: 1024 Largest 4 -digit perfect square: 9801
ที่เกี่ยวข้อง: วิธีค้นหาตัวเลขที่ใหญ่ที่สุดและเล็กที่สุดของตัวเลขด้วยการเขียนโปรแกรม
โปรแกรม JavaScript เพื่อค้นหา N-Digit Perfect Squares ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรม JavaScript เพื่อค้นหากำลังสองสมบูรณ์ n หลักที่เล็กที่สุดและใหญ่ที่สุด:
// JavaScript program to find the smallest and largest // n-digit perfect squares function findPerfectSquares(n) { document.write("Smallest " + n + "-digit perfect square: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n - 1))), 2) + " "); document.write("Largest " + n + "-digit perfect square: " + Math.pow(Math.ceil(Math.sqrt(Math.pow(10, n))) - 1, 2) + " "); } var n1 = 1; document.write("Number of digits: " + n1 + " "); findPerfectSquares(n1); var n2 = 2; document.write("Number of digits: " + n2 + " "); findPerfectSquares(n2); var n3 = 3; document.write("Number of digits: " + n3 + " "); findPerfectSquares(n3); var n4 = 4; document.write("Number of digits: " + n4 + " "); findPerfectSquares(n4);
เอาท์พุต :
Number of digits: 1 Smallest 1-digit perfect square: 1 Largest 1-digit perfect square: 9 Number of digits: 2 Smallest 2-digit perfect square: 16 Largest 2-digit perfect square: 81 Number of digits: 3 Smallest 3-digit perfect square: 100 Largest 3-digit perfect square: 961 Number of digits: 4 Smallest 4-digit perfect square: 1024 Largest 4-digit perfect square: 9801
N-Digit Perfect Cubes ที่เล็กที่สุดและใหญ่ที่สุด
คำชี้แจงปัญหา
คุณได้รับจำนวนเต็มnคุณต้องหาตัวเลข n หลักที่เล็กที่สุดและใหญ่ที่สุดที่เป็นลูกบาศก์ที่สมบูรณ์แบบเช่นกัน
ตัวอย่างที่ 1 : ให้ n = 2
ลูกบาศก์ที่สมบูรณ์แบบ 2 หลักที่เล็กที่สุดคือ 27 และลูกบาศก์ที่สมบูรณ์แบบ 2 หลักที่ใหญ่ที่สุดคือ 64
ดังนั้นผลลัพธ์ที่ได้คือ:
ลูกบาศก์ที่สมบูรณ์แบบ 2 หลักที่เล็กที่สุด: 27
ลูกบาศก์ที่สมบูรณ์แบบ 2 หลักที่ใหญ่ที่สุด: 64
ตัวอย่างที่ 2 : ให้ n = 3
ลูกบาศก์สมบูรณ์ 3 หลักที่เล็กที่สุดคือ 120 และลูกบาศก์สมบูรณ์ 3 หลักที่ใหญ่ที่สุดคือ 729
ดังนั้นผลลัพธ์ที่ได้คือ:
ลูกบาศก์ที่สมบูรณ์แบบ 3 หลักที่เล็กที่สุด: 125
ลูกบาศก์ที่สมบูรณ์แบบ 3 หลักที่ใหญ่ที่สุด: 729
แนวทางการแก้ปัญหา
คุณสามารถหาลูกบาศก์ที่สมบูรณ์แบบ n-digit ที่เล็กที่สุดได้โดยใช้สูตรต่อไปนี้:
pow(ceil(cbrt(pow(10, (n – 1)))), 3)
และในการหาลูกบาศก์สมบูรณ์ n หลักที่ใหญ่ที่สุด ให้ใช้สูตรต่อไปนี้:
pow(ceil(cbrt(pow(10, (n))))-1, 3)
โปรแกรม C++ เพื่อค้นหา N-Digit Perfect Cubes ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรม C++ เพื่อค้นหาลูกบาศก์ที่สมบูรณ์แบบ n-digit ที่เล็กที่สุดและใหญ่ที่สุด:
// C++ program to find the smallest and largest // n-digit perfect cubes #include using namespace std; void findPerfectCubes(int n) { cout < "smallest="">< n="">< "-digit="" perfect="" cube:="" "="">< pow(ceil(cbrt(pow(10,="" (n="" -="" 1)))),="" 3)=""><> cout < "largest="" "="">< n="">< "-digit="" perfect="" cube:="" "="">< (int)pow(ceil(cbrt(pow(10,="" (n))))="" -="" 1,="" 3)=""><> } int main() { int n1 = 1; cout < "number="" of="" digits:="" "="">< n1=""><> findPerfectCubes(n1); int n2 = 2; cout < "number="" of="" digits:="" "="">< n2=""><> findPerfectCubes(n2); int n3 = 3; cout < "number="" of="" digits:="" "="">< n3=""><> findPerfectCubes(n3); int n4 = 4; cout < "number="" of="" digits:="" "="">< n4=""><> findPerfectCubes(n4); return 0; }
เอาท์พุต :
Number of digits: 1 Smallest 1-digit perfect cube: 1 Largest 1-digit perfect cube: 8 Number of digits: 2 Smallest 2-digit perfect cube: 27 Largest 2-digit perfect cube: 64 Number of digits: 3 Smallest 3-digit perfect cube: 125 Largest 3-digit perfect cube: 729 Number of digits: 4 Smallest 4-digit perfect cube: 1000 Largest 4-digit perfect cube: 9261
โปรแกรม Python เพื่อค้นหา N-Digit Perfect Cubes ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรม Python เพื่อค้นหาลูกบาศก์ที่สมบูรณ์แบบ n-digit ที่เล็กที่สุดและใหญ่ที่สุด:
# Python program to find the smallest and largest # n-digit perfect cubes import math def findPerfectCubes(n): print("Smallest ", n,"-digit perfect cube:", pow(math.ceil((pow(10, (n - 1))) ** (1 / 3)), 3) ) print("Largest ", n,"-digit perfect cube:", pow(math.ceil((pow(10, (n))) ** (1 / 3)) - 1, 3)) n1 = 1 print("Number of digits:", n1) findPerfectCubes(n1) n2 = 2 print("Number of digits:", n2) findPerfectCubes(n2) n3 = 3 print("Number of digits:", n3) findPerfectCubes(n3) n4 = 4 print("Number of digits:", n4) findPerfectCubes(n4)
เอาท์พุต :
Number of digits: 1 Smallest 1 -digit perfect cube: 1 Largest 1 -digit perfect cube: 8 Number of digits: 2 Smallest 2 -digit perfect cube: 27 Largest 2 -digit perfect cube: 64 Number of digits: 3 Smallest 3 -digit perfect cube: 125 Largest 3 -digit perfect cube: 729 Number of digits: 4 Smallest 4 -digit perfect cube: 1000 Largest 4 -digit perfect cube: 9261
โปรแกรม JavaScript เพื่อค้นหา N-Digit Perfect Cubes ที่เล็กที่สุดและใหญ่ที่สุด
ด้านล่างนี้คือโปรแกรมJavaScriptเพื่อค้นหาลูกบาศก์ที่สมบูรณ์แบบ n-digit ที่เล็กที่สุดและใหญ่ที่สุด:
// JavaScript program to find the smallest and largest // n-digit perfect cubes function findPerfectCubes(n) { document.write("Smallest " + n + "-digit perfect cube: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n - 1)))), 3) + " "); document.write("Largest " + n + "-digit perfect cube: " + Math.pow(Math.ceil(Math.cbrt(Math.pow(10, (n)))) - 1, 3) + " "); } var n1 = 1; document.write("Number of digits: " + n1 + " "); findPerfectCubes(n1); var n2 = 2; document.write("Number of digits: " + n2 + " "); findPerfectCubes(n2); var n3 = 3; document.write("Number of digits: " + n3 + " "); findPerfectCubes(n3); var n4 = 4; document.write("Number of digits: " + n4 + " "); findPerfectCubes(n4);
เอาท์พุต :
Number of digits: 1 Smallest 1-digit perfect cube: 1 Largest 1-digit perfect cube: 8 Number of digits: 2 Smallest 2-digit perfect cube: 27 Largest 2-digit perfect cube: 64 Number of digits: 3 Smallest 3-digit perfect cube: 125 Largest 3-digit perfect cube: 729 Number of digits: 4 Smallest 4-digit perfect cube: 1000 Largest 4-digit perfect cube: 9261
ลับสมองของคุณด้วยปริศนาคณิตศาสตร์กระตุ้น
หากคุณเป็นคนที่รักการไขปริศนาคณิตศาสตร์และปริศนา แสดงว่าคุณกำลังช่วยสมองของคุณ! การไขปริศนาคณิตศาสตร์และปริศนาช่วยเพิ่มความจำ เพิ่มทักษะการแก้ปัญหา และยังช่วยเพิ่มไอคิวได้อีกด้วย เว็บไซต์ ช่อง YouTube และแอปดีๆ บางแห่งมีเกมไขปริศนาคณิตศาสตร์ที่น่าทึ่งและเกมให้เล่นฟรี