상세 컨텐츠

본문 제목

[Codility] ParityDegree (Go)

Development/Algorithm

by thisisnew 2023. 1. 4. 00:01

본문

반응형

https://app.codility.com/programmers/trainings/5/parity_degree/

 

ParityDegree coding task - Practice Coding - Codility

Find the highest power of 2 that divides N.

app.codility.com

Task description

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:

  • N is an integer within the range [1..1,000,000,000].

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++
	}
}

반응형

관련글 더보기

댓글 영역