The continuous odd number (N+2*X) implemented by Java is the algorithm of the arithmetic problem of composite number _java

Source: Internet
Author: User
Tags arithmetic int size square root

6 consecutive odd-numbered a,a+2,a+4,a+6,a+8,a+10 are composite numbers, and the least-a-brute-force solution is obtained.

First the result, followed by the code:

1 consecutive n=9, number of consecutive values: 1. Time-consuming: 0ms, total: 0ms 2 consecutive n=25, number of consecutive values: 1; Time: 0ms, Total: 0ms 3 consecutive n=91, number of consecutive values: 1; Time consuming: 0ms, Total: 0ms 4 consecutive n=115, number of consecutive values: 3; When: 0ms, total: 1ms 5 consecutive n=115, number of consecutive values: 3; Time consuming: 0ms, Total: 1ms 6 consecutive n=115, number of consecutive values: 3; Time consuming: 0ms, Total: 1ms 7 consecutive n=525, number of consecutive values: 2; time consuming: 0ms, total: 1ms 8 times Continuous n=525, number of consecutive values: 2; Time: 0ms, Total: 1ms 9 consecutive n=889, number of consecutive values: 1; Time: 0ms, total: 1ms 10 consecutive n=1131, number of consecutive values: 1; Time consuming: 0ms, Total: 1ms 11 consecutive n=1329, number of consecutive values : 6; Time: 0ms, total: 1ms 12 consecutive n=1329, number of consecutive values: 6; Time: 0ms, total: 1ms 13 consecutive n=1329, number of consecutive values: 6; Time consuming: 0ms, Total: 1ms 14 consecutive n=1329, number of consecutive values: 6; Time-consuming: 0ms , total: 1ms 15 consecutive n=1329, number of consecutive values: 6; Time consuming: 0ms, Total: 1ms 16 consecutive n=1329, number of consecutive values: 6; Time consuming: 0ms, Total: 1ms 17 consecutive n=9553, number of consecutive values: 1; Time-consuming: 2ms, total: 3ms 18 Continuous n=15685, number of consecutive values: 4; Time: 1ms, total: 5ms 19 consecutive n=15685, number of consecutive values: 4; Time: 1ms, total: 5ms 20 consecutive n=15685, number of consecutive values: 4; Time: 1ms, total: 5ms 21 consecutive n=156 85, the number of consecutive values: 4, Time: 1ms, total: 5ms 22 consecutive n=19611, number of consecutive values: 4; Time: 2ms, total: 8ms 23 consecutive n=19611, number of consecutive values: 4; Time consuming: 2ms, Total: 8ms 24 consecutive n=19611, number of consecutive values : 4; Time-consuming: 2ms, total: 8ms 25 consecutive n=19611, number of consecutive values: 4; Time: 2ms, total: 8ms 26 consecutive n=31399, number of consecutive values: 10; Time consuming: 5ms, Total: 13ms 27 consecutive n=31399, number of consecutive values: 10; Time: 5ms, total: 13ms 28 consecutive n=31399, the number of consecutive values: 10, Time: 5ms, total: 13ms 29 consecutive n=31399, number of consecutive values: 10; Time: 5ms, total: 13ms 30 consecutive n=31399, number of consecutive values: 10; Time consuming: 5ms, total: 13ms 31 consecutive n=313 99, the number of consecutive values: 10, Time: 5ms, total: 13ms 32 consecutive n=31399, number of consecutive values: 10; Time: 5ms, total: 13ms 33 consecutive n=31399, number of consecutive values: 10; Time consuming: 5ms, Total: 13ms 34 consecutive n=31399 , the number of consecutive values: 10; Time: 5ms, total: 13ms 35 consecutive n=31399, number of consecutive values: 10; Time: 5ms, total: 13ms 36 consecutive n=155923, number of consecutive values: 7; Time consuming: 92ms, Total: 105ms 37 consecutive n=15592 3, the number of consecutive values: 7, Time: 92ms, total: 105ms 38 consecutive n=155923, number of consecutive values: 7; Time: 92ms, total: 105ms 39 consecutive n=155923, number of consecutive values: 7; Time consuming: 92ms, total: 105ms 40 consecutive N=1 55923, the number of consecutive values: 7, Time: 92ms, total: 105ms 41 consecutive n=155923, the number of consecutive values: 7; Time: 92ms, total: 105ms 42 consecutive n=155923, number of consecutive values: 7; Time consuming: 93ms, total: 106ms 43 times Lian Continuous n=360655, number of consecutive values: 5; Time: 243ms, Total: 349ms 44 consecutive n=360655, number of consecutive values: 5; Time: 243ms, total: 349ms 45 consecutive n=360655, number of consecutive values: 5; Time consuming: 243ms, Total: 34 9MS 46 consecutive n=360655, number of consecutive values: 5; Time: 243ms, total: 349ms 47 consecutive n=360655, number of consecutive values: 5; Time consuming: 243ms, total: 349ms 48 consecutive n=370263, number of consecutive values: 8; Time-consuming: 14ms  , total: 363ms 49 consecutive n=370263, number of consecutive values: 8; Time consuming: 14ms, Total: 363ms 50 consecutive n=370263, number of consecutive values: 8; Time consuming: 14ms, Total: 363ms 51 consecutive n=370263, number of consecutive values: 8; Time-consuming: 14ms, total: 363ms 52 consecutive n=370263, the number of consecutive values: 8, Time: 14ms, total: 363ms 53 consecutive n=370263, number of consecutive values: 8; Time: 14ms, total: 363ms 54 consecutive n=370263, number of consecutive values: 8; Time consuming: 14ms, total: 363ms 55 consecutive n= 370263, the number of consecutive values: 8, Time: 14ms, total: 363ms 56 consecutive n=492115, number of consecutive values: 1; Time: 185ms, total: 548ms 57 consecutive n=1349535, number of consecutive values: 2; time consuming: 1854ms, total: 2402 Ms 58 consecutive n=1349535, number of consecutive values: 2; time consuming: 1854ms, total: 2402ms 59 consecutive n=1357203, number of consecutive values: 7; Time consuming: 22ms, Total: 2424ms 60 consecutive n=1357203, number of consecutive values: 7; Time-consuming: 22ms, Total: 2424ms 61 consecutive n=1357203, number of consecutive values: 7; Time consuming: 22ms, Total: 2424ms 62 consecutive n=1357203, number of consecutive values: 7; Time consuming: 22ms, Total: 2424ms 63 consecutive n=1357203, Number of consecutive values: 7 time: 22ms, total: 2424ms 64 consecutive n=1357203, number of consecutive values: 7; Time: 22ms, Total: 2424ms 65 consecutive n=1357203, number of consecutive values: 7; Time consuming: 22ms, Total: 2424ms 66 consecutive n=2010735, number of consecutive values: 8; Time: 1889ms, total: 4313ms 67 consecutive n=2010735, number of consecutive values: 8; Time-consuming: 1889ms, total: 4313ms 68 consecutive n=2010735, number of consecutive values: 8; Time consuming: 1889ms, Total: 4313ms 69 consecutive n=2010735, number of consecutive values: 8; Time consuming: 1889ms, Total: 4313ms 70 consecutive n=2010735, number of consecutive values: 8; Time consuming: 1889ms, Total: 4313ms 71 consecutive n= 2010735, the number of consecutive values: 8, Time: 1889ms, total: 4313ms 72 consecutive n=2010735, number of consecutive values: 8; Time consuming: 1889ms, Total: 4313ms 73 consecutive n=2010735, number of consecutive values: 8; time-consuming: 1890ms, Total: 4314ms 74 consecutive n=4652355, number of consecutive values: 3; time consuming: 10583MS, total: 14897ms 75 consecutive n=4652355, number of consecutive values: 3; Time consuming: 10583ms, Total: 14897ms 76 consecutive n=4652355, number of consecutive values: 3; Time consuming: 10583ms, Total: 14897ms 77 consecutive n= 17051709, the number of consecutive values: 13, Time: 86082ms, total: 100979ms 78 consecutive n=17051709, the number of consecutive values: 13; Time consuming: 86082ms, Total: 100979ms 79 consecutive n=17051709, number of consecutive values: 13. Time-consuming: 86082ms, total: 100979ms 80 consecutive n=17051709, number of consecutive values: 13; Time: 86082ms, total: 100979ms 81 consecutive n=17051709, number of consecutive values: 13; Time consuming: 86082ms,
Total: 100979ms 82 consecutive n=17051709, number of consecutive values: 13; Time consuming: 86082ms, Total: 100979ms 83 consecutive n=17051709, number of consecutive values: 13; Time consuming: 86082ms, total: 100979ms 84 consecutive n=17051709, number of consecutive values: 13; Time consuming: 86082ms, Total: 100979ms 85 consecutive n=17051709, number of consecutive values: 13; Time-consuming: 86083ms, total: 100980ms 86 consecutive n=17051709, Number of consecutive values: 13 Time: 86083MS, total: 100980ms 87 consecutive n=17051709, number of consecutive values: 13; Time: 86083ms, total: 100980ms 88 consecutive n=17051709, number of consecutive values: 13; Time-consuming: 86083MS, Total: 100980ms 89 consecutive n=17051709, number of consecutive values: 13; Time consuming: 86083ms, Total: 100980ms 90 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34772ms, total: 135752ms 91 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34772ms, Total: 135752ms 92 consecutive n=20831325, number of consecutive values: 15; Time-consuming: 34772ms, total: 135752ms 93 consecutive n= 20831325, number of consecutive values: 15; Time consuming: 34772ms, Total: 135752ms 94 consecutive n=20831325, number of consecutive values: 15; Time-consuming: 34772ms, Total: 135752ms 95 consecutive n=20831325, number of consecutive values: 15; Time: 34772ms, Total: 135752ms 96 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34772ms,
Total: 135752ms 97 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34772ms, Total: 135752ms 98 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34772ms, total: 135752ms 99 consecutive n=20831325, number of consecutive values: 15; Time consuming: 34773ms, total: 135753ms 100 consecutive n=20831325, number of consecutive values: 15; Time-consuming: 34773ms, total: 135753ms 101 consecutive n= 20831325, the number of consecutive values: 15, Time: 34773ms, total: 135753ms 102 consecutive n=20831325, the number of consecutive values: 15; Time consuming: 34773ms, Total: 135753ms 103 consecutive n=20831325, number of consecutive values : 15; Time-consuming: 34773ms, total: 135753ms 104 consecutive n=20831325, number of consecutive values: 15; Time: 34773ms, Total: 135753ms 105 consecutive n=47326695, number of consecutive values: 5; time consuming: 319130ms
, total: 452155ms 106 consecutive n=47326695, number of consecutive values: 5; Time consuming: 319131ms, Total: 452156ms 107 consecutive n=47326695, number of consecutive values: 5; Time consuming: 319131ms, total: 452156ms 108 consecutive n=47326695, number of consecutive values: 5; Time consuming: 319131ms, Total: 452156ms 109 consecutive n=47326695, number of consecutive values: 5; Time-consuming: 319131ms, total: 452156ms 110 consecutive n= 122164749, the number of consecutive values: 1, Time: 1395200ms, total: 1847356ms 111 consecutive n=189695661, number of consecutive values: 6; Time consuming: 1705936ms, total: 3553292ms 112 consecutive n= 189695661, continuous value: 6; time consuming: 1705936ms, Total: 3553292ms 113 consecutive n=189695661, number of consecutive values: 6; Time consuming: 1705936ms, Total: 3553292ms 114 consecutive n=189695661, number of consecutive values: 6; Time consuming: 1705936ms, Total: 3553292ms 115 consecutive n=189695661, number of consecutive values: 6; Time-consuming: 1705936ms, Total: 3553292ms 116 consecutive n=189695661, number of consecutive values: 6; Time consuming: 1705936ms, Total: 3553292ms 117 consecutive n=191912785, number of consecutive values: 7; Time consuming: 61964ms,
Total: 3615256ms 118 consecutive n=191912785, number of consecutive values: 7; Time consuming: 61964ms, Total: 3615256ms 119 consecutive n=191912785, number of consecutive values: 7; Time consuming: 61964ms, total: 3615256ms 120 consecutive n=191912785, number of consecutive values: 7; Time consuming: 61964ms, Total: 3615256ms 121 consecutive n=191912785, number of consecutive values: 7; Time-consuming: 61964ms, total: 3615256ms 122 consecutive n= 191912785, the number of consecutive values: 7, Time: 61964ms, total: 3615256ms 123 consecutive n=191912785, number of consecutive values: 7; Time consuming: 61964ms, Total: 3615256ms 124 consecutive n=387096135,
 Number of consecutive values: 1; Time-consuming: 6650201ms, total: 10265457ms-----This time has run out, the next value exceeds 1000 times; useless time: 0ms, total: xxxxxx135395ms

