📜  系列1、11、55、239、991等中的第N个术语。

📅  最后修改于: 2021-04-24 14:22:59             🧑  作者: Mango

给定数字N。任务是编写一个程序来查找系列中的第N个术语:

例子

Input: N = 3
Output: 55

Input: N = 4 
Output: 239 

方法1:在写下给定数字的二进制表示形式时,可以观察到一种模式。

因此,对于N = 1,答案将始终为1。对于第N个术语,二进制字符串为(n-1)* 1 +(0)+(n)* 1 ,该字符串将转换为十进制值以获得答案。
下面是上述方法的实现:

C++
// C++ program to find the N-th term
// in 1, 11, 55, 239, 991, ....
#include 
using namespace std;
 
// Function to return the decimal value
// of a binary number
int binaryToDecimal(string n)
{
    string num = n;
    int dec_value = 0;
 
    // Initializing base value to 1, i.e 2^0
    int base = 1;
 
    int len = num.length();
    for (int i = len - 1; i >= 0; i--) {
        if (num[i] == '1')
            dec_value += base;
        base = base * 2;
    }
 
    return dec_value;
}
 
// find the binary representation
// of the N-th number in sequence
int numberSequence(int n)
{
    // base case
    if (n == 1)
        return 1;
 
    // answer string
    string s = "";
 
    // add n-1 1's
    for (int i = 1; i < n; i++)
        s += '1';
 
    // add 0
    s += '0';
 
    // add n 1's at end
    for (int i = 1; i <= n; i++)
        s += '1';
 
    int num = binaryToDecimal(s);
 
    return num;
}
 
// Driver Code
int main()
{
    int n = 4;
 
    cout << numberSequence(n);
 
    return 0;
}


Java
// Java program to find the N-th
// term in 1, 11, 55, 239, 991, ....
import java.util.*;
 
class GFG
{
 
// Function to return the decimal
// value of a binary number
static int binaryToDecimal(String n)
{
    String num = n;
    int dec_value = 0;
 
    // Initializing base
    // value to 1, i.e 2^0
    int base = 1;
 
    int len = num.length();
    for (int i = len - 1; i >= 0; i--)
    {
        if (num.charAt(i) == '1')
            dec_value += base;
        base = base * 2;
    }
 
    return dec_value;
}
 
// find the binary representation
// of the N-th number in sequence
static int numberSequence(int n)
{
    // base case
    if (n == 1)
        return 1;
 
    // answer string
    String s = "";
 
    // add n-1 1's
    for (int i = 1; i < n; i++)
        s += '1';
 
    // add 0
    s += '0';
 
    // add n 1's at end
    for (int i = 1; i <= n; i++)
        s += '1';
 
    int num = binaryToDecimal(s);
 
    return num;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 4;
 
    System.out.println(numberSequence(n));
}
}
 
// This code is contributed
// by Arnab Kundu


Python 3
# Python 3 program to find the N-th term
# in 1, 11, 55, 239, 991, ....
  
# Function to return the decimal value
# of a binary number
def binaryToDecimal(n):
 
    num = n
    dec_value = 0
  
    # Initializing base value to 1, i.e 2^0
    base = 1
  
    l = len(num)
    for i in range(l - 1,-1, -1):
        if (num[i] == '1'):
            dec_value += base
        base = base * 2
  
    return dec_value
  
# find the binary representation
# of the N-th number in sequence
def numberSequence(n):
     
    # base case
    if (n == 1):
        return 1
  
    # answer string
    s = ""
  
    # add n-1 1's
    for i in range(1, n):
        s += '1'
  
    # add 0
    s += '0'
  
    # add n 1's at end
    for i in range(1,n+1):
        s += '1'
  
    num = binaryToDecimal(s)
  
    return num
  
# Driver Code
if __name__ == "__main__":
     
    n = 4
  
    print(numberSequence(n))
 
# this code is contributed by ChitraNayal


C#
// C# program to find the N-th
// term in 1, 11, 55, 239, 991, ....
using System;
 
class GFG
{
 
// Function to return the decimal
// value of a binary number
static int binaryToDecimal(String n)
{
    String num = n;
    int dec_value = 0;
 
    // Initializing base
    // value to 1, i.e 2^0
    int base_ = 1;
 
    int len = num.Length;
    for (int i = len - 1; i >= 0; i--)
    {
        if (num[i] == '1')
            dec_value += base_;
        base_ = base_ * 2;
    }
 
    return dec_value;
}
 
// find the binary representation
// of the N-th number in sequence
static int numberSequence(int n)
{
    // base case
    if (n == 1)
        return 1;
 
    // answer string
    String s = "";
 
    // add n-1 1's
    for (int i = 1; i < n; i++)
        s += '1';
 
    // add 0
    s += '0';
 
    // add n 1's at end
    for (int i = 1; i <= n; i++)
        s += '1';
 
    int num = binaryToDecimal(s);
 
    return num;
}
 
// Driver Code
public static void Main()
{
    int n = 4;
 
    Console.WriteLine(numberSequence(n));
}
}
 
