כאשר אתה מחפש לשפר את כישורי התכנות שלך, סביר להניח שתרצה ללמוד על רצפים גיאומטריים בשלב כלשהו. ברצף גיאומטרי, כל מונח נמצא על ידי הכפלת המונח הקודם בקבוע.
במאמר זה תלמד כיצד למצוא את סכום הסדרות הגיאומטריות באמצעות Python, C ++, JavaScript ו- C.
מהי סדרה גיאומטרית?
סכום המונחים של רצף גיאומטרי אינסופי נקרא סדרה גיאומטרית. הרצף הגיאומטרי או ההתקדמות הגיאומטרית מסומנים כדלקמן:
מלכודת מצב ליבה בלתי צפויה חלונות 10
a, ar, ar², ar³, ...
איפה,
a = First term
r = Common ratio
הצהרת בעיה
ניתן לך את המונח הראשון, יחס משותף, ולא. במונחים של הסדרה הגיאומטרית. עליך למצוא את סכום הסדרה הגיאומטרית. דוגמא : תן firstTerm = 1, commonRatio = 2, noOfTerms = 8. סדרות גיאומטריות: 1 + 2 + 4 + 8 + 16 + 32 + 64 + 128 סכום הסדרה הגיאומטרית: 255 לפיכך, הפלט הוא 255.
גישה איטרטיבית למציאת סכום של סדרה גיאומטרית
ראשית, בואו נסתכל על הדרך האיטרטיבית למצוא סכום של סדרה גיאומטרית. למד כיצד תוכל לעשות זאת עם כל שפת תכנות ראשית.
תוכנית C ++ למציאת סכום של סדרה גיאומטרית באמצעות Iteration
להלן תוכנית C ++ למציאת סכום של סדרה גיאומטרית באמצעות איטרציה:
// C++ program to find the sum of geometric series
#include
using namespace std;
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
float result = 0;
for (int i=0; i {
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
cout << 'First Term: ' << firstTerm << endl;
cout << 'Common Ratio: ' << commonRatio << endl;
cout << 'Number of Terms: ' << noOfTerms << endl;
cout << 'Sum of the geometric series: ' << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;
return 0;
}
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
תוכנית פייתון למציאת סכום של סדרה גיאומטרית באמצעות Iteration
להלן תוכנית Python למציאת סכום של סדרה גיאומטרית באמצעות איטרציה:
# Python program to find the sum of geometric series
# Function to find the sum of geometric series
def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):
result = 0
for i in range(noOfTerms):
result = result + firstTerm
firstTerm = firstTerm * commonRatio
return result
firstTerm = 1
commonRatio = 2
noOfTerms = 8
print('First Term:', firstTerm)
print('Common Ratio:', commonRatio)
print('Number of Terms:', noOfTerms)
print('Sum of the geometric series:', sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
קָשׁוּר: כיצד להדפיס 'שלום, עולם!' בשפות התכנות הפופולריות ביותר
תוכנית JavaScript למציאת סכום של סדרה גיאומטרית באמצעות Iteration
להלן תוכנית JavaScript לאיתור סכום של סדרה גיאומטרית באמצעות איטרציה:
// JavaScript program to find the sum of geometric series
// Function to find the sum of geometric series
function sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) {
var result = 0;
for (let i=0; i {
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}
var firstTerm = 1;
var commonRatio = 2;
var noOfTerms = 8;
document.write('First Term: ' + firstTerm + '
');
document.write('Common Ratio: ' + commonRatio + '
');
document.write('Number of Terms: ' + noOfTerms + '
');
document.write('Sum of the geometric series: ' + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
תוכנית C למצוא את סכום הסדרה הגיאומטרית באמצעות Iteration
להלן תוכנית C למציאת סכום של סדרה גיאומטרית באמצעות איטרציה:
// C program to find the sum of geometric series
#include
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
float result = 0;
for (int i=0; i {
result = result + firstTerm;
firstTerm = firstTerm * commonRatio;
}
return result;
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
printf('First Term: %f n', firstTerm);
printf('Common Ratio: %f n', commonRatio);
printf('Number of Terms: %d n', noOfTerms);
printf('Sum of the geometric series: %f n', sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
return 0;
}
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
גישה יעילה למציאת סכום סדרה גיאומטרית באמצעות נוסחה
אתה יכול להשתמש בנוסחה הבאה כדי למצוא את סכום הסדרה הגיאומטרית:
Sum of geometric series = a(1 – rn)/(1 – r)
איפה,
a = First term
d = Common ratio
n = No. of terms
תוכנית C ++ למציאת סכום סדרה גיאומטרית באמצעות נוסחה
להלן תוכנית C ++ למציאת סכום של סדרה גיאומטרית באמצעות הנוסחה:
// C++ program to find the sum of geometric series
#include
using namespace std;
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
cout << 'First Term: ' << firstTerm << endl;
cout << 'Common Ratio: ' << commonRatio << endl;
cout << 'Number of Terms: ' << noOfTerms << endl;
cout << 'Sum of the geometric series: ' << sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) << endl;
return 0;
}
תְפוּקָה:
חנות מיקרוסופט לא פותחת את חלונות 10
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
תוכנית פייתון למצוא את סכום הסדרה הגיאומטרית באמצעות נוסחה
להלן תוכנית Python למציאת סכום של סדרה גיאומטרית באמצעות הנוסחה:
# Python program to find the sum of geometric series
# Function to find the sum of geometric series
def sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms):
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio)
firstTerm = 1
commonRatio = 2
noOfTerms = 8
print('First Term:', firstTerm)
print('Common Ratio:', commonRatio)
print('Number of Terms:', noOfTerms)
print('Sum of the geometric series:', sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms))
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
קשור: כיצד למצוא את LCM ו- GCD של שני מספרים בשפות מרובות
הדברים הראשונים לעשות עם חלונות 10
תוכנית JavaScript לאיתור סכום סדרה גיאומטרית באמצעות נוסחה
להלן תוכנית JavaScript לאיתור סכום של סדרה גיאומטרית באמצעות הנוסחה:
// JavaScript program to find the sum of geometric series
// Function to find the sum of geometric series
function sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms) {
return (firstTerm * (1 - Math.pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}
var firstTerm = 1;
var commonRatio = 2;
var noOfTerms = 8;
document.write('First Term: ' + firstTerm + '
');
document.write('Common Ratio: ' + commonRatio + '
');
document.write('Number of Terms: ' + noOfTerms + '
');
document.write('Sum of the geometric series: ' + sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
קשור: כיצד לספור את המופעים של דמות נתונה במחרוזת
תוכנית C למצוא את סכום הסדרה הגיאומטרית באמצעות נוסחה
להלן תוכנית C לאיתור סכום סדרה גיאומטרית באמצעות הנוסחה:
// C program to find the sum of geometric series
#include
#include
// Function to find the sum of geometric series
float sumOfGeometricSeries(float firstTerm, float commonRatio, int noOfTerms)
{
return (firstTerm * (1 - pow(commonRatio, noOfTerms))) / (1 - commonRatio);
}
int main()
{
float firstTerm = 1;
float commonRatio = 2;
int noOfTerms = 8;
printf('First Term: %f n', firstTerm);
printf('Common Ratio: %f n', commonRatio);
printf('Number of Terms: %d n', noOfTerms);
printf('Sum of the geometric series: %f n', sumOfGeometricSeries(firstTerm, commonRatio, noOfTerms));
return 0;
}
תְפוּקָה:
First Term: 1
Common Ratio: 2
Number of Terms: 8
Sum of the geometric series: 255
כעת אתה יודע כיצד למצוא סכומי סדרה גיאומטרית באמצעות שפות תכנות שונות
במאמר זה למדת כיצד למצוא את סכום הסדרות הגיאומטריות באמצעות שתי גישות: איטרציה ונוסחה. למדת גם כיצד לפתור בעיה זו באמצעות שפות תכנות שונות כגון Python, C ++, JavaScript ו- C.
פייתון היא שפת תכנות לכל דבר עם דגש על קריאת קוד. אתה יכול להשתמש בפייתון למדעי נתונים, למידת מכונות, פיתוח אתרים, עיבוד תמונות, ראיית מחשב וכו '. זוהי אחת משפות התכנות המגוונות ביותר. כדאי מאוד לחקור את שפת התכנות העוצמתית הזו.
לַחֲלוֹק לַחֲלוֹק צִיוּץ אימייל 3 דרכים לבדוק אם דוא'ל אמיתי או מזויףאם קיבלת מייל שנראה קצת מפוקפק, תמיד עדיף לבדוק את האותנטיות שלו. להלן שלוש דרכים לדעת אם הודעת דוא'ל אמיתית.
קרא הבא נושאים קשורים- תִכנוּת
- פִּיתוֹן
- JavaScript
- C תכנות
- תִכנוּת
יובראג 'הוא סטודנט לתואר ראשון במדעי המחשב באוניברסיטת דלהי, הודו. הוא נלהב מ- Full Stack Web Development. כשהוא לא כותב, הוא בוחן את עומק הטכנולוגיות השונות.
עוד מאת Yuvraj Chandraהירשם לניוזלטר שלנו
הצטרף לניוזלטר שלנו לקבלת טיפים, סקירות, ספרים אלקטרוניים בחינם ומבצעים בלעדיים!
לחצו כאן להרשמה