Practical Java language design-generates a random integer between 2-K to determine whether a number is a prime number

Source: Internet
Author: User

1                     import java.io.*; 
2                        import java.util.*;
3                          public class J2_13 { 
4                            public static void main(String a[]) {
5. Construct objects = new objects (system. In);/* construct a struct object. The input parameter is system. In */
6                          System.out.println("Please enter a number to limit the random number that yout want:");
7 int K = bytes. nextint ();/* read an int value K */
8 int P = (INT) (2 + (K-2) * Math. Random ();/* generates a random integer between 2 and K */
9                        if(yes(p)) 
10                     System.out.println(p+" is prime。");  
11                   else 
12                   System.out.println(p+" is not prime。"); 
13                    return; 
14                   } 
15/* determine whether a number is a prime number */
16                   public static boolean yes(int q) { 
17                        boolean t=true; 
18                           for(int i=2;i<q;i++) 
19                               if(q%i==0) { 
20                          t=false; 
21                    break; 
22                    } 
23 return t;/* return a Boolean value */
24                   } 
25       }

 

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.