Java implementation of the continuous odd of n+2*x is a composite algorithm problem violence algorithm

  • 2020-04-01 03:29:21
  • OfStack

Six odd Numbers a,a plus 2,a plus 4,a plus 6,a plus 8,a plus 10 are composite Numbers. Find the least violent solution to a.

Write the result first, and then paste the code:


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

. We haven't worked out the rest of this

The code is as follows:


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;


public class ZhishuTest {
 
 public static boolean He(int num){
 //The 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;
 }

 
 public static void main(String[] args) {
 test();
 }
 public static void test() {
 //The start time
 long startMillis = System.currentTimeMillis();
 //Last completed time
 long preMillis = System.currentTimeMillis();
 //Completion time
 long curMillis = System.currentTimeMillis();
 //
 int lianxu = 1000;
 int start = 1;
 int times = 1;
 for (int x = 1; x <= lianxu; x++) {
  if(times > x){
  continue;//Skip to the next loop
  } else {
  times = x;
  }
  List<Map<Integer, Integer>> resList = testTimesHe(x, start, false);
  //
  //If there are Numbers, process them
  if(null == resList || resList.isEmpty()){
  //If you can't find it, there won't be another one...
  //Deep nesting is 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 sucks. Bad Java
   Set<Integer> keys= map.keySet();
   Iterator<Integer> iteratorK = keys.iterator();
   if(iteratorK.hasNext()){
   Integer key = iteratorK.next(); //The number of
   Integer value = map.get(key); //The smallest n
   //
   //Completion time
   curMillis = System.currentTimeMillis();
   //
   long allTimeout = curMillis - startMillis;
   long curTimeout = curMillis - preMillis;
   System.out.println(""+key+" consecutive n="+value +", Number of continuous values : "+size +
    "; Time consuming : " + curTimeout + "ms, A total of:  "+allTimeout+"ms");
   //Deal with the data. The greedy don't deal with it
   if(key > 0 && value > 0){
    times = key+1;
    start = value;
   }
   }
  }
  }
  //Include last completion time
  preMillis = System.currentTimeMillis();
 }
 //
 //Completion time
 curMillis = System.currentTimeMillis();
 //
 long allTimeout = curMillis - startMillis;
 long curTimeout = curMillis - preMillis;
 System.out.println(" The run is over , The next value exceeds 100 time  " +
  "; Useless time-consuming : " + curTimeout + "ms, A total of:  "+allTimeout+"ms");
 }
 
 
 
 public static List<Map<Integer, Integer>> testTimesHe(int times,int start, boolean onlyStart) {
 //
 List<Map<Integer, Integer>> resList= new ArrayList<Map<Integer, Integer>>();
 //
 //Defensive programming
 if(start < 1){
  return resList;
 }
 if(0 == start % 2){ //Not dealing with even Numbers
  return resList;
 }
 if(times < 1){
  times = 1;
 }
 //
 int result = -1;
 //
 for (int i = start; i < Integer.MAX_VALUE; i+=2) {
  //
  //Avoid counting all the time without returning
  if(onlyStart && i > start){ //Start is not satisfied, just straight
  return resList;
  }
  for (int j = 0; j < times; j++) {
  int n = i + 2*j;
  //
  if(!He(n)){
   break;//The inner exit
  }
  //
  if(j+1 == times){
   //Time is up
   result = i;
   break;//We're at the end of for
  }
  }
  //
  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; Actually this recursion can be optimized a little bit; Greedy algorithm, just add next time...
  //StartTimes, just add this parameter... Greedy recursion ?
  //One more time, starting with the number result
  int t = times +1;
  int s = result;
  List<Map<Integer, Integer>> nextList = testTimesHe(t, s, true);
  //If there are Numbers in the next layer, add to the current result
  if(null != nextList && false==nextList.isEmpty()){
   resList.addAll(nextList);
  }
  
  //
  break;//The outer exit
  }
 }
 //
 return resList;
 }
}

Note: there is still room for improvement. Welcome to revise next time


Related articles: