Digit – Product – Sequence
Given a number N, the task is to print the sequence up to N. The sequence is 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, 108, 116, 122, 126, 138, 162, and so on… This sequence is known as Digit – Product – Sequence. In this series, we take every non-zero digit of the number, multiply them and add the product to the number itself.
Examples:
Input : N = 10
Output :1 2 4 8 16 22 26 38 62 74
Input : N = 7
Output :1 2 4 8 16 22 26
Explanation:
1 + (1 * 1) = 1 + 1 = 2
2 + (2 * 1) = 2 + 2 = 4
4 + (4 * 1) = 4 + 4 = 8
8 + (8 * 1) = 8 + 8 = 16
16 + (1 * 6) = 16 + 6 = 22
22 + (2 * 2) = 22 + 4 = 26
26 + (2 * 6) = 26 + 12 = 38
38 + (3 * 8) = 38 + 24 = 62
62 + (6 * 2) = 62 + 12 = 74
and so on...
Implementation:
C++
#include <bits/stdc++.h>
using namespace std;
void digit_product_Sum( int N)
{
int a[N];
int product = 1;
a[0] = 1;
for ( int i = 1; i <= N; i++) {
product = a[i - 1] / 10;
if (product == 0)
product = 1;
else
product = a[i - 1] % 10;
int val = a[i - 1] / 10;
if (val == 0)
val = a[i - 1];
a[i] = a[i - 1] + (val * product);
}
for ( int i = 0; i < N; i++)
cout << a[i] << " " ;
}
int main()
{
int N = 10;
digit_product_Sum(N);
return 0;
}
|
Java
import java.lang.*;
import java.io.*;
class GFG
{
public static void digit_product_Sum( int N)
{
int a[] = new int [N+ 1 ] ;
int product = 1 ;
a[ 0 ] = 1 ;
for ( int i = 1 ; i <= N; i++)
{
product = a[i - 1 ] / 10 ;
if (product == 0 )
product = 1 ;
else
product = a[i - 1 ] % 10 ;
int val = a[i - 1 ] / 10 ;
if (val == 0 )
val = a[i - 1 ];
a[i] = a[i - 1 ] + (val * product);
}
for ( int i = 0 ; i < N; i++)
System.out.print(a[i] + " " );
}
public static void main(String[] args)
{
int N = 10 ;
digit_product_Sum(N);
}
}
|
Python3
def digit_product_Sum(N):
a = [ 0 ] * (N + 1 );
product = 1 ;
a[ 0 ] = 1 ;
for i in range ( 1 , N + 1 ):
product = int (a[i - 1 ] / 10 );
if (product = = 0 ):
product = 1 ;
else :
product = a[i - 1 ] % 10 ;
val = int (a[i - 1 ] / 10 );
if (val = = 0 ):
val = a[i - 1 ];
a[i] = a[i - 1 ] + (val * product);
for i in range (N):
print (a[i], end = " " );
N = 10 ;
digit_product_Sum(N);
|
C#
using System;
class GFG
{
public static void digit_product_Sum( int N)
{
int []a = new int [N + 1] ;
int product = 1;
a[0] = 1;
for ( int i = 1; i <= N; i++)
{
product = a[i - 1] / 10;
if (product == 0)
product = 1;
else
product = a[i - 1] % 10;
int val = a[i - 1] / 10;
if (val == 0)
val = a[i - 1];
a[i] = a[i - 1] + (val * product);
}
for ( int i = 0; i < N; i++)
Console.Write(a[i] + " " );
}
public static void Main()
{
int N = 10;
digit_product_Sum(N);
}
}
|
PHP
<?php
function digit_product_Sum( $N )
{
$a = array_fill (0, $N , 0);
$product = 1;
$a [0] = 1;
for ( $i = 1; $i <= $N ; $i ++)
{
$product = (int)( $a [ $i - 1] / 10);
if ( $product == 0)
$product = 1;
else
$product = $a [ $i - 1] % 10;
$val = (int)( $a [ $i - 1] / 10);
if ( $val == 0)
$val = $a [ $i - 1];
$a [ $i ] = $a [ $i - 1] +
( $val * $product );
}
for ( $i = 0; $i < $N ; $i ++)
echo $a [ $i ]. " " ;
}
$N = 10;
digit_product_Sum( $N );
?>
|
Javascript
<script>
function digit_product_Sum(N) {
var a = [...Array(N)];
var product = 1;
a[0] = 1;
for ( var i = 1; i <= N; i++)
{
product = parseInt(a[i - 1] / 10);
if (product == 0) product = 1;
else product = a[i - 1] % 10;
var val = parseInt(a[i - 1] / 10);
if (val == 0) val = a[i - 1];
a[i] = a[i - 1] + val * product;
}
for ( var i = 0; i < N; i++)
document.write(a[i] + " " );
}
var N = 10;
digit_product_Sum(N);
</script>
|
Output
1 2 4 8 16 22 26 38 62 74
Time Complexity: O(N)
Auxiliary Space: O(N)
Last Updated :
16 Feb, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...