Created with <3 with dartpad.dev.
Created
August 1, 2023 15:30
-
-
Save ervinod/21d734ddf0e0638c5699f807ca0df8c7 to your computer and use it in GitHub Desktop.
problem-2
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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