project euler 23

Problem 23


Non-abundant sums

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.


并非盈数之和

完全数是指真因数之和等于自身的那些数。例如,28的真因数之和为1 + 2 + 4 + 7 + 14 = 28,因此28是一个完全数。

一个数n被称为亏数,如果它的真因数之和小于n;反之则被称为盈数。

由于12是最小的盈数,它的真因数之和为1 + 2 + 3 + 4 + 6 = 16,所以最小的能够表示成两个盈数之和的数是24。通过数学分析可以得出,所有大于28123的数都可以被写成两个盈数的和;尽管我们知道最大的不能被写成两个盈数的和的数要小于这个值,但这是通过分析所能得到的最好上界。

找出所有不能被写成两个盈数之和的正整数,并求它们的和。

package projecteuler;

import java.util.ArrayList;
import java.util.BitSet;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;

import org.junit.Test;

public class Prj23 {

	/**
	 * 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.
	 */
	@Test
	public void test() {
		System.out.println(Calculator.calculate(28123));
	}

	public static class Calculator {

		public static int calculate(int upLimit) {

			List<Integer> abundantList = new ArrayList<Integer>();

			// 1, 2 , 3,
			for (int i = 1; i < upLimit; i++) {

				int sum = sumAllDivisors(getAllDivsors(i));
				if (sum - i > i) {
					abundantList.add(i);
				}
			}

			Set<Integer> set = new HashSet<Integer>();
			for (int i = 0; i < abundantList.size(); i++) {
				for (int j = 0; j < abundantList.size(); j++) {
					set.add(abundantList.get(i) + abundantList.get(j));
				}
			}

			BitSet bt = new BitSet(upLimit);

			for (int i = 1; i <= upLimit; i++) {
				if (set.contains(i)) {
					bt.set(i - 1);
				}
			}

			int sum = 0;
			for (int i = 0; i < upLimit; i++) {
				if (!bt.get(i)) {
					sum += (i + 1);
				}
			}

			return sum;
		}

		/**
		 * more simple method use Sigma(p^n) = [p^(n+1)]-1/(p-1)
		 * 
		 * @param divisors
		 * @return
		 */
		public static int sumAllDivisors(List<Integer> divisors) {
			int sum = 0;
			for (Integer i : divisors) {
				sum += i;
			}
			return sum;
		}

		public static List<Integer> getAllDivsors(int num) {

			List<Integer> primeList = new ArrayList<Integer>();

			IntegerDivisor div = new IntegerDivisor();
			div.divisor(num);

			try {
				Calculator.iterList(primeList, div.primeMap);
			} catch (Exception e) {
				e.printStackTrace();
			}

			int sum = 1;
			for (Entry<Long, Integer> entry : div.primeMap.entrySet()) {
				sum *= (entry.getValue() + 1);
			}

			assert (primeList.size() == sum);
			return primeList;
		}

		/**
		 * 迭代获取所有因子
		 * 
		 * @param divisorList
		 * @param primeMap
		 * @throws Exception
		 */
		public static void iterList(List<Integer> divisorList,
				Map<Long, Integer> primeMap) throws Exception {

			// empty
			if (primeMap.keySet().size() == 0) {
				return;
			}

			// one
			if (primeMap.keySet().size() == 1) {

				for (Entry<Long, Integer> entry : primeMap.entrySet()) {
					Long prime = entry.getKey();

					for (int i = 0; i <= entry.getValue(); i++) {
						divisorList.add((int) Math.pow(prime, i));
					}
				}

				return;
			}

			if (primeMap.keySet().size() <= 1)
				throw new Exception("iter error !!!");

			assert (primeMap.keySet().size() > 1);

			for (Entry<Long, Integer> entry : primeMap.entrySet()) {
				Long prime = entry.getKey();
				int times = entry.getValue();

				List<Integer> tmp = new ArrayList<Integer>();

				Map<Long, Integer> primeMapCopy = copyMapExceptKey(primeMap,
						prime);

				for (int i = 0; i <= times; i++) {

					List<Integer> iterRemoveThisPrime = new ArrayList<Integer>();

					iterList(iterRemoveThisPrime, primeMapCopy);

					for (int iter = 0; iter < iterRemoveThisPrime.size(); iter++) {
						tmp.add((int) (Math.pow(prime, i) * iterRemoveThisPrime
								.get(iter)));
					}

				}

				copyArrs(tmp, divisorList);

			}

			return;

		}

