Greatest Odd Divisors

Let us define a function f(x) to be the greatest odd divisor of x, where x is a positive integer. You are given a positive integer N. Return f(1)+f(2)+. . .+f(N).

 

Input

The input will contain an integer N on a separate line. N will be between 1 and 100000, inclusive.

 

Output

You have to print the result for each input case on a separate line.

 

Sample Input

7

1

777

 

Sample Output

21

1

201537

 

using System;

using System.Collections.Generic;

using System.Text;

using System.Text.RegularExpressions;

using System.Collections;

 

namespace ConsoleApplication1

{

    class Program

    {

        static void Main(string[] args)

        {

            double num = 777;

            double sum = 0;

            for (double i = 1; i <= num; i++)

            {

                sum = sum + GreatestOddDivisor(i);

            }

            Console.WriteLine("Sum of Greatest Odd Divisors = " + sum);

        }

        public static double GreatestOddDivisor(double num)

        {

            if (num % 2 != 0)

            {

                return num;

            }

            else

            {

                for (double i = num / 2; i > 0 ; i–)

                {

                    if (num % i == 0 && i % 2 != 0)

                    {

                        return i;

                    }

                }

                return 0;

            }

        }

    }

}

Advertisements
This entry was posted in Information Technology. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s