Home>

### Continuous odd number (n + 2 * x) implemented by Java is a composite algorithm

6 consecutive odd numbers a, a + 2, a + 4, a + 6, a + 8, a + 10 are composite numbers,Find the minimum a solution to violence.

Results first,Paste the code behind:

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

. . . . . . Later results have not been calculated 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 numbers a, a + 2, a + 4, a + 6, a + 8, a + 10 are composite numbers,Find the smallest a
* /
public class zhishutest {
/**
* Determine if a number is composite.
Compared to prime
* @param num
* @return
* /
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 () {
//Starting time
long startmillis=system.currenttimemillis ();
//Last completion 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;x) {
continue;//skip and enter the next cycle
} else {
times=x;
}
list<map<integer, integer>>reslist=testtimeshe (x, start, false);
//
//if there are numbers,Processing
if (null == reslist || reslist.isempty ()) {
..
//Deep nesting is too 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 is too disgusting.
Rotten java
set<integer>keys=map.keyset ();
iterator<integer>iteratork=keys.iterator ();
if (iteratork.hasnext ()) {
integer key=iteratork.next ();//times
integer value=map.get (key);//minimum n
//
//completion time
curmillis=system.currenttimemillis ();
//
long alltimeout=curmillis-startmillis;
long curtimeout=curmillis-premillis;
system.out.println ("" + key + "consecutive n =" + value + ", number of consecutive values:" + size +
";Time-consuming:" + curtimeout + "ms, Total:" + alltimeout + "ms");
//Data processing,The greedy will not deal with it
if (key>0&&value>0) {
times=key + 1;
start=value;
}
}
}
}
//Count to last completed time
premillis=system.currenttimemillis ();
}
//
//completion time
curmillis=system.currenttimemillis ();
//
long alltimeout=curmillis-startmillis;
long curtimeout=curmillis-premillis;
system.out.println ("This time has finished,Next value exceeded 100 times "+
";Useless time-consuming:" + curtimeout + "ms, total:" + alltimeout + "ms");
}
/**
*
* Test times +2 are all the smallest n
* @param times calculation times
* @param start starting number
* @param onlystart only calculates a single start value. Used for recursion.
External calls should be passed in
* @return
* /
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) {//do not process even numbers
return reslist;
}
if (times<1) {
times=1;
}
//
int result=-1;
//
for (int i=start;i<integer.max_value;i +=2) {
//
//Avoid always calculating and not returning
if (onlystart&&i>start) {//start is not satisfied, just directly
return 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 result.
times are met
result=i;
break;//It doesn't matter if you retreat here,Run to the end of for
}
}
//
if (result>0) {
//
//system.out.println("result="+ result);
//
map<integer, integer>resmap=new hashmap<integer, integer>();
resmap.put (times, result);
//try the next number,Recursion;in fact, this recursion can continue to be optimized a little;
. .
. . Greedy Recursion?
//1 more time, starting from the result number
int t=times +1;
int s=result;
list<map<integer, integer>>nextlist=testtimeshe (t, s, true);
//If there are numbers in the next layer,Added to the current result
if (null!=nextlist&&false == nextlist.isempty ()) {