		private static Map<Long, Integer> copyMapExceptKey(
				Map<Long, Integer> primeMap, Long prime) {

			HashMap<Long, Integer> ret = new HashMap<Long, Integer>(primeMap);
			ret.remove(prime);

			return ret;
		}

		private static void copyArrs(List<Integer> tmp,
				List<Integer> divisorList) {

			divisorList.clear();
			divisorList.addAll(tmp);

		}

	}

	/**
	 * 因子分解
	 * 
	 * @author 1440
	 * 
	 */
	public static class IntegerDivisor {

		public Map<Long, Integer> primeMap = new HashMap<Long, Integer>();
		public List<Long> primeList = new ArrayList<Long>();

		public void clear() {
			primeMap.clear();
			primeList.clear();
		}

		public void divisor(long num) {

			if (num <= 1)
				return;

			long prime = getPrime(
					num,
					primeList.size() == 0 ? 2
							: primeList.get(primeList.size() - 1));
			if (prime < 0) {
				primeMap.put(num, 1);
				primeList.add(num);
				return;
			} else {

				primeList.add(prime);
				int count = 0;
				do {

					count += 1;
					num = num / prime;
				} while (num % prime == 0);

				primeMap.put(prime, count);

				divisor(num);

			}

		}

		private long getPrime(long num, long start) {

			for (long i = start; i <= Math.sqrt(num); i++) {
				if (num % i == 0) {
					return i;
				}
			}
			return -1;
		}

		@Override
		public String toString() {

			return print_Map(this.primeMap);
		}

		public Long getLargestPrime() {
			return primeList.get(primeList.size() - 1);
		}

	}

	public static String print_Map(Map<?, ?> primeMap) {
		StringBuilder sb = new StringBuilder();
		for (Entry<?, ?> entry : primeMap.entrySet()) {
			sb.append(entry.getKey().toString() + "="
					+ entry.getValue().toString() + "\n");
		}
		return sb.toString();
	}

