Group of prime numbers

Source: Internet
Author: User

Description

Minimum 1 ~ The number of groups in which N is divided can make the sum of the numbers in each group a prime number.

 

Input

Multiple groups of data
The first row is a number t, indicating the number of data groups.
One row of data in each group, positive integer n

 

Output

There are t rows. The minimum number of groups for each action. If the group cannot be grouped, the output is-1.

 

Sample input 12 sample output 1 godebach conjecture bare question first if sum (n) is an even number, that is, the sum of two prime numbers, writeln (2) if sum (n) is an odd number, then we will discuss the classification. If sum (n) is a prime number, 1 can be used if it is not a prime number check (sum (N)-2). If it is a prime number, it is three proof: first ~ N all add up, so we are equivalent to 1 ~ N is divided into groups. We know that any even number can be expressed as the sum of two prime numbers. If (Σ N and 1) = 0, it is the sum of two prime numbers. Otherwise, if Σ N is not an even number, we also need to discuss it by category. If the classification is, the odd number and the non-odd number, the odd number can be considered as a group, writeln (1) otherwise, for this non-prime number, we use this number to subtract 22 from the unique even prime number, any odd number greater than 7 can be expressed as the sum of the three prime numbers by mathematical induction. If 2 is not in the three prime numbers, it is larger than the other. Therefore, if the value is reduced by 2, it is not the prime number writeln (3). Otherwise, 2 is output. Miller-Rabin is used to judge the prime number. The code for greatly optimizing the constant is as follows:
{$inline on}var j,k,l,n,m,s,t:int64;    b:boolean;    i:longint;    a:array[2..6] of integer=(3,5,7,13,19); const count=10;      pri:array [0..10] of longint=(2,3,5,7,11,13,17,19,23,29,31); function multi(a,b,m:int64):int64;var ans:int64;begin  ans:=0;  a:=a mod m;  while b<>0 do    begin      if (b and 1)=1 then        begin          ans:=(ans+a) mod m;          dec(b);        end;      b:=b>>1;      a:=(a+a) mod m;    end;  exit(ans);end; function gcd(x,y:int64):int64;begin  if x mod y=0 then    exit(y)  else exit(gcd(y,x mod y));end; function quick_mod(a,b,m:int64):int64;var ans:int64;begin        ans:=1; a:=a mod m;        while b<>0 do                begin                        if (b and 1)=1 then                                begin                                        ans:=multi(ans,a,m);                                        dec(b);                                end;                        b:=b>>1;                        a:=multi(a,a,m);                end;        exit(ans);end; function prime(n:int64):boolean;var m,k,a,x,y:int64; i,j:longint;begin  if n=2 then exit(true);  if (n<2) or ((n and 1)=0) then exit(false);  m:=n-1; k:=0;  while (m and 1)=0 do        begin                inc(k);                m:=m>>1;        end;  randomize;  for i:=0 to count do        begin          a:=random(n) mod (n-1)+1;          x:=quick_mod(a,m,n);          y:=0;          for j:=0 to k-1 do                begin                        y:=multi(x,x,n);                        if (y=1) and (x<>1) and (x<>n-1) then exit(false);                        x:=y;                end;          if y<>1 then exit(false);        end;  exit(true);end;  procedure start; inline;var s:int64;begin  s:=n*(n+1) shr 1;  if n>=7 then  if (s and 1)=0 then    writeln(‘2‘)  else  if (prime(s)) then    writeln(‘-1‘)  else  if prime(s-2) then    writeln(‘2‘)  else    writeln(‘3‘);  if (n<7) then  begin    if odd(n) then s:=n*(n shr 1+1) else s:=(n+1)*(n shr 1);    m:=s-a[n];    if m=0 then writeln(‘1‘) else writeln(‘2‘);  end;end;procedure main; inline;begin  read(t);  for i:=1 to t do  begin    read(n);    if (n=0)or(n=1) then writeln(‘-1‘)    else start;  end;end;begin      main;end. 
View code

 

Group of prime numbers

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.