Problem23

寻找不能表示为两个丰数之和的正整数
部署运行你感兴趣的模型镜像
A perfect number is a number for which the sum of its proper divisors is exactly equal to the number. For example, the sum of the proper divisors of 28 would be 1 + 2 + 4 + 7 + 14 = 28, which means that 28 is a perfect number.

A number n is called deficient if the sum of its proper divisors is less than n and it is called abundant if this sum exceeds n.

As 12 is the smallest abundant number, 1 + 2 + 3 + 4 + 6 = 16, the smallest number that can be written as the sum of two abundant numbers is 24. By mathematical analysis, it can be shown that all integers greater than 28123 can be written as the sum of two abundant numbers. However, this upper limit cannot be reduced any further by analysis even though it is known that the greatest number that cannot be expressed as the sum of two abundant numbers is less than this limit.

Find the sum of all the positive integers which cannot be written as the sum of two abundant numbers.

package com.yao;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
* Created by IntelliJ IDEA.
* User: Administrator
* Date: 12-3-18
* Time: 下午12:50
*/
public class Problem23 {
public static void main(String[] args) {
List<Integer> list=new ArrayList<Integer>();
for(int i=1;i<=28123;i++){
if(sum(i)>i)
{
list.add(i);
}
}
Set<Integer> abundant2sum =new HashSet<Integer>();
int size=list.size();
int sum=0;
for(int i=1;i<=28123;i++){
sum+=i;
}
for(int i=0;i<size;i++)
for(int j=i;j<size;j++){
int k=list.get(i)+list.get(j);
if(k<=28123){
if(!abundant2sum.contains(k)){
abundant2sum.add(k);
sum-=k;
}
}

}
System.out.println(sum);

}
private static int sum(int n) {
if(n==1)return 0;
int sum=1;
int middle=(int)Math.sqrt(n);
for(int j=2;j<=middle;j++){
if(n%j==0){
int k=n/j;
if(k==j)
sum+=j;
else
sum+=(k+j);
}
}
return sum;
}
}

您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

### LeetCode Problem 23: Merge k Sorted Lists 对于这个问题,目标是从给定的`k`个升序链表中合并所有链表并返回一个已排序的链表。可以采用分治法来解决此问题,通过两两合并的方式逐步减少列表数量直到只剩下一个最终的结果链表[^1]。 为了实现这一算法,一种有效的方法是利用优先队列(最小堆)。将每个链表的第一个节点加入到最小堆中;每次从堆顶取出当前最小值作为新链表的一部分,并将其所在原链表中的下一节点继续放入堆中参与后续比较。重复上述过程直至处理完所有的节点。 以下是Python版本的具体代码实现: ```python from typing import List, Optional import heapq class ListNode: def __init__(self, val=0, next=None): self.val = val self.next = next # Define comparison methods for heap operations def __lt__(self, other): return self.val < other.val def mergeKLists(lists: List[ListNode]) -> Optional[ListNode]: min_heap = [] # Initialize the heap with head nodes of all lists. for l in lists: if l: heapq.heappush(min_heap, (l.val, l)) dummy_head = cur_node = ListNode(0) while min_heap: _, node = heapq.heappop(min_heap) # Add this node's value into our result list. cur_node.next = node cur_node = cur_node.next # If there are more elements on current linked-list, # push them back onto the priority queue. if node.next: heapq.heappush(min_heap, (node.next.val, node.next)) return dummy_head.next ``` 该方法的时间复杂度主要取决于构建和维护最小堆的过程,即O(Nlogk),其中N表示所有链表内元素总数目而k代表输入链表的数量。空间复杂度则由用于存储待处理节点的小根堆决定,最坏情况下为O(k)。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值