Problem 27

本文探讨了一种通过二次多项式生成连续素数的方法,并提供了一段Java代码实现。通过对不同系数的二次方程进行遍历,寻找能够生成最多连续素数的公式及其系数乘积。

问题描述:

Euler published the remarkable quadratic formula:

n² + n + 41

It turns out that the formula will produce 40 primes for the consecutive values n = 0 to 39. However, when n = 40, 402 + 40 + 41 = 40(40 + 1) + 41 is divisible by 41, and certainly when n = 41, 41² + 41 + 41 is clearly divisible by 41.

Using computers, the incredible formula  n² − 79n + 1601 was discovered, which produces 80 primes for the consecutive values n = 0 to 79. The product of the coefficients, −79 and 1601, is −126479.

Considering quadratics of the form:

 

解决问题:

 利用题目本身的限制条件,因为a和b是1000以下,那么先使用boolean[] prime = new boolean[1000001];

 

 

public static boolean IsPrime(int number){
		boolean result = true;
		
		if(number%2==0){
			result = false;
		}else{
			int middle = (int)Math.sqrt(number);
			for(int i=3; i<=middle; i+=2){
				if(number%i==0){
					result = false;
					break;
				}
			}
		}
		
		return result;
	}
	
	public static void find(){
		int a =0, b=0;
		int max_len = 0;
		//先找出1000以内的所有素数
		boolean[] prime = new boolean[1000001];
		Arrays.fill(prime, false);
		for(int i=2;i<=1000001;i++){
			if(IsPrime(i)){
				prime[i] = true;
			}
		}
		
		for(int i=-1000; i<=1000; i++){ //循环a
			for(int j=-1000; j<=1000; j++){ //循环b
				int n;
				for( n=0; n<Math.abs(j); n++){
					int value = n*n+ i*n + j;
					if(value>0&&prime[value]){
						;
					}else{
						break;
					}
				}
				if(max_len<n){
					a = i;
					b = j;
					max_len = n;
				}
			}
		}
		System.out.println("a:"+a+",b:"+b+",len"+max_len);
		System.out.println(a*b);
	}

 

public List<OaCoopApprove> findDoneList(String userName) { Criteria criteria = this.getSession().createCriteria(OaCoopApprove.class, "a"); criteria.createAlias("oaWorkflowNode", "b", Criteria.INNER_JOIN); criteria.add(Restrictions.ne("b.type", ScpConstants.NUMBER_3)); criteria.add(Restrictions.eq("a.userName", userName)); DetachedCriteria subQuery = DetachedCriteria.forClass(OaCoopApprove.class, "c"); subQuery.setProjection(Projections.max("c.workDate")); subQuery.add(Restrictions.eqProperty("c.oaCoopId", "a.oaCoopId")); subQuery.add(Restrictions.eq("c.userName", userName)); criteria.add(Subqueries.propertyEq("a.workDate", subQuery)); criteria.setFirstResult(0); criteria.setMaxResults(4); List<OaCoopApprove> list = criteria.list(); if (list != null && list.size() > 0) { return list; } return new ArrayList<OaCoopApprove>(); } Hibernate: select top 5 this_.id as id147_0_, this_.chengWenClass as chengWen2_147_0_, this_.contentId as contentId147_0_, this_.createDate as createDate147_0_, this_.createUserId as createUs5_147_0_, this_.createUserName as createUs6_147_0_, this_.state as state147_0_, this_.updateTime as updateTime147_0_, this_.updateUserId as updateUs9_147_0_, this_.updateUserName as updateU10_147_0_ from KY_CHENG_WEN_LOG this_ where this_.state=? order by this_.updateTime asc Hibernate: select this_.id as id536_0_, this_.affirmDate as affirmDate536_0_, this_.assessMess as assessMess536_0_, this_.assessedObject as assessed4_536_0_, this_.basisStandard as basisSta5_536_0_, this_.chapterNumber as chapterN6_536_0_, this_.checkListCode as checkLis7_536_0_, this_.checkListId as checkLis8_536_0_, this_.checkListProblemNo as checkLis9_536_0_, this_.checkListVersion as checkLi10_536_0_, this_.checkProject as checkPr11_536_0_, this_.correctiveMeasures as correct12_536_0_, this_.createDate as createDate536_0_, this_.createId as createId536_0_, this_.depId as depId536_0_, this_.depName as depName536_0_, this_.documents as documents536_0_, this_.fileCode as fileCode536_0_, this_.fileName as fileName536_0_, this_.fileSort as fileSort536_0_, this_.findItem as findItem536_0_, this_.flowState as flowState536_0_, this_.folderName as folderName536_0_, this_.lastUpdateDate as lastUpd24_536_0_, this_.lastUpdateId as lastUpd25_536_0_, this_.page as page536_0_, this_.problemPersonId as problem27_536_0_, this_.problemPersonName as problem28_536_0_, this_.proportionOfProblems as proport29_536_0_, this_.rank as rank536_0_, this_.remark as remark536_0_, this_.reportDate as reportDate536_0_, this_.stage as stage536_0_, this_.status as status536_0_, this_.technicalOrManagementIssues as technic35_536_0_, this_.versionCode as version36_536_0_ from NB_PROBLEM_REPORT this_ where this_.id NOT IN(select contentId from OA_COOP) 2025-07-23 15:25:40,538 - 500.jsp -2001219 [http-bio-8080-exec-5] ERROR 500.jsp - Unknown entity: null org.hibernate.impl.SessionFactoryImpl.getEntityPersister(SessionFactoryImpl.java:693) org.hibernate.loader.criteria.CriteriaQueryTranslator.getPropertyMapping(CriteriaQueryTranslator.java:596) org.hibernate.loader.criteria.CriteriaQueryTranslator.getColumns(CriteriaQueryTranslator.java:483) org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:498) org.hibernate.loader.criteria.CriteriaQueryTranslator.findColumns(CriteriaQueryTranslator.java:503) org.hibernate.criterion.PropertyExpression.toSqlString(PropertyExpression.java:52) org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380) org.hibernate.loader.criteria.CriteriaJoinWalker.<init>(CriteriaJoinWalker.java:102) org.hibernate.criterion.SubqueryExpression.toSqlString(SubqueryExpression.java:72) org.hibernate.loader.criteria.CriteriaQueryTranslator.getWhereCondition(CriteriaQueryTranslator.java:380) org.hibernate.loader.cr
最新发布
07-24
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值