Ultraviolet A 1415-Gauss prime (number theory, Gaussian prime number extension)

Source: Internet
Author: User
Ultraviolet A 1415-Gauss prime

Question Link

Given a + bi, determine whether it is a Gaussian prime number, I = SQRT (-2 ).

Train of Thought: the common Gaussian prime I = SQRT (-1), the judgment method is:
1. If A or B is 0, judge another prime number in the form of 4 * n + 3 (using the squared theorem of ferma)
2. If neither a nor B is 0, judge whether a ^ 2 + B ^ 2 is a prime number.

Then this question is extracted from SQRT (2), which is the same as the basic situation.
If both A and B are not 0, judge whether a ^ 2 + 2 B ^ 2 is a prime number.
1 is a little different. It is in the form of x ^ 2 + 2 y ^ 2. It is not applicable to the ferma sum of squares theorem. Because A and B have a maximum of 1 W, they can be enumerated completely, determine whether it can be split into the form of x ^ 2 + 2 * y ^ 2. This solves the problem.

Code:

#include <stdio.h>#include <string.h>#include <math.h>const int N = 20005;int t, a, b, prime[N], pn = 0, vis[N];bool judge() {    if (a == 0) {int m = sqrt(m);for (int i = 0; i <= m; i++) {    double t = sqrt((b - i * i) * 1.0 / 2);    if (ceil(t) == floor(t)) return true;}return false;    }    int tmp = a * a + 2 * b * b;    for (int i = 0; i < pn && prime[i] < tmp; i++)if (tmp % prime[i] == 0) return false;    return true;}int main() {    vis[1] = 1;    for (int i = 2; i < N; i++) {if (vis[i]) continue;prime[pn++] = i;for (int j = i * i; j < N; j += i) {    vis[j] = 1;}    }    scanf("%d", &t);    while (t--) {scanf("%d%d", &a, &b);printf("%s\n", judge()?"Yes":"No");    }    return 0;}


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.