https://app.codility.com/programmers/trainings/5/parity_degree/
A positive integer N is given. The goal is to find the highest power of 2 that divides N. In other words, we have to find the maximum K for which N modulo 2^K is 0.
For example, given integer N = 24 the answer is 3, because 2^3 = 8 is the highest power of 2 that divides N.
Write a function:
func Solution(N int) int
that, given a positive integer N, returns the highest power of 2 that divides N.
For example, given integer N = 24, the function should return 3, as explained above.
Assume that:
In your solution, focus on correctness. The performance of your solution will not be the focus of the assessment.
package solution
import "math"
func Solution(N int) int {
var result = 0
var n float64 = 0
for {
pow := int(math.Pow(2.0, n))
if pow > N {
return result
}
if N%(pow) == 0 {
result = int(n)
}
n++
}
}
[Programmers] Lv.1 개인정보 수집 유효기간 (Go) (0) | 2023.01.06 |
---|---|
[Codility] StrSymmetryPoint (Go) (0) | 2023.01.05 |
[Codility] ParkingBill (Go) (0) | 2023.01.03 |
[Codility] BinaryGap (Go) (0) | 2023.01.02 |
[백준] 2154번 수 이어 쓰기 3 (Go) (2) | 2022.12.30 |
댓글 영역