// This code is contributed
// by Subhadeep


PHP
= 0; $i--)
    {
        if ($num[$i] == '1')
            $dec_value += $base;
        $base = $base * 2;
    }
 
    return $dec_value;
}
 
// find the binary representation
// of the N-th number in sequence
function numberSequence($n)
{
    // base case
    if ($n == 1)
        return 1;
 
    // answer string
    $s = "";
 
    // add n-1 1's
    for ($i = 1; $i < $n; $i++)
        $s .= '1';
 
    // add 0
    $s .= '0';
 
    // add n 1's at end
    for ($i = 1; $i <= $n; $i++)
        $s .= '1';
 
    $num = binaryToDecimal($s);
 
    return $num;
}
 
// Driver Code
$n = 4;
 
echo numberSequence($n);
 
// This code is contributed by mits
?>


Javascript


C++
// C++ program to find the N-th term
// in 1, 11, 55, 239, 991, ....
#include 
using namespace std;
 
// Function to find the N-th term
int numberSequence(int n)
{
    // calculates the N-th term
    int num = pow(4, n) - pow(2, n) - 1;
 
    return num;
}
 
// Driver Code
int main()
{
    int n = 4;
 
    cout << numberSequence(n);
 
    return 0;
}


Java
// Java program to find the N-th
// term in 1, 11, 55, 239, 991, ....
class GFG
{
// Function to find the N-th term
static int numberSequence(int n)
{
    // calculates the N-th term
    int num = (int)(Math.pow(4, n) -
                    Math.pow(2, n)) - 1;
 
    return num;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 4;
 
    System.out.println(numberSequence(n));
}
}
 
// This code is contributed
// by Arnab Kundu


Python 3
# Python 3 program to find N-th term
# in 1, 11, 55, 239, 991, ....
 
# calculate Nth term of series
def numberSequence(n) :
 
    # calculates the N-th term
    num = pow(4, n) - pow(2, n) - 1
 
    return num
 
# Driver Code
if __name__ == "__main__" :
 
    n = 4
     
    print(numberSequence(n))
 
# This code is contributed by ANKITRAI1


C#
// C# program to find the N-th
// term in 1, 11, 55, 239, 991, ....
using System;
 
class GFG
{
// Function to find the N-th term
static int numberSequence(int n)
{
    // calculates the N-th term
    int num = (int)(Math.Pow(4, n) -
                    Math.Pow(2, n)) - 1;
 
    return num;
}
 
// Driver Code
public static void Main()
{
    int n = 4;
 
    Console.WriteLine(numberSequence(n));
}
}
 
// This code is contributed
// by chandan_jnu.


PHP


Javascript


输出:
239

方法2:该级数的一般公式为4 N -2 N -1 ,用于获得级数第N个项。
下面是上述方法的实现:

C++

// C++ program to find the N-th term
// in 1, 11, 55, 239, 991, ....
#include 
using namespace std;
 
// Function to find the N-th term
int numberSequence(int n)
{
    // calculates the N-th term
    int num = pow(4, n) - pow(2, n) - 1;
 
    return num;
}
 
// Driver Code
int main()
{
    int n = 4;
 
    cout << numberSequence(n);
 
    return 0;
}

Java

// Java program to find the N-th
// term in 1, 11, 55, 239, 991, ....
class GFG
{
// Function to find the N-th term
static int numberSequence(int n)
{
    // calculates the N-th term
    int num = (int)(Math.pow(4, n) -
                    Math.pow(2, n)) - 1;
 
    return num;
}
 
// Driver Code
public static void main(String args[])
{
    int n = 4;
 
    System.out.println(numberSequence(n));
}
}
 
// This code is contributed
// by Arnab Kundu

的Python 3

# Python 3 program to find N-th term
# in 1, 11, 55, 239, 991, ....
 
# calculate Nth term of series
def numberSequence(n) :
 
    # calculates the N-th term
    num = pow(4, n) - pow(2, n) - 1
 
    return num
 
# Driver Code
if __name__ == "__main__" :
 
    n = 4
     
    print(numberSequence(n))
 
# This code is contributed by ANKITRAI1

C#

// C# program to find the N-th
// term in 1, 11, 55, 239, 991, ....
using System;
 
class GFG
{
// Function to find the N-th term
static int numberSequence(int n)
{
    // calculates the N-th term
    int num = (int)(Math.Pow(4, n) -
                    Math.Pow(2, n)) - 1;
 
    return num;
}
 
// Driver Code
public static void Main()
{
    int n = 4;
 
    Console.WriteLine(numberSequence(n));
}
}
 
// This code is contributed
// by chandan_jnu.

的PHP


Java脚本


输出:
239