给定n和一个数字,任务是找到可被给定数字整除的n个数字的总和。
例子:
Input : n = 2, number = 7
Output : 728
There are nine n digit numbers that
are divisible by 7. Numbers are 14+
21 + 28 + 35 + 42 + 49 + .... + 97.
Input : n = 3, number = 7
Output : 70336
Input : n = 3, number = 4
Output : 124200
本机方法:遍历所有n位数字。对于每个数字,请检查其除数,然后求和。
C++
// Simple CPP program to sum of n digit
// divisible numbers.
#include
#include
using namespace std;
// Returns sum of n digit numbers
// divisible by 'number'
int totalSumDivisibleByNum(int n, int number)
{
// compute the first and last term
int firstnum = pow(10, n - 1);
int lastnum = pow(10, n);
// sum of number which having
// n digit and divisible by number
int sum = 0;
for (int i = firstnum; i < lastnum; i++)
if (i % number == 0)
sum += i;
return sum;
}
// Driver code
int main()
{
int n = 3, num = 7;
cout << totalSumDivisibleByNum(n, num) << "\n";
return 0;
}
Java
// Simple Java program to sum of n digit
// divisible numbers.
import java.io.*;
class GFG {
// Returns sum of n digit numbers
// divisible by 'number'
static int totalSumDivisibleByNum(int n, int number)
{
// compute the first and last term
int firstnum = (int)Math.pow(10, n - 1);
int lastnum = (int)Math.pow(10, n);
// sum of number which having
// n digit and divisible by number
int sum = 0;
for (int i = firstnum; i < lastnum; i++)
if (i % number == 0)
sum += i;
return sum;
}
// Driver code
public static void main (String[] args)
{
int n = 3, num = 7;
System.out.println(totalSumDivisibleByNum(n, num));
}
}
// This code is contributed by Ajit.
Python3
# Simple Python 3 program to sum
# of n digit divisible numbers.
# Returns sum of n digit numbers
# divisible by 'number'
def totalSumDivisibleByNum(n, number):
# compute the first and last term
firstnum = pow(10, n - 1)
lastnum = pow(10, n)
# sum of number which having
# n digit and divisible by number
sum = 0
for i in range(firstnum, lastnum):
if (i % number == 0):
sum += i
return sum
# Driver code
n = 3; num = 7
print(totalSumDivisibleByNum(n, num))
# This code is contributed by Smitha Dinesh Semwal
C#
// Simple C# program to sum of n digit
// divisible numbers.
using System;
class GFG {
// Returns sum of n digit numbers
// divisible by 'number'
static int totalSumDivisibleByNum(int n, int number)
{
// compute the first and last term
int firstnum = (int)Math.Pow(10, n - 1);
int lastnum = (int)Math.Pow(10, n);
// sum of number which having
// n digit and divisible by number
int sum = 0;
for (int i = firstnum; i < lastnum; i++)
if (i % number == 0)
sum += i;
return sum;
}
// Driver code
public static void Main ()
{
int n = 3, num = 7;
Console.WriteLine(totalSumDivisibleByNum(n, num));
}
}
// This code is contributed by vt_m.
PHP
Javascript
C++
// Efficient CPP program to find the sum
// divisible numbers.
#include
#include
using namespace std;
// find the Sum of having n digit and
// divisible by the number
int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = pow(10, digit - 1);
int lastnum = pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
int main()
{
int n = 3, number = 7;
cout << totalSumDivisibleByNum(n, number);
return 0;
}
Java
// Efficient Java program to find the sum
// divisible numbers.
import java.io.*;
class GFG {
// find the Sum of having n digit and
// divisible by the number
static int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = (int)Math.pow(10, digit - 1);
int lastnum = (int)Math.pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
// Driver code
public static void main (String[] args)
{
int n = 3, number = 7;
System.out.println(totalSumDivisibleByNum(n, number));
}
}
// This code is contributed by Ajit.
Python3
# Efficient Python3 program to
# find the sum divisible numbers.
# find the Sum of having n digit
# and divisible by the number
def totalSumDivisibleByNum(digit, number):
# compute the first and last term
firstnum = pow(10, digit - 1)
lastnum = pow(10, digit)
# first number which is divisible
# by given number
firstnum = (firstnum - firstnum % number) + number
# last number which is divisible
# by given number
lastnum = (lastnum - lastnum % number)
# total divisible number
count = ((lastnum - firstnum) / number + 1)
# return the total sum
return int(((lastnum + firstnum) * count) / 2)
# Driver code
digit = 3; num = 7
print(totalSumDivisibleByNum(digit, num))
# This code is contributed by Smitha Dinesh Semwal
C#
// Efficient Java program to find the sum
// divisible numbers.
using System;
class GFG {
// find the Sum of having n digit and
// divisible by the number
static int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = (int)Math.Pow(10, digit - 1);
int lastnum = (int)Math.Pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
// Driver code
public static void Main ()
{
int n = 3, number = 7;
Console.WriteLine(totalSumDivisibleByNum(n, number));
}
}
// This code is contributed by vt_m.
PHP
输出:
70336
高效方法:
首先,找到可被给定数字整除的n位数字的计数。然后对AP的总和应用公式。
count/2 * (first-term + last-term)
C++
// Efficient CPP program to find the sum
// divisible numbers.
#include
#include
using namespace std;
// find the Sum of having n digit and
// divisible by the number
int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = pow(10, digit - 1);
int lastnum = pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
int main()
{
int n = 3, number = 7;
cout << totalSumDivisibleByNum(n, number);
return 0;
}
Java
// Efficient Java program to find the sum
// divisible numbers.
import java.io.*;
class GFG {
// find the Sum of having n digit and
// divisible by the number
static int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = (int)Math.pow(10, digit - 1);
int lastnum = (int)Math.pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
// Driver code
public static void main (String[] args)
{
int n = 3, number = 7;
System.out.println(totalSumDivisibleByNum(n, number));
}
}
// This code is contributed by Ajit.
Python3
# Efficient Python3 program to
# find the sum divisible numbers.
# find the Sum of having n digit
# and divisible by the number
def totalSumDivisibleByNum(digit, number):
# compute the first and last term
firstnum = pow(10, digit - 1)
lastnum = pow(10, digit)
# first number which is divisible
# by given number
firstnum = (firstnum - firstnum % number) + number
# last number which is divisible
# by given number
lastnum = (lastnum - lastnum % number)
# total divisible number
count = ((lastnum - firstnum) / number + 1)
# return the total sum
return int(((lastnum + firstnum) * count) / 2)
# Driver code
digit = 3; num = 7
print(totalSumDivisibleByNum(digit, num))
# This code is contributed by Smitha Dinesh Semwal
C#
// Efficient Java program to find the sum
// divisible numbers.
using System;
class GFG {
// find the Sum of having n digit and
// divisible by the number
static int totalSumDivisibleByNum(int digit,
int number)
{
// compute the first and last term
int firstnum = (int)Math.Pow(10, digit - 1);
int lastnum = (int)Math.Pow(10, digit);
// first number which is divisible
// by given number
firstnum = (firstnum - firstnum % number)
+ number;
// last number which is divisible
// by given number
lastnum = (lastnum - lastnum % number);
// total divisible number
int count = ((lastnum - firstnum) /
number + 1);
// return the total sum
return ((lastnum + firstnum) * count) / 2;
}
// Driver code
public static void Main ()
{
int n = 3, number = 7;
Console.WriteLine(totalSumDivisibleByNum(n, number));
}
}
// This code is contributed by vt_m.
的PHP
输出:
70336