给定一个整数N ,任务是查找从2到N / 2的所有基数中写入的N的数字的总和。
例子:
Input: N = 6
Output: 4
In base 2, 6 is represented as 110.
In base 3, 6 is represented as 20.
Sum = 1 + 1 + 0 + 2 + 0 = 4
Input: N = 8
Output: 7
方法:
- 对于从2到(n / 2)的每个底数,使用以下公式计算特定底数中n的数字:
- 通过将n除以底数来计算余数,余数是该底数中n的数字之一。
- 将数字加到总和并更新n为(n = n / base) 。
- 当n> 0时重复上述步骤
- 打印在先前步骤中计算出的总和。
下面是上述方法的实现:
C++
// C++ implementation of the approach
#include
using namespace std;
// Function to calculate the sum of the digits of
// n in the given base
int solve(int n, int base)
{
// Sum of digits
int sum = 0;
while (n > 0) {
// Digit of n in the given base
int remainder = n % base;
// Add the digit
sum += remainder;
n = n / base;
}
return sum;
}
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
void SumsOfDigits(int n)
{
// to store digit sum in all bases
int sum = 0;
// function call for multiple bases
for (int base = 2; base <= n / 2; ++base)
sum += solve(n, base);
cout << sum;
}
// Driver program
int main()
{
int n = 8;
SumsOfDigits(n);
return 0;
}
Java
// Java implementation of the approach
import java.io.*;
class GFG {
// Function to calculate the sum of the digits of
// n in the given base
static int solve(int n, int base)
{
// Sum of digits
int sum = 0;
while (n > 0) {
// Digit of n in the given base
int remainder = n % base;
// Add the digit
sum += remainder;
n = n / base;
}
return sum;
}
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
static void SumsOfDigits(int n)
{
// to store digit sum in all bases
int sum = 0;
// function call for multiple bases
for (int base = 2; base <= n / 2; ++base)
sum += solve(n, base);
System.out.println(sum);
}
// Driver program
public static void main (String[] args) {
int n = 8;
SumsOfDigits(n);
}
}
// This code is contributed by anuj_67..
Python3
# Python 3 implementation of the approach
from math import floor
# Function to calculate the sum of the digits of
# n in the given base
def solve(n, base):
# Sum of digits
sum = 0
while (n > 0):
# Digit of n in the given base
remainder = n % base
# Add the digit
sum = sum + remainder
n = int(n / base)
return sum
# Function to calculate the sum of
# digits of n in bases from 2 to n/2
def SumsOfDigits(n):
# to store digit sum in all base
sum = 0
N = floor(n/2)
# function call for multiple bases
for base in range(2,N+1,1):
sum = sum + solve(n, base)
print(sum)
# Driver program
if __name__ == '__main__':
n = 8
SumsOfDigits(n)
# This code is contributed by
# Surendra_Gangwar
C#
// C# implementation of the approach
using System;
class GFG
{
// Function to calculate the sum of
// the digits of n in the given base
static int solve(int n, int base1)
{
// Sum of digits
int sum = 0;
while (n > 0)
{
// Digit of n in the given base
int remainder1 = n % base1;
// Add the digit
sum += remainder1;
n = n / base1;
}
return sum;
}
// Function to calculate the sum of
// digits of n in base1s from 2 to n/2
static void SumsOfDigits(int n)
{
// to store digit sum in all bases
int sum = 0;
// function call for multiple bases
for (int base1 = 2;
base1 <= n / 2; ++base1)
sum += solve(n, base1);
Console.WriteLine(sum);
}
// Driver Code
public static void Main (String []args)
{
int n = 8;
SumsOfDigits(n);
}
}
// This code is contributed by Arnab Kundu
PHP
0)
{
// Digit of n in the given base
$remainder = $n % $base;
// Add the digit
$sum += $remainder;
$n = $n / $base;
}
return $sum;
}
// Function to calculate the sum of
// digits of n in bases from 2 to n/2
function SumsOfDigits($n)
{
// to store digit sum in all bases
$sum = 0;
// function call for multiple bases
for ($base = 2;
$base <= $n / 2; ++$base)
$sum += solve($n, $base);
echo $sum;
}
// Driver Code
$n = 8;
SumsOfDigits($n);
// This code is contributed
// by Akanksha Rai
?>
Javascript
输出:
7
如果您希望与行业专家一起参加现场课程,请参阅《 Geeks现场课程》和《 Geeks现场课程美国》。