Computer >> คอมพิวเตอร์ >  >> การเขียนโปรแกรม >> C++

ค้นหา Nth_Non_Square_Number โดยใช้ C++


เราทุกคนรู้เกี่ยวกับตัวเลขที่ไม่ใช่กำลังสองของตัวเลขใดๆ เช่น 2, 3, 5, 7, 8 เป็นต้น มีตัวเลขที่ N ของตัวเลขที่ไม่ใช่กำลังสอง และเป็นไปไม่ได้ที่จะรู้ทุกตัวเลข ดังนั้น ในบทความนี้ เราจะอธิบายทุกอย่างเกี่ยวกับจำนวนที่ไม่ใช่กำลังสองหรือจำนวนที่ไม่ใช่กำลังสอง และวิธีค้นหาจำนวนที่ไม่ใช่กำลังสองที่ N ใน C++

Nth Non-Square Number

ตัวเลขจะเป็นกำลังสองสมบูรณ์ถ้าเป็นกำลังสองของจำนวนเต็ม ตัวอย่างของเลขกำลังสองสมบูรณ์ ได้แก่ −

1 is square of 1
4 is square of 2
9 is square of 3
16 is square of 4
25 is square of 5

ตัวเลขจะเป็นจำนวนที่ไม่ใช่กำลังสอง ถ้ามันไม่ใช่กำลังสองของจำนวนเต็มใดๆ ตัวอย่างเช่น ตัวเลขที่ไม่ใช่สี่เหลี่ยมจัตุรัส 15 ตัวแรกคือ −

2, 3, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 17, 18, 19

จะหาจำนวนที่ไม่ใช่กำลังสอง Nth ได้อย่างไร

ต่อไปนี้คือตัวอย่างการหาจำนวนที่ไม่ใช่กำลังสองที่ N -

Input : 2
Output : 3
Explanation : 2nd Non square number is 3 (after 2 which is first non square number)

Input : 5
Output : 7
Explanation : 7th Non square number is 7 ( after 2,3,5,6 which are first four non square

หลังจากดูตัวอย่างข้างต้นแล้ว เราก็สามารถคิดวิธีแก้ปัญหาได้ว่า สำหรับการหาจำนวนที่ไม่ใช่กำลังสอง เราต้องเริ่มนับจำนวนที่ n และตรวจสอบจำนวนเต็มแต่ละจำนวนว่าเป็นกำลังสองสมบูรณ์หรือไม่และไม่นับจำนวนที่เป็น กำลังสองสมบูรณ์ คือ ให้นับว่าตัวเลขนั้นเป็นกำลังสองสมบูรณ์หรือไม่

การสร้างโปรแกรม C + + เพื่อค้นหา Nth Non-Square Number

เราได้สร้างรูปแบบที่สมบูรณ์สำหรับการค้นหาจำนวนที่ไม่ใช่กำลังสองที่ N ใน C++

ตัวอย่าง

#include <bits/stdc++.h>
using namespace std;
int main(){
    int n;
    cin >> n; // Taking input from the user.
    int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2.
    int cnt = 0; // declaring counter variable;
    while(cnt != n){// the loop will terminate when out counter will have the same value as n.
        int a = sqrt(i);
        if(i != a*a)
            cnt++;
        if(cnt != n)
            i++;
    }
    cout << i << "\n"; // printing the nth non square number.
}

ผลลัพธ์

5

(เมื่อเราระบุ 3 เป็นอินพุต เราก็ได้ 5 เป็นเอาต์พุต)

ให้เราอธิบายสั้นๆ เกี่ยวกับโค้ดด้านบนนี้

ขั้นตอนที่ 1 − รับข้อมูลจากผู้ใช้และตั้งค่าเป็น 0

cin >> n; // Taking input from the user.
int i = 2; // as 0 and 1 are squares of themselves so we start calculating from 2.
int cnt = 0; // declaring counter variable;

ขั้นตอนที่ 2 − การนับจำนวนที่ไม่ใช่กำลังสองและการข้ามเลขกำลังสอง

while(cnt != n) // the loop will terminate when out counter will have the same value as n.{
   int a = sqrt(i); // finding square root using sqrt() function.
   if(i != a*a) // check whether the number is a perfect square or not.
      cnt++; // incrementing counter if found non perfect number.
      if(cnt != n)
   i++;
}

ขั้นตอนที่ 3 − พิมพ์เลขสี่เหลี่ยมที่ N

cout << i << "\n"; // printing the nth non square number.

บทสรุป

ในบทความนี้ เราได้อธิบายเกี่ยวกับจำนวนที่ไม่ใช่กำลังสองและวิธีค้นหาจำนวนที่ไม่ใช่กำลังสองที่ N ใน C++ นอกเหนือจาก C++ แล้ว เราสามารถใช้โปรแกรมนี้ในภาษาการเขียนโปรแกรมต่างๆ เช่น Java, Python, C หรืออื่นๆ เราหวังว่าคุณจะพบว่าบทความนี้มีประโยชน์และให้ข้อมูลในขณะที่เราได้อธิบายทุกอย่างด้วยวิธีที่ง่ายที่สุด