1451A — Subtract or Divide.

gevaraweb · updated November 26, 2020
import java.util.*;
import java.lang.*;
import java.io.*;
import java.util.Arrays;
import java.util.Scanner;
import java.util.Random;

// The main method must be in a class named "Main".
class Main {
    
     static boolean isPrime(int n) {
        for (int i = 2; i < n; i++) {
            if (n % i == 0)
                return false;
        }
        return true;
    }

    public static int largestDivisor(int n) {
        for (int i = n / 2; i >= 2; i--) {
            if (n % i == 0) {
                return i;
            }
        }
        return 1;
    }
    
    
    public static void main(String[] args) {
      
        Scanner scanner = new Scanner(System.in);
      //  int count = scanner.nextInt();
        int count = 5;
        boolean d = false; Random rand = new Random();
        for ( int i=0; i<count; i++ ) {
            d = false;

           // int value =   scanner.nextInt();
            int value = rand.nextInt(50) + 3;
            System.out.println("value = " + value);
            int shag = 0;
            while ( value > 1 ) {
                d = isPrime(value);
                if (d) { value = value-1; }
                    else { value = value / largestDivisor( value ); }
                shag++;
            }

            System.out.println(shag);
        }
      

    }
}
Output
(Run the program to view its output)

Comments

Please sign up or log in to contribute to the discussion.