	public static void print_List(List<Integer> list) {
		for (int i = 0; i < list.size(); i++) {
			System.out.print(list.get(i) + ",");
		}
		System.out.println();
	}
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
水资源是人类社会的宝贵财富,在生活、工农业生产中是不可缺少的。随着世界人口的增长及工农业生产的发展,需水量也在日益增长,水已经变得比以往任何时候都要珍贵。但是,由于人类的生产和生活,导致水体的污染,水质恶化,使有限的水资源更加紧张。长期以来,油类物质(石油类物质和动植物油)一直是水和土壤中的重要污染源。它不仅对人的身体健康带来极大危害,而且使水质恶化,严重破坏水体生态平衡。因此各国都加强了油类物质对水体和土壤的污染的治理。对于水中油含量的检测,我国处于落后阶段,与国际先进水平存在差距,所以难以满足当今技术水平的要求。为了取得具有代表性的正确数据,使分析数据具有与现代测试技术水平相应的准确性和先进性,不断提高分析成果的可比性和应用效果,检测的方法和仪器是非常重要的。只有保证了这两方面才能保证快速和准确地测量出水中油类污染物含量,以达到保护和治理水污染的目的。开展水中油污染检测方法、技术和检测设备的研究,是提高水污染检测的一条重要措施。通过本课题的研究,探索出一套适合我国国情的水质污染现场检测技术和检测设备,具有广泛的应用前景和科学研究价值。 本课题针对我国水体的油污染,探索一套检测油污染的可行方案和方法,利用非分散红外光度法技术,开发研制具有自主知识产权的适合国情的适于野外便携式的测油仪。利用此仪器,可以检测出被测水样中亚甲基、甲基物质和动植物油脂的污染物含量,为我国众多的环境检测站点监测水体的油污染状况提供依据。
### 内容概要 《计算机试卷1》是一份综合性的计算机基础和应用测试卷,涵盖了计算机硬件、软件、操作系统、网络、多媒体技术等多个领域的知识点。试卷包括单选题和操作应用两大类,单选题部分测试学生对计算机基础知识的掌握,操作应用部分则评估学生对计算机应用软件的实际操作能力。 ### 适用人群 本试卷适用于: - 计算机专业或信息技术相关专业的学生,用于课程学习或考试复习。 - 准备计算机等级考试或职业资格认证的人士,作为实战演练材料。 - 对计算机操作有兴趣的自学者,用于提升个人计算机应用技能。 - 计算机基础教育工作者,作为教学资源或出题参考。 ### 使用场景及目标 1. **学习评估**:作为学校或教育机构对学生计算机基础知识和应用技能的评估工具。 2. **自学测试**:供个人自学者检验自己对计算机知识的掌握程度和操作熟练度。 3. **职业发展**:帮助职场人士通过实际操作练习,提升计算机应用能力,增强工作竞争力。 4. **教学资源**:教师可以用于课堂教学,作为教学内容的补充或学生的课后练习。 5. **竞赛准备**:适合准备计算机相关竞赛的学生,作为强化训练和技能检测的材料。 试卷的目标是通过系统性的题目设计,帮助学生全面复习和巩固计算机基础知识,同时通过实际操作题目,提高学生解决实际问题的能力。通过本试卷的学习与练习,学生将能够更加深入地理解计算机的工作原理,掌握常用软件的使用方法,为未来的学术或职业生涯打下坚实的基础。
以下是在openeuler上安装OpenStack的步骤: 1. 确保你的openeuler系统已经安装了必要的软件包和依赖项。可以使用以下命令安装: ```shell sudo dnf install -y python3-devel libffi-devel gcc openssl-devel ``` 2. 添加OpenStack Train软件源。可以使用以下命令添加: ```shell sudo dnf install -y centos-release-openstack-train ``` 3. 安装OpenStack客户端和服务组件。可以使用以下命令安装: ```shell sudo dnf install -y python3-openstackclient openstack-selinux openstack-utils ``` 4. 配置数据库。可以使用以下命令安装MariaDB数据库: ```shell sudo dnf install -y mariadb mariadb-server python3-PyMySQL ``` 然后启动MariaDB服务并设置开机自启: ```shell sudo systemctl enable mariadb.service sudo systemctl start mariadb.service ``` 接下来,使用以下命令来配置MariaDB数据库: ```shell sudo mysql_secure_installation ``` 5. 配置消息队列。可以使用以下命令安装RabbitMQ消息队列: ```shell sudo dnf install -y rabbitmq-server ``` 然后启动RabbitMQ服务并设置开机自启: ```shell sudo systemctl enable rabbitmq-server.service sudo systemctl start rabbitmq-server.service ``` 6. 配置身份认证服务。可以使用以下命令安装Keystone身份认证服务: ```shell sudo dnf install -y openstack-keystone httpd mod_wsgi ``` 然后启动httpd服务并设置开机自启: ```shell sudo systemctl enable httpd.service sudo systemctl start httpd.service ``` 7. 配置计算服务。可以使用以下命令安装Nova计算服务: ```shell sudo dnf install -y openstack-nova-api openstack-nova-conductor \ openstack-nova-console openstack-nova-novncproxy \ openstack-nova-scheduler python3-novaclient ``` 8. 配置网络服务。可以使用以下命令安装Neutron网络服务: ```shell sudo dnf install -y openstack-neutron openstack-neutron-ml2 \ openstack-neutron-linuxbridge ebtables ipset ``` 9. 配置镜像服务。可以使用以下命令安装Glance镜像服务: ```shell sudo dnf install -y openstack-glance ``` 10. 配置块存储服务。可以使用以下命令安装Cinder块存储服务: ```shell sudo dnf install -y openstack-cinder targetcli python-keystone ``` 11. 配置对象存储服务。可以使用以下命令安装Swift对象存储服务: ```shell sudo dnf install -y openstack-swift-proxy python3-swiftclient \ python3-keystoneclient python3-keystonemiddleware \ python3-eventlet xfsprogs rsync ``` 12. 配置Dashboard服务。可以使用以下命令安装Horizon Dashboard服务: ```shell sudo dnf install -y openstack-dashboard ``` 13. 配置OpenStack服务。可以使用以下命令配置OpenStack服务: ```shell sudo openstack-config --set /etc/nova/nova.conf database connection mysql+pymysql://nova:password@controller/nova sudo openstack-config --set /etc/nova/nova.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/nova/nova.conf api auth_strategy keystone sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken project_name service sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken username nova sudo openstack-config --set /etc/nova/nova.conf keystone_authtoken password password sudo openstack-config --set /etc/nova/nova.conf DEFAULT my_ip 10.0.0.11 sudo openstack-config --set /etc/nova/nova.conf DEFAULT use_neutron True sudo openstack-config --set /etc/nova/nova.conf DEFAULT firewall_driver nova.virt.firewall.NoopFirewallDriver sudo openstack-config --set /etc/nova/nova.conf vnc enabled true sudo openstack-config --set /etc/nova/nova.conf vnc server_listen 0.0.0.0 sudo openstack-config --set /etc/nova/nova.conf vnc server_proxyclient_address \$my_ip sudo openstack-config --set /etc/nova/nova.conf vnc novncproxy_base_url http://controller:6080/vnc_auto.html sudo openstack-config --set /etc/nova/nova.conf glance api_servers http://controller:9292 sudo openstack-config --set /etc/nova/nova.conf oslo_concurrency lock_path /var/lib/nova/tmp sudo openstack-config --set /etc/neutron/neutron.conf database connection mysql+pymysql://neutron:password@controller/neutron sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/neutron/neutron.conf DEFAULT auth_strategy keystone sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken project_name service sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken username neutron sudo openstack-config --set /etc/neutron/neutron.conf keystone_authtoken password password sudo openstack-config --set /etc/neutron/neutron.conf oslo_concurrency lock_path /var/lib/neutron/tmp sudo openstack-config --set /etc/glance/glance-api.conf database connection mysql+pymysql://glance:password@controller/glance sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken project_name service sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken username glance sudo openstack-config --set /etc/glance/glance-api.conf keystone_authtoken password password sudo openstack-config --set /etc/glance/glance-api.conf paste_deploy flavor keystone sudo openstack-config --set /etc/glance/glance-api.conf glance_store stores file,http sudo openstack-config --set /etc/glance/glance-api.conf glance_store default_store file sudo openstack-config --set /etc/glance/glance-api.conf glance_store filesystem_store_datadir /var/lib/glance/images/ sudo openstack-config --set /etc/glance/glance-registry.conf database connection mysql+pymysql://glance:password@controller/glance sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken project_name service sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken username glance sudo openstack-config --set /etc/glance/glance-registry.conf keystone_authtoken password password sudo openstack-config --set /etc/glance/glance-registry.conf paste_deploy flavor keystone sudo openstack-config --set /etc/cinder/cinder.conf database connection mysql+pymysql://cinder:password@controller/cinder sudo openstack-config --set /etc/cinder/cinder.conf DEFAULT transport_url rabbit://openstack:password@controller sudo openstack-config --set /etc/cinder/cinder.conf DEFAULT auth_strategy keystone sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken www_authenticate_uri http://controller:5000 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken auth_type password sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_domain_name Default sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken user_domain_name Default sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken project_name service sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken username cinder sudo openstack-config --set /etc/cinder/cinder.conf keystone_authtoken password password sudo openstack-config --set /etc/cinder/cinder.conf oslo_concurrency lock_path /var/lib/cinder/tmp sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT bind_port 8080 sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT user swift sudo openstack-config --set /etc/swift/proxy-server.conf DEFAULT swift_dir /etc/swift sudo openstack-config --set /etc/swift/proxy-server.conf pipeline:main pipeline "catch_errors healthcheck cache authtoken keystoneauth proxy-server" sudo openstack-config --set /etc/swift/proxy-server.conf filter:keystoneauth use "egg:swift#keystoneauth" sudo openstack-config --set /etc/swift/proxy-server.conf filter:keystoneauth operator_roles admin,user sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken paste.filter_factory keystonemiddleware.auth_token:filter_factory sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_uri http://controller:5000 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_url http://controller:5000 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken memcached_servers controller:11211 sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken auth_type password sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken project_domain_name Default sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken user_domain_name Default sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken project_name service sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken username swift sudo openstack-config --set /etc/swift/proxy-server.conf filter:authtoken password password sudo openstack-config --set /etc/swift/proxy-server.conf filter:cache use "egg:swift#memcache" sudo openstack-config --set /etc/swift/proxy-server.conf filter:cache memcache_servers controller:11211 sudo openstack-config --set /etc/swift/proxy-server.conf filter:

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值