]]>问题10Q求于2000000的质C?/title>http://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338831.htmlClumsyBirdClumsyBirdTue, 23 Nov 2010 09:22:00 GMThttp://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338831.htmlhttp://www.aygfsteel.com/zhou-zhihao/comments/338831.htmlhttp://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338831.html#Feedback0http://www.aygfsteel.com/zhou-zhihao/comments/commentRss/338831.htmlhttp://www.aygfsteel.com/zhou-zhihao/services/trackbacks/338831.html
“10以下的质C和ؓ(f)2+3+5+7Q?7Q求2000000以下的质C和?”
此问题相Ҏ(gu)较简单,在前面的问题中已l给数判断的Ҏ(gu)Q具体代码如下:(x)
/** *//** * 判断是否是素?br />
* * @param n * @return */ publicstaticboolean isPrimeNumber(int n) { if (n <2) { returnfalse; } double max = Math.sqrt(n); for (int i =2; i <= max; i++) { if (n % i ==0) { returnfalse; } } returntrue; }
问题的实现方法如下:(x)
/** *//** * 于n的质C?br />
* @param n * @return */ privatestatic Long getPrimeNumberSum(int n) { int i =2; Long sum =2L; while (i <= n) { if (AlgorithmUtil.isPrimeNumber(++i)) { sum += i; } } return sum; }
卛_得到{案142913828922?br />
E稍优化一下,
/** *//** * 于n的质C?br />
* * @param n * @return */ privatestatic Long getPrimeNumberSum(int n) { int i =5; Long sum =5L;//׃2Q?都是质数Q初始gؓ(f)5 while (i <= n) { if (AlgorithmUtil.isPrimeNumber(i +=2)) {//质数 不能?整除 sum += i; } if (i <= n && AlgorithmUtil.isPrimeNumber(i +=4)) {//不能?整除 sum += i; } } return sum; }
]]>问题9Q求毕达哥拉斯三元数l{a,b,c},使得a+b+cQ?000,l出a*b*chttp://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338828.htmlClumsyBirdClumsyBirdTue, 23 Nov 2010 09:09:00 GMThttp://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338828.htmlhttp://www.aygfsteel.com/zhou-zhihao/comments/338828.htmlhttp://www.aygfsteel.com/zhou-zhihao/archive/2010/11/23/338828.html#Feedback0http://www.aygfsteel.com/zhou-zhihao/comments/commentRss/338828.htmlhttp://www.aygfsteel.com/zhou-zhihao/services/trackbacks/338828.html
“毕达哥拉斯三元数l存在{a,b,c},a<b<c,使得a^2+b^2=c^2Q如3^2+4^2=5^2=25,求aQbQc满以上条gQƈ使a+b+c=1000,l出a*b*c的倹{?#8221;
代码如下Q?br />
/** *//** * 求毕辑֓拉斯三元数组{a,b,c},使得a+b+cQtarget . 毕达哥拉斯三元数l存在{a,b,c},a<b<c,使得a^2+b^2=c^2 * a > 3,(target-(a+b))^2=c^2=a^2+b^2 --> target^2=2*target*(a+b)-2ab * * @return */ privatestaticint getNumber(int target) { int a =0; int b =0; int c =0; for (int i =3; i < target; i++) { for (int j = i; j < target; j++) { if (target * target ==2* target * i +2* target * j -2* i * j) {// target^2=2*target*(a+b)-2ab a = i; b = j; break; } } } c = target - a - b; return a * b * c; }
privatestaticint getPrimeNumberBy(int n) { int j =1; int i =1; int result =0; while (j < n) { if (AlgorithmUtil.isPrimeNumber(i)) { result = i; j++; } i +=2; } return result; }
下面是判断质数的代码Q?br />
/** *//** * 判断是否是素?br />
* * @param n * @return */ publicstaticboolean isPrimeNumber(int n) { if (n <2) { returnfalse; } double max = Math.sqrt(n); for (int i =2; i <= max; i++) { if (n % i ==0) { returnfalse; } } returntrue; }
/** *//** * 求前n个自然数和^方与qx和之?br />
* @param n * @return */ privatestaticint getDifference(int n) { int first =0; int second =0; for (int i =1; i <= n; i++) { first += i * i; second += i; } return second * second - first; }
/** *//** * 数字i从m到nQ遍历,如果i不能被result整除Q我们就i除以i与result的最大公U数Qƈ与当前result想乘 * */ privatestaticint getNumber(int m, int n) { int result = n; for (int i = n -1; i >= m; i--) { if (result % i !=0) { result *= i/gcd(result,i); } } return result; } /** *//** * 最大公U数Q欧几里L(fng)?nbsp;定理Qgcd(a,b) = gcd(b,a mod b) * * @param a * @param b * @return */ privatestaticint gcd(int a, int b) { if (b !=0) return gcd(b, a % b); else return a; }
/** *//** * 最大公U数Q欧几里L(fng)?br />
* @param a * @param b * @return */ privatestaticint gcd1(int a, int b) { if (a > b) { gcd1(b, a); } int temp =0; for (; b !=0;) { temp = a % b; a = b; b = temp; } return a; } /** *//** * 最大公U数QStein法 gcd(ka,kb) = k gcd(a,b)Q也是最大公U数q算和倍乘q算可以交换Q?br />
* Ҏ(gu)的,当k=2Ӟ说明两个偶数的最大公U数必然能被2整除 * * @param a * @param b * @return */ privatestaticint gcdByStein(int a, int b) { if (a > b) { gcdByStein(b, a); } if (b ==0) { return a; } if (a %2==0&& b %2==0) { return2* gcdByStein(a /2, b /2);//a,b都是偶数 } if (a %2==0) { return gcdByStein(a /2, b);//仅a为偶?/span> } if (b %2==0) { return gcdByStein(a, b /2);//仅b为偶?/span> } return gcdByStein((a + b) /2, (a - b) /2);//a,b都是奇数 }