I need an optimal algorithm to find the largest divisor of a number N. Preferably in C++ or C#

前端 未结 12 1547
无人共我
无人共我 2020-12-29 11:51

I am currently using the following code but its very slow for large numbers



        static int divisor(int number)
        {
            int i;
                    


        
12条回答
  •  一向
    一向 (楼主)
    2020-12-29 12:24

    Some additional optimizations:

    1.  If even then divisable by 2.
    2.  If the sum of the digits is divisable by 3, then number is divisble by 3 (ie 78 = 7 + 8 = 15 = 1 + 5 = 6 which is divisable by 3)
    3.  If it ends in 0 or 5 it is divisable by 5
    

    Gordon.

提交回复
热议问题