给定一个系列7,15,32,…找出这个系列的第n项。
null
例如:
Input : 5Output : 138Input : 7Output : 568
方法: 通过观察序列的模式,我们可以很容易地确定它是一个混合序列。 S=7,15,32…。。 序列中的每个元素都乘以2,然后比前一个元素多加一。 S=7,15(2*7+1),32(2*15+2)……。 通过迭代,我们可以很容易地找到级数的第n项。
以下是上述方法的实施情况:
C++
// CPP program to find nth term #include <bits/stdc++.h> using namespace std; // utility function int findTerm( int n) { if (n == 1) return n; else { // since first element of the // series is 7, we initialise // a variable with 7 int term = 7; // Using iteration to find nth // term for ( int i = 2; i <= n; i++) term = term * 2 + (i - 1); return term; } } // driver function int main() { int n = 5; cout << findTerm(n); return 0; } |
JAVA
// Java program to find nth term import java.lang.*; class GFG { // utility function static int findTerm( int n) { if (n == 1 ) return n; else { // since first element of the // series is 7, we initialise // a variable with 7 int term = 7 ; // Using iteration to find nth // term for ( int i = 2 ; i <= n; i++) term = term * 2 + (i - 1 ); return term; } } // Driver code public static void main(String[] args) { int n = 5 ; System.out.print(findTerm(n)); } } // This code is contributed by Anant Agarwal. |
Python3
# Python3 program to find nth term # utility function def findTerm(n) : if n = = 1 : return n else : # since first element of the # series is 7, we initialise # a variable with 7 term = 7 # Using iteration to find nth # term for i in range ( 2 , n + 1 ) : term = term * 2 + (i - 1 ); return term; # driver function print (findTerm( 5 )) # This code is contributed by Saloni Gupta |
C#
// C# program to find nth term using System; class GFG { // utility function static int findTerm( int n) { if (n == 1) return n; else { // since first element of the // series is 7, we initialise // a variable with 7 int term = 7; // Using iteration to find nth // term for ( int i = 2; i <= n; i++) term = term * 2 + (i - 1); return term; } } // Driver code public static void Main() { int n = 5; Console.WriteLine(findTerm(n)); } } // This code is contributed by vt_m. |
PHP
<?php // PHP program to find nth term // utility function function findTerm( $n ) { if ( $n == 1) return $n ; else { // since first element of the // series is 7, we initialise // a variable with 7 $term = 7; // Using iteration to find nth // term for ( $i = 2; $i <= $n ; $i ++) $term = $term * 2 + ( $i - 1); return $term ; } } // Driver Code $n = 5; echo (findTerm( $n )); // This code is contributed by Ajit. ?> |
Javascript
<script> // Javascript program to find nth term // utility function function findTerm(n) { if (n == 1) return n; else { // since first element of the // series is 7, we initialise // a variable with 7 let term = 7; // Using iteration to find nth // term for (let i = 2; i <= n; i++) term = term * 2 + (i - 1); return term; } } // Driver Code let n = 5; document.write(findTerm(n)); // This code is contributed by gfgking. </script> |
输出:
138
本文由 萨洛尼·古普塔 .如果你喜欢GeekSforgek,并想贡献自己的力量,你也可以使用 写极客。组织 或者把你的文章寄去评论-team@geeksforgeeks.org.看到你的文章出现在Geeksforgeks主页上,并帮助其他极客。 如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END