Find n-th element in a series with only 2 digits (4 and 7) allowed - GeeksforGeeks

25 Mar, 2021
Consider a series of numbers composed of only digits 4 and 7. First few numbers in the series are 4, 7, 44, 47, 74, 44744,.. etc. Given a number n, we need to find n-th number in the series.Examples:  
Input : n = 2
Output : 7

Input : n = 3
Output : 44

Input  : n = 5
Output : 74

Input  : n = 6
Output : 77
 
The idea is based on the fact that the value of last digit alternates in series. For example, if last digit of i-th number is 4, then last digit of (i-1)-th and (i+1)-th numbers must be 7.We create an array of size (n+1) and push 4 and 7 (These two are always first two elements of series) to it. For more elements we check 1) If i is odd,       arr[i] = arr[i/2]*10 + 4; 2) If it is even,       arr[i] = arr[(i/2)-1]*10 + 7; At last return arr[n]. 
C++
Java
Python3
C#
PHP
Javascript
// C++ program to find n-th number in a series// made of digits 4 and 7#include <bits/stdc++.h>using namespace std; // Return n-th number in series made of 4 and 7int printNthElement(int n){    // create an array of size (n+1)    int arr[n+1];    arr[1] = 4;    arr[2] = 7;     for (int i=3; i<=n; i++)    {        // If i is odd        if (i%2 != 0)            arr[i] = arr[i/2]*10 + 4;        else            arr[i] = arr[(i/2)-1]*10 + 7;    }    return arr[n];} // Driver codeint main(){    int n = 6;    cout << printNthElement(n);    return 0;}
// Java program to find n-th number in a series// made of digits 4 and 7 class FindNth{    // Return n-th number in series made of 4 and 7    static int printNthElement(int n)    {        // create an array of size (n+1)        int arr[] = new int[n+1];        arr[1] = 4;        arr[2] = 7;              for (int i=3; i<=n; i++)        {            // If i is odd            if (i%2 != 0)                arr[i] = arr[i/2]*10 + 4;            else                arr[i] = arr[(i/2)-1]*10 + 7;        }        return arr[n];    }            // main function    public static void main (String[] args)    {        int n = 6;        System.out.println(printNthElement(n));    }}
# Python3 program to find n-th number# in a series made of digits 4 and 7 # Return n-th number in series made# of 4 and 7def printNthElement(n) :         # create an array of size (n + 1)    arr =[0] * (n + 1);    arr[1] = 4    arr[2] = 7     for i in range(3, n + 1) :        # If i is odd        if (i % 2 != 0) :            arr[i] = arr[i // 2] * 10 + 4        else :            arr[i] = arr[(i // 2) - 1] * 10 + 7         return arr[n]     # Driver coden = 6print(printNthElement(n)) # This code is contributed by Nikita Tiwari.
// C# program to find n-th number in a series// made of digits 4 and 7using System; class GFG{    // Return n-th number in series made of 4 and 7    static int printNthElement(int n)    {        // create an array of size (n+1)        int []arr = new int[n+1];        arr[1] = 4;        arr[2] = 7;             for (int i = 3; i <= n; i++)        {            // If i is odd            if (i % 2 != 0)                arr[i] = arr[i / 2] * 10 + 4;            else                arr[i] = arr[(i / 2) - 1] * 10 + 7;        }        return arr[n];    }         // Driver code    public static void Main ()    {        int n = 6;        Console.Write(printNthElement(n));    }} // This code is contributed by vt_m.
<?php// PHP program to find n-th// number in a series// made of digits 4 and 7 // Return n-th number in// series made of 4 and 7function printNthElement($n){         // create an array    // of size (n+1)    $arr[1] = 4;    $arr[2] = 7;     for ($i = 3; $i <= $n; $i++)    {                 // If i is odd        if ($i % 2 != 0)            $arr[$i] = $arr[$i / 2] *                               10 + 4;        else            $arr[$i] = $arr[($i / 2) - 1] *                                    10 + 7;    }    return $arr[$n];} // Driver code$n = 6;echo(printNthElement($n)); // This code is contributed by Ajit.?>
<script>// javascript program to find n-th number in a series// made of digits 4 and 7      // Return n-th number in series made of 4 and 7    function printNthElement(n) {        // create an array of size (n+1)        var arr = Array(n + 1).fill(0);        arr[1] = 4;        arr[2] = 7;         for (var i = 3; i <= n; i++) {            // If i is odd            if (i % 2 != 0)                arr[i] = arr[i / 2] * 10 + 4;            else                arr[i] = arr[(i / 2) - 1] * 10 + 7;        }        return arr[n];    }     // main function             var n = 6;        document.write(printNthElement(n)); // This code is contributed by Princi Singh</script>
Output:  
77
Find n-th element in a series with only 2 digits (4 and 7) allowed | Set 2 (log(n) method)This article is contributed by Roshni Agarwal. If you like GeeksforGeeks and would like to contribute, you can also write an article using write.geeksforgeeks.org or mail your article to review-team@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above. 
jit_t
princi singh
number-digits
series
Mathematical
Mathematical
series
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
Program to find GCD or HCF of two numbers
Prime Numbers
Merge two sorted arrays
Modulo Operator (%) in C/C++ with Examples
Operators in C / C++
Program to find sum of elements in a given array
Program for factorial of a number
Sieve of Eratosthenes
Print all possible combinations of r elements in a given array of size n
Efficient program to print all prime factors of a given number