kainster

never too late
posts - 33, comments - 3, trackbacks - 0, articles - 0
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

怎么就不对了呢

Posted on 2008-10-28 12:58 kainster 阅读(143) 评论(0)  编辑  收藏

For a positive integer n, let σ2(n) be the sum of the squares of its divisors. For example,

σ2(10) = 1 + 4 + 25 + 100 = 130.

 

Find the sum of all n, 0 n 64,000,000 such that σ2(n) is a perfect square.

using System;
using System.Collections.Generic;
using System.Text;

namespace Prob211
{
    
class Program
    
{
        
const int NUM = 64000000;
        
// 为了方便,values数组从1开始计数
        static long[] values = new long[NUM ];
        
// 验证某个数是不是完全平方数
        static bool IsPerfect(long num)
        
{
            
long sq = (long)Math.Sqrt(num);
            
            
if (sq * sq == num)
                
return true;
            
return false;
        }

        
static void Prob211()
        
{
            
for (int i = 0; i < values.Length; i++)
                values[i] 
= 0;
            
long sum = 0;
            
for (int i = 1; i < values.Length; i++)
            
{
                
long num = i;
                
// 对于i的每个倍数都加上 i*i
                while (num < values.Length)
                
{
                    values[num] 
+= i * i;
                    num 
+= i;
                }

                
// 判断当前数是否是完全平方数
                if (IsPerfect(values[i]))
                
{
                    sum 
+= i;
                    Console.WriteLine(i 
+ ":" + values[i]);
                }

            }

            Console.WriteLine(sum);

        }

        
static void Main(string[] args)
        
{
            Prob211();
            Console.Read();
        }

    }

}

只有注册用户登录后才能发表评论。


网站导航:
博客园   IT新闻   Chat2DB   C++博客   博问