Integers: Recreation One (5kyu)

Integers: Recreation One (5kyu)

Description

1, 246, 2, 123, 3, 82, 6, 41 are the divisors of number 246. Squaring these divisors we get: 1, 60516, 4, 15129, 9, 6724, 36, 1681. The sum of these squares is 84100 which is 290 * 290.

Task

Find all integers between m and n (m and n integers with 1 <= m <= n) such that the sum of their squared divisors is itself a square.

We will return an array of subarrays or of tuples (in C an array of Pair) or a string. The subarrays (or tuples or Pairs) will have two elements: first the number the squared divisors of which is a square and then the sum of the squared divisors.

Example:
list_squared(1, 250) --> [[1, 1], [42, 2500], [246, 84100]]
list_squared(42, 250) --> [[42, 2500], [246, 84100]]

The form of the examples may change according to the language, see “Sample Tests”.

Note

In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings.

Solution

The step I took is to

  1. Go over all of the possible divisors between 1…num.sqrt()
  2. If it is the divisor of num, let sum += its square and another divisor’s square
  3. After checking all possible items, see if the square of the root of sum equals to sum itself.
  4. If yes, push (num, sum) into answer vector
  5. Else continue
fn list_squared(m: u64, n: u64) -> Vec<(u64, u64)> {
    let mut ans = Vec::new();
    for num in m..=n {
        let mut sum = 0;
        let root = (num as f64).sqrt() as u64;
        for item in 1..=root {
            match num % item {
                0 => {
                    sum += item * item;
                    let another = num / item;
                    if another != item {
                        sum += another * another;
                    }
                }
                _ => continue
            }
        }
        let root_sum = ((sum as f64).sqrt()) as u64;
        if root_sum * root_sum == sum {
            ans.push((num, sum));
        }
    }
    ans
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值