Suma de enteros hasta N con dígito unitario dado

Dados dos enteros N y D , la tarea es encontrar la suma de todos los enteros del 1 al N cuya unidad es D .
Ejemplos: 
 

Entrada: N = 30, D = 3 
Salida: 39 
3 + 13 + 23 = 39
Entrada: N = 5, D = 7 
Salida:
 

Enfoque ingenuo: 
 

  • Travesía de 1 a N .
  • Si el dígito de la unidad del número es D , agregue el número a la suma .
  • Finalmente imprima el valor de sum .

A continuación se muestra la implementación del enfoque anterior:
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    for (int i = 1; i <= n; i++) {
 
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}

Java

// Java implementation of the approach
import java.util.*;
 
class solution
{
 
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    for (int i = 1; i <= n; i++) {
 
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
 
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.println(getSum(n, d));
    
}
}

Python3

# Python3 implementation of the approach
 
# Function to return the required sum
def getSum(n, d) :
    sum = 0;
    for i in range(n + 1) :
 
        # If the unit digit is d
        if (i % 10 == d) :
            sum += i
    return sum
 
# Driver code
if __name__ == "__main__" :
 
    n , d = 30, 3
    print(getSum(n, d))
 
# This code is contributed by Ryuga

PHP

<?php
// PHP implementation of the approach
 
// Function to return the required sum
function getSum($n, $d)
{
    $sum = 0;
    for ($i = 1; $i <= $n; $i++)
    {
 
        // If the unit digit is d
        if ($i % 10 == $d)
            $sum += $i;
    }
    return $sum;
}
 
// Driver code
$n = 30;
$d = 3;
echo getSum($n, $d);
     
// This code is contributed by Sachin
?>

Javascript

<script>
 
// java script implementation of the approach
 
// Function to return the required sum
function getSum(n, d)
{
    let sum = 0;
    for (let i = 1; i <= n; i++)
    {
 
        // If the unit digit is d
        if (i % 10 == d)
            sum += i;
    }
    return sum;
}
 
// Driver code
let n = 30;
let d = 3;
document.write( getSum(n, d));
 
// This code is contributed
// by bobby
</script>
Producción: 

39

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Enfoque eficiente: mientras que D < N actualiza sum = sum + D y D = D + 10 . Imprime la suma al final.
A continuación se muestra la implementación del enfoque anterior: 
 

C++

// C++ implementation of the approach
#include <bits/stdc++.h>
using namespace std;
#define ll long long int
 
// Function to return the required sum
ll getSum(int n, int d)
{
    ll sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
int main()
{
    int n = 30, d = 3;
    cout << getSum(n, d);
    return 0;
}

Java

// Java implementation of the approach
class Solution
{
 
  
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n) {
        sum += d;
        d += 10;
    }
    return sum;
}
  
// Driver code
public static void main(String args[])
{
    int n = 30, d = 3;
    System.out.print(getSum(n, d));
     
}
}
//contributed by Arnab Kundu

Python3

# Python3 implementation of the approach
# Function to return the required sum
def getSum(n, d):
    sum = 0
    while (d <= n):
        sum += d
        d += 10
    return sum
 
# Driver code
n = 30
d = 3
print(getSum(n, d))
 
# This code is contributed
# by sahishelangia

C#

// C# implementation of the approach
using System;
class GFG
{
 
// Function to return the required sum
static long getSum(int n, int d)
{
    long sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
public static void Main()
{
    int n = 30, d = 3;
    Console.Write(getSum(n, d));
}
}
 
// This code is contributed
// by Akanksha Rai

PHP

<?php
// PHP implementation of the approach
// Function to return the required sum
function getSum($n, $d)
{
    $sum = 0;
    while ($d <= $n)
    {
        $sum += $d;
        $d += 10;
    }
    return $sum;
}
 
// Driver code
$n = 30; $d = 3;
echo(getSum($n, $d));
     
// This Code is contributed
// by Mukul Singh
?>

Javascript

<script>
// java script  implementation of the approach
// Function to return the required sum
function getSum(n, d)
{
    let sum = 0;
    while (d <= n)
    {
        sum += d;
        d += 10;
    }
    return sum;
}
 
// Driver code
let n = 30;
let d = 3;
document.write(getSum(n, d));
    
// This code is contributed
// by sravan kumar
</script>
Producción: 

39

 

Complejidad de tiempo: O(n)

Espacio Auxiliar: O(1)

Publicación traducida automáticamente

Artículo escrito por SanjayR 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 *