2024-11-16: Hashad numbers. In go language, if an integer is divisible by the sum of the digits in each of its digits.
We call this integer the Harshad number.
Given an integer x
If x is a hashard number, return the sum of the digits of x;
If not, returns -1.
Input: x = 18.
Output: 9.
Explanation:
The sum of the digits of x is 9. 18 is divisible by 9. Therefore, 18 is a Hashad number and the answer is 9.
Answer 2024-11-16:
chatgpt
Title from leetcode3099.
The broad steps are as follows:
1.function definition:
- defines a function
sumOfTheDigitsOfHarshadNumber
which accepts an integerx
as a parameter for the purpose of calculating the sum of the digits of the number and determining whether it is a hashard number.
2.Initialization sum:
-
Inside the function, initialize a variable
s
0 is used to store the sum of each digit. -
In addition, the input
x
assign to a cyclic variabley
The subsequent operations will be performed with they
Instead of directly modifyingx
。
3.Calculate the sum of the digits:
3.1. use afor
loop, the loop condition isy
is not equal to 0.
3.2. in each cycle:
3.2.1.y % 10
gainy
and add the last digit of thes
Up.
3.2.2. then byy /= 10
commander-in-chief (military)y
Divide by 10 to remove the last digit.
3.3. At the end of the loop, the variables
is stored in thex
Sum of the numbers.
4.Determine if it is a Hashad number:
-
After calculating the numbers and
s
Afterwards, checkx
whether or not it can bes
Divide (x % s
). If it is not divisible, the function returns -1, indicating that thex
Not the Hashad number. -
If it is divisible, return
s
expressedx
is the Hashad number, and we return the sum of the individual numbers.
5.main function:
-
exist
main
function that defines an integerx
(in this case 18). -
invocations
sumOfTheDigitsOfHarshadNumber(x)
function and prints its return value.
time complexity
-
The step of calculating the sum of the numbers involves a comparison of the
x
of each bit to make a visit. Assuming that thex
The digits ofd
, then the time complexity is O(d). -
In decimal, the number of bits is proportional to the logarithm of the size of the number (
d = log10(x)
), so the time complexity can be considered to be O(log x).
(math.) space complexity
-
Several integer variables are used in the function (
s
cap (a poem)y
), the space occupation of these variables is at the constant level. -
Therefore, the space complexity is O(1), i.e., constant level space complexity.
summarize
-
time complexity:O(log x)
-
(math.) space complexity:O(1)
The full Go code is below:
package main
import (
"fmt"
)
func sumOfTheDigitsOfHarshadNumber(x int) int {
s := 0
for y := x; y != 0; y /= 10 {
s += y % 10
}
if x%s != 0 {
return -1
}
return s
}
func main() {
x := 18
(sumOfTheDigitsOfHarshadNumber(x))
}
The full Rust code is below:
fn sum_of_the_digits_of_harshad_number(x: i32) -> i32 {
let mut s = 0;
let mut y = x;
while y != 0 {
s += y % 10;
y /= 10;
}
if x % s != 0 {
return -1;
}
s
}
fn main() {
let x = 18;
println!("{}", sum_of_the_digits_of_harshad_number(x));
}