Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Arrays;
- import java.util.Scanner;
- public class Main {
- static Scanner scan = new Scanner(System.in);
- public static void writeTask(){
- System.out.println("Данная программа находит все натуральные числа, которые можно представить\n" +
- "в виде произведения двух простых чисел и которые не превосходят введённое\n" +
- "натуральное число N");
- }
- public static int inputNumber () {
- int number = 0;
- final int MAX_NUMBER = 2147483647;
- final int MIN_NUMBER = 2;
- boolean isIncorrect = false;
- do {
- isIncorrect = false;
- System.out.println("Введите натуральное число в диапазоне от " + MIN_NUMBER + " до " + MAX_NUMBER);
- try {
- number = Byte.parseByte(scan.nextLine());
- }catch (Exception e) {
- System.out.println("Число введено некорректно. Повторите попытку");
- isIncorrect = true;
- }
- if ((!isIncorrect) && (number < MIN_NUMBER)) {
- System.out.println("Введённое число не соответствует диапазону");
- isIncorrect = true;
- }
- } while (isIncorrect);
- return number;
- }
- public static int[] consistencyUpToNumber (int number) {
- int[] array = new int[number - 1];
- int i = 0;
- while (i < number - 1)
- {
- array[i] = i + 2;
- i++;
- }
- return array;
- }
- public static int[] arrayOfPrimes(int number) {
- boolean[] isPrime = new boolean[number];
- Arrays.fill(isPrime, true);
- for (int i = 2; i * i < number; i++) {
- if (isPrime[i]) {
- for (int j = 2 * i; j < number; j += i) {
- isPrime[j] = false;
- }
- }
- }
- int[] primeNumbers = new int[number - 1];
- int j = 0;
- for (int i = 2; i < number; i++) {
- if (isPrime[i]) {
- primeNumbers[j] = i;
- j++;
- }
- }
- return primeNumbers;
- }
- public static int secondDivision(int arrNumbersElement, int quotient, int number)
- {
- int[] arrOfPrimes = arrayOfPrimes(number);
- int arrCorrectnessElement = 0;
- int k = 0;
- while ((k < number) && (arrOfPrimes[k] != 0) && (arrOfPrimes[k] <= quotient))
- {
- if (quotient == arrOfPrimes[k])
- {
- arrCorrectnessElement = arrNumbersElement;
- k = number;
- }
- k++;
- }
- return arrCorrectnessElement;
- }
- public static int[] findingRequiredNumbers (int number) {
- int[] arrOfPrimes = arrayOfPrimes(number);
- int[] arrNumbers = consistencyUpToNumber(number);
- number--;
- int[] arrCorrectness = new int[number];
- for (int i = 0; i < number; i++) {
- arrCorrectness[i] = 0;
- int k = 0;
- while ((k < number) && (arrOfPrimes[k] != 0) && (arrOfPrimes[k] < arrNumbers[i]))
- {
- if (arrNumbers[i] % arrOfPrimes[k] == 0)
- {
- int quotient = arrNumbers[i] / arrOfPrimes[k];
- arrCorrectness[i] = secondDivision(arrNumbers[i], quotient, number);
- k = number;
- }
- k++;
- }
- }
- return arrCorrectness;
- }
- public static void outputNumbers (int number, int[] arrCorrect) {
- System.out.println("Полученные натуральные числа:");
- int[] arrNumbers = consistencyUpToNumber(number);
- number--;
- int zeroValue = number;
- for (int i = 0; i < number; i++)
- {
- if (arrCorrect[i] != 0)
- {
- System.out.print(arrCorrect[i] + " ");
- zeroValue++;
- }
- }
- if (zeroValue == number)
- {
- System.out.println("Натуральные числа, соответствующие требованиям, не найдены");
- }
- }
- public static void main (String[] args) {
- writeTask();
- int numb = inputNumber();
- scan.close();
- int[] arrayOfCorrect = findingRequiredNumbers(numb);
- outputNumbers(numb, arrayOfCorrect);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement