Skip to content

Commit f5b4eaa

Browse files
Merge pull request #1229 from yadavan88/power-of-2
Added codeto check if a number is power of 2
2 parents d093cc5 + 9f6eee1 commit f5b4eaa

File tree

3 files changed

+79
-1
lines changed

3 files changed

+79
-1
lines changed

build.sbt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,8 @@ lazy val scala_core_9 = (project in file("scala-core-modules/scala-core-9"))
116116
.settings(
117117
name := "scala-core-9",
118118
libraryDependencies ++= scalaTestDeps,
119-
scalaVersion := scala3Version
119+
scalaVersion := scala3Version,
120+
libraryDependencies += "org.scalatestplus" %% "scalacheck-1-17" % "3.2.18.0" % Test
120121
)
121122

122123
lazy val scala_core_io = (project in file("scala-core-modules/scala-core-io"))
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.baeldung.scala.powerof2
2+
3+
import scala.annotation.tailrec
4+
5+
object PowerOfTwo {
6+
7+
def isPowerOfTwoByDivision(num: Long): Boolean = {
8+
@tailrec
9+
def isPowerRec(n: Long): Boolean = {
10+
if (n == 1) true
11+
else if (n % 2 == 0) isPowerRec(n / 2)
12+
else false
13+
}
14+
num > 0 && isPowerRec(num)
15+
}
16+
17+
def isPowerOfTwoByCountingOnes(num: Long): Boolean = {
18+
num > 0 && num.toBinaryString.count(_ == '1') == 1
19+
}
20+
21+
def isPowerOfTwoByBitwiseAnd(num: Long): Boolean = {
22+
num > 0 && (num & (num - 1)) == 0
23+
}
24+
25+
def isPowerOfTwoByLazyList(num: Long): Boolean = {
26+
num > 0 && LazyList.iterate(num)(_ / 2).takeWhile(_ > 1).forall(_ % 2 == 0)
27+
}
28+
29+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.baeldung.scala.powerof2
2+
3+
import org.scalacheck.Gen
4+
import org.scalatest.flatspec.AnyFlatSpec
5+
import org.scalatest.matchers.should.Matchers
6+
import org.scalatestplus.scalacheck.ScalaCheckPropertyChecks
7+
8+
class PowerOfTwoUnitTest
9+
extends AnyFlatSpec
10+
with Matchers
11+
with ScalaCheckPropertyChecks {
12+
13+
private val powerOfTwoFunctions = Seq(
14+
("Division", PowerOfTwo.isPowerOfTwoByDivision),
15+
("Counting Ones", PowerOfTwo.isPowerOfTwoByCountingOnes),
16+
("Bitwise AND", PowerOfTwo.isPowerOfTwoByBitwiseAnd),
17+
("LazyList", PowerOfTwo.isPowerOfTwoByLazyList)
18+
)
19+
20+
powerOfTwoFunctions.foreach { (desc, fn) =>
21+
it should s"[$desc] return true for a number that is power of 2" in {
22+
val powersOfTwo: Gen[Long] =
23+
Gen.choose(0, 62).map(n => Math.pow(2, n).toLong)
24+
forAll(powersOfTwo) { num =>
25+
fn(num) shouldBe true
26+
}
27+
}
28+
29+
it should s"[$desc] return false for a number that is NOT a power of 2" in {
30+
val powersOfTwo: Gen[Long] =
31+
Gen.choose(0, 62).map(n => Math.pow(2, n).toLong)
32+
val notPowerOf2 = Gen
33+
.choose(0L, Long.MaxValue)
34+
.suchThat(n => !powersOfTwo.sample.contains(n))
35+
forAll(notPowerOf2) { num =>
36+
fn(num) shouldBe false
37+
}
38+
}
39+
40+
it should s"[$desc] return false for any negative numbers" in {
41+
val negativeNumbers = Gen.choose(Long.MinValue, 0L)
42+
forAll(negativeNumbers) { num =>
43+
fn(num) shouldBe false
44+
}
45+
}
46+
}
47+
48+
}

0 commit comments

Comments
 (0)