Problem of prime number of the US prime number in HDU 4548, hdu4548

Source: Internet
Author: User

Problem of prime number of the US prime number in HDU 4548, hdu4548

This question can be used to directly create tables. It also requires skills to determine whether tables can be used:

1. The maximum value is 1000000. Tables of tables smaller than one million can be queried.

2. Linear preprocessing is possible, and linear preprocessing can be achieved by using the prime screening method.

Therefore, you can create a table.


Basic knowledge points to be familiar:

1 Prime Number sieve-write code within one or two minutes

2. The prime number embedding method is generally used to determine the prime number because the value after the number of digits is added is very small. If you write a primality test algorithm, it will be useless.

3. Then, the idea of dynamic planning with dots overwrites the preceding number of elements to facilitate searching.


It's a basic question. Some people also talk about the water question. I 'd like to call it a basic question.

The difficulty lies in the judgment, and the use of these basic knowledge, simple and elegant solution, write code.

#include <stdio.h>#include <string.h>const int MAX_N = 1000001;bool isPrime(int n){if (n == 2) return true;for (int r = 2; r * r <= n; r++){if (n % r == 0) return false;}return true;}bool isMeiPrime(int n){int d = 0;while (n){d += n % 10;n /= 10;}return isPrime(d);}int primeNums[MAX_N];bool primes[MAX_N];void seive(){memset(primes, 0, MAX_N * sizeof(bool));for (int i = 2; i < MAX_N; i++){if (!primes[i]){for (int j = i << 1; j < MAX_N; j += i){primes[j] = true;}}}primeNums[0] = 0, primeNums[1] = 0;for (int i = 2; i < MAX_N; i++){if (!primes[i] && isMeiPrime(i)) primeNums[i] = primeNums[i-1] + 1;else primeNums[i] = primeNums[i-1];}}int main(){seive();int T, a, b;scanf("%d", &T);for (int t = 1; t <= T; t++){scanf("%d %d", &a, &b);printf("Case #%d: %d\n", t, primeNums[b] - primeNums[a-1]);}return 0;}




Help me with the following vb questions: 1. Compile a function to judge the prime number, and use inputbox to input data when you click an event on the single-choice button.

Function isprime (m as integer) as boolean
Dim I as integer
Isprime = true
For I = 2 to sqr (m)
If m mod I =-0 then
Isprime = false
Exit
_ End if ____
_ Next I _____
End function
Private sub commandementclick ()
Dim n as integer
N =__ inputbox ("Enter n value ")_
If _ isprime (n) _ then
Print n; "is a prime number"
Else
Print n; "not a prime number" 'Please complete this section by yourself
End if
End sub
2. open app. path & "\ a2.txt" for input as #1
Do while not eof (1)
Lineinput #1,
Str1 = str1 &
Loop
Open app. path & "a1.txt" for append as #2
Print #2, str1
Close

Teach programming, the question should be a prime number of less than 1000 in C ++

# Include <iostream. h>
Void main ()
{
Int a, I, leap;
Cout <"prime numbers less than 1000 :";
For (a = 1; a <= 1000; a ++)
{
Leap = 1; // each time you re-determine whether a number is a prime number, the leap must be reset to 1.
For (I = 2; I <a; I ++)
If (a % I = 0) leap = 0;
If (leap) cout <a <",";
}
}

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.