Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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 = 127;
- 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 secondDivision(int arrNumbersElement, int quotient)
- {
- int[] arrOfPrimes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
- int arrCorrectnessElement = 0;
- int k = 0;
- while ((k < 18) && (arrOfPrimes[k] <= quotient))
- {
- if (quotient == arrOfPrimes[k])
- {
- arrCorrectnessElement = arrNumbersElement;
- k = 18;
- }
- else
- {
- arrCorrectnessElement = 0;
- k++;
- }
- }
- return arrCorrectnessElement;
- }
- public static int[] findingRequiredNumbers (int number) {
- int[] arrOfPrimes = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61};
- 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 < 18) && (arrOfPrimes[k] < arrNumbers[i]))
- {
- if (arrNumbers[i] % arrOfPrimes[k] == 0)
- {
- int quotient = arrNumbers[i] / arrOfPrimes[k];
- arrCorrectness[i] = secondDivision(arrNumbers[i], quotient);
- k = 18;
- }
- 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