Dígito – Producto – Secuencia

Dado un número N, la tarea es imprimir la secuencia hasta N. La secuencia es: 1, 2, 4, 8, 16, 22, 26, 38, 62, 74, 102, 104, 108, 116, 122, 126, 138, 162 y así sucesivamente… Esta secuencia se conoce como Dígito – Producto – Secuencia. En esta serie, tomamos todos los dígitos distintos de cero del número, los multiplicamos y sumamos el producto al número mismo.

Ejemplos: 

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

Explicación: 

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...

C++

// CPP program for Digit Product Sequence
#include <bits/stdc++.h>
using namespace std;
 
// function to produce and print Digit
// Product Sequence
void digit_product_Sum(int N)
{
    // Array which store sequence
    int a[N];
 
    // Temporary variable to store product
    int product = 1;
 
    // Initialize first element of the
    // array with 1
    a[0] = 1;
 
    // Run a loop from 1 to N. Check if
    // previous number is single digit or
    // not. If yes then product = 1 else
    // take modulus. Then again check if
    // previous number is single digit or
    // not if yes then store previous number,
    // else store its first value Then for
    // every i store value in the array.
    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);
    }
 
    // Print sequence
    for (int i = 0; i < N; i++)
        cout << a[i] << " ";
}
 
// Driver Code
int main()
{
    // Value of N
    int N = 10;
 
    // Calling function
    digit_product_Sum(N);
    return 0;
}

Java

// Java program for Digit Product Sequence
 
// function to produce and print Digit
// Product Sequence
import java.lang.*;
import java.io.*;
 
class GFG
{
    public static void digit_product_Sum(int N)
    {
        // Array which store sequence
        int a[] = new int[N+1] ;
 
        // Temporary variable to store product
        int product = 1;
 
        // Initialize first element of the
        // array with 1
        a[0] = 1;
 
        // Run a loop from 1 to N. Check if
        // previous number is single digit or
        // not. If yes then product = 1 else
        // take modulus. Then again check if
        // previous number is single digit or
        // not if yes then store previous number,
        // else store its first value Then for
        // every i store value in the array.
        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);
        }
 
        // Print sequence
        for (int i = 0; i < N; i++)
            System.out.print(a[i] + " ");
    }
 
// Driver Code
    public static void main(String[] args)
    {
        // Value of N
        int N = 10;
 
        // Calling function
        digit_product_Sum(N);
 
    }
}
// Code contributed by Mohit Gupta_OMG <(0_o)>

Python3

# Python3 program for
# Digit Product Sequence
 
# function to produce and
# print Digit Product Sequence
 
def digit_product_Sum(N):
     
    # Array which store sequence
    a = [0] * (N + 1);
     
    # Temporary variable
    # to store product
    product = 1;
     
    # Initialize first element
    # of the array with 1
    a[0] = 1;
     
    # Run a loop from 1 to N.
    # Check if previous number
    # is single digit or not.
    # If yes then product = 1
    # else take modulus. Then
    # again check if previous
    # number is single digit or
    # not if yes then store
    # previous number, else store
    # its first value Then for
    # every i store value in
    # the array.
    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);
     
    # Print sequence
    for i in range(N):
        print(a[i], end = " ");
         
# Driver Code
 
# Value of N
N = 10;
 
# Calling function
digit_product_Sum(N);
 
# This Code is contributed
# by mits.

C#

// C# program for Digit Product Sequence
// function to produce and print Digit
// Product Sequence
using System;
 
class GFG
{
    public static void digit_product_Sum(int N)
    {
        // Array which store sequence
        int []a = new int[N + 1] ;
 
        // Temporary variable to store product
        int product = 1;
 
        // Initialize first element of the
        // array with 1
        a[0] = 1;
 
        // Run a loop from 1 to N. Check if
        // previous number is single digit or
        // not. If yes then product = 1 else
        // take modulus. Then again check if
        // previous number is single digit or
        // not if yes then store previous number,
        // else store its first value Then for
        // every i store value in the array.
        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);
        }
 
        // Print sequence
        for (int i = 0; i < N; i++)
        Console.Write(a[i] + " ");
    }
 
    // Driver Code
    public static void Main()
    {
        // Value of N
        int N = 10;
 
        // Calling function
        digit_product_Sum(N);
 
    }
}
// This Code is contributed by vt_m.

PHP

<?php
// PHP program for Digit
// Product Sequence
 
// function to produce
// and print Digit
// Product Sequence
function digit_product_Sum($N)
{
    // Array which
    // store sequence
    $a = array_fill(0, $N, 0);
 
    // Temporary variable
    // to store product
    $product = 1;
 
    // Initialize first
    // element of the
    // array with 1
    $a[0] = 1;
 
    // Run a loop from 1 to
    // N. Check if previous
    // number is single digit
    // or not. If yes then
    // product = 1 else take
    // modulus. Then again check
    // if previous number is single
    // digit or not if yes then
    // store previous number,
    // else store its first value
    // Then for every i store value
    // in the array.
    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);
    }
 
    // Print sequence
    for ($i = 0; $i < $N; $i++)
        echo $a[$i]." ";
}
 
// Driver Code
 
// Value of N
$N = 10;
 
// Calling function
digit_product_Sum($N);
 
// This Code is contributed
// by mits.
 
?>

Javascript

<script>
      // JavaScript program for Digit
      // Product Sequence
 
      // function to produce and print Digit
      // Product Sequence
      function digit_product_Sum(N) {
        // Array which store sequence
        var a = [...Array(N)];
 
        // Temporary variable to store product
        var product = 1;
 
        // Initialize first element of the
        // array with 1
        a[0] = 1;
 
        // Run a loop from 1 to N. Check if
        // previous number is single digit or
        // not. If yes then product = 1 else
        // take modulus. Then again check if
        // previous number is single digit or
        // not if yes then store previous number,
        // else store its first value Then for
        // every i store value in the array.
        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;
        }
 
        // Print sequence
        for (var i = 0; i < N; i++)
        document.write(a[i] + " ");
      }
 
      // Driver Code
      // Value of N
      var N = 10;
       
      // Calling function
      digit_product_Sum(N);
       
</script>

Producción:  

1 2 4 8 16 22 26 38 62 74

Este artículo es una contribución de Ayush Saxena . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *