2013 Blue Bridge Cup preliminary lucky number

Source: Internet
Author: User
Problem description
The lucky number is named by the Polish mathematician Ulam. It is generated using a "sieve method" similar to the number of primes generated

First write the natural number 1,2,3,4,5,6,.... starting from 1
1 is the first lucky number.
We start with the number 2. Delete all items that are divisible by 2 to:
1_3_5_7_9....
Tighten them up and re-order them:
1 3 5 7 9 ..... At this point, the 3 is the 2nd lucky number, and then the number of all can be divisible by 3 ordinal position to delete. Note that is the ordinal position, not the number itself can be divisible by 3!! The deletion should be 5, 11, 17, ...
At this point 7 is the 3rd lucky number, and then delete the ordinal position can be divisible by 7 (19,39,...)
The last remaining sequence is similar to the following:
1, 3, 7, 9, 13, 15, 21, 25, 31, 33, 37, 43, 49, 51, 63, 67, 69, 73, 75, 79, ...

Input format
Enter two positive integers m n, separated by spaces (M < n < 1000*1000)
Output format
The number of lucky numbers (without M and N) that the program output is between M and N.

Sample Input 1
1 20
Sample Output 1
5

Sample Input 2
30 69
Sample Output 2

8

Import java.util.ArrayList;
Import Java.util.Scanner;
public class Xingyunshu {public

	static void Main (string[] args) {
		Scanner Scanner = new Scanner (system.in);
		int m = Scanner.nextint ();
		int n = scanner.nextint ();
		int k = 1;
		int luck = 2;
		arraylist<integer> link = new arraylist<integer> ();
		for (int i = 0; i < n; i++) {
			Link.add (i + 1);
		}

		while (Luck <= link.size ()) {for

			(int j = 1; J <= Link.size (); j + +) {
				if (j% Luck = 0) {
					link.set ( j-1, 0);
				}
			}
			for (int j = 0; J < Link.size (); j + +) {
				if ((Link.get (j)) = = 0) {
					link.remove (j);
				}
			}
			Luck = Link.get (k++);
		}
		int count = 0;
		for (int i = 0; i < link.size (); i++) {

			if (link.get (i) > M &&  link.get (i) < n) {
				count++ ;
			}
		}
		System.out.println (count);
	}
}


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.