。。。。。。 The results in the back haven't been figured out yet.

The code looks like this:

Package Com.test.test.zhihe;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;

Import Java.util.Set; /** * 6 consecutive odd-numbered a,a+2,a+4,a+6,a+8,a+10 are composite numbers, and the smallest A/public class Zhishutest {/** * Determines whether a number is a composite. Compared to prime number * @param num * @re
 Turn */public static Boolean he (int num) {//square root int sq = ((Double) math.sqrt (num)). Intvalue ();
  2.. sq for (int i = 2; I <= sq; i++) {int mo = num% i;
  if (0 = mo) {return true;
 }//return false;
 /** * Main function * @param args/public static void main (string[] args) {test ();
 public static void Test () {//start time long Startmillis = System.currenttimemillis ();
 Last finish time Long premillis = System.currenttimemillis ();
 This time of completion is long Curmillis = System.currenttimemillis ();
 int lianxu = 1000;
 int start = 1;
 int times = 1;
  for (int x = 1; x <= Lianxu x + +) {if (Times > x) {continue;//skipped, into the next loop} else {time = x; } List<maP<integer, integer>> reslist = Testtimeshe (x, start, false);
  If there is a number, then processing if (null = = Reslist | | reslist.isempty ()) {//not found, there will be no next ...//deep nesting it's disgusting ...
  Break
  int size = Reslist.size ();
  Traverse Iterator<map<integer, integer>> Iteratorr = Reslist.iterator ();
  while (Iteratorr.hasnext ()) {Map<integer, integer> Map = (Map<integer, integer>) Iteratorr.next ();
   if (null!= map &&!map.isempty ()) {//MAP traversal It's disgusting. Rotten Java set<integer> keys= map.keyset ();
   iterator<integer> iteratork = Keys.iterator (); if (Iteratork.hasnext ()) {integer key = Iteratork.next ();//number of Integer value = Map.get (key);//min N////The completion of this time
   Time Curmillis = System.currenttimemillis ();
   Long alltimeout = Curmillis-startmillis;
   Long curtimeout = Curmillis-premillis;
   System.out.println ("" "+key+" secondary continuous n= "+value +", number of consecutive values: "+size +"; Time consuming: "+ curtimeout +" MS, total: "+alltimeout+" MS "); Processing the data, the greedy processing will not deal with the IF (Key > 0 && value > 0) {times = key+1;
   start = value;
 }}///count in last completed time Premillis = System.currenttimemillis ();
 }///this time Curmillis = System.currenttimemillis ();
 Long alltimeout = Curmillis-startmillis;
 Long curtimeout = Curmillis-premillis;
 System.out.println ("This time has run out, the next value exceeds 100 times" + "; useless time consuming:" + curtimeout + "MS, total:" +alltimeout+ "MS"); /** * * * Test times +2 are the smallest n * @param number of Numbers * @param start number * @param onlystart only a single start value is evaluated. For recursion. External adjustment Use should be passed in * @return/public static List<map<integer, integer>> testtimeshe (int times,int start, Boolean Onlys
 Tart) {//List<map<integer, integer>> reslist= new Arraylist<map<integer, Integer>> ();
 Defensive programming if (start < 1) {return reslist;
 } if (0 = = start% 2) {//do not handle even return reslist;
 if (Times < 1) {times = 1;
 //INT result =-1;
for (int i = start; I < Integer.max_value i+=2) {////Avoid always counting not returning  if (Onlystart && i > Start) {//Start is not satisfied, return directly to Reslist;
  for (int j = 0; J < times; J +) {int n = i + 2*j; if (!
   He (n)) {break;//inner exit}//if (j+1 = times) {//ran to the results. Time is satisfied result = i;
  break;//here does not matter, ran to for the last}//if (Result > 0) {////system.out.println ("result =" +result);
  Map<integer, integer> resmap = new Hashmap<integer, integer> ();
  Resmap.put (times, result);
  Reslist.add (RESMAP); Try the next number, recursion; In fact, this recursive return can continue to optimize a little;
  Greedy algorithm, directly add next time ... Starttimes, add this parameter directly ...
  Greedy recursion?
  1 more times, starting from result this number int t = times +1;
  int s = result;
  List<map<integer, integer>> nextlist = Testtimeshe (t, S, true);
  If there is a number on the next level, add to the current result if (null!= nextlist && false==nextlist.isempty ()) {Reslist.addall (nextlist);
 }//break;//outer exit}//return reslist;

 }
}

Description: There is room for improvement, welcome to the next amendment

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.