Skip to content

Instantly share code, notes, and snippets.

@ervinod
Created August 1, 2023 15:30
Show Gist options
  • Select an option

  • Save ervinod/21d734ddf0e0638c5699f807ca0df8c7 to your computer and use it in GitHub Desktop.

Select an option

Save ervinod/21d734ddf0e0638c5699f807ca0df8c7 to your computer and use it in GitHub Desktop.
problem-2
void main() {
// Given a positive integer n, calculate the following sum:
// n + n/2 + n/4 + n/8 + ...
// All elements of the sum are the results of integer division.
// Example
// 25 => 25 + 12 + 6 + 3 + 1 = 47
int no = 25;
int sum = 25;
while (no > 0) {
no = getDivision(no);
sum = sum + no;
}
print("sum: $sum");
}
int getDivision(int no) {
return (no / 2).floor();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment