Question

In: Computer Science

The variable x takes the values ​​from 1 to 100, with these values, create two lists,...

The variable x takes the values ​​from 1 to 100, with these values, create two lists, one where the prime numbers are stored in one, and another where the non-prime numbers are stored, indicate and print the result

Solutions

Expert Solution

Code:

import java.util.*;  
class Main{  
    public static boolean CheckPrime(int n) {     
        for (int i = 2; i <= n / 2; i++) {    
            if ((n % i) == 0) {       //ckeck number is divisible by any previous numbers 
                return false;         //if divisible then ite not prime then return false
            }
        }
        return true;     //if number is not divisible by any previous numbers then its prime number so return true
    }
    public static void main(String args[]){ 
        
        int[] x = new int[100];                  //initialize x variable to store 1 to 100 numbers
        boolean isPrime;
        
        ArrayList<Integer> prime=new ArrayList<Integer>();   //list to store prime numbers
        ArrayList<Integer> nonprime=new ArrayList<Integer>(); //list to store non prime numbers
        for (int i = 0; i < 100; i++) {       //store 1 to 100 numbers in x variable
            x[i]=i+1;
        }
        for (int i = 0; i < 100; i++) {       //Iterate 1 to 100 numbers
            isPrime = CheckPrime(x[i]);       //call function to check number is prime or not
            if (isPrime)                      //if number is prime number
                prime.add(x[i]);              //then store in prime list
            else
                nonprime.add(x[i]);          //otherwise store in nonprime list
        }
        System.out.println("Prime numbers between 1 to 100:\n"+prime);        //print list of prime numbes
        System.out.println("\nNon-Prime numbers between 1 to 100:\n"+nonprime); //print list of nonprime numbers
    }  
}

Output:

.


Related Solutions

Consider the two dependent discrete random variables X and Y . The variable X takes values...
Consider the two dependent discrete random variables X and Y . The variable X takes values in {−1, 1} while Y takes values in {1, 2, 3}. We observe that P(Y =1|X=−1)=1/6 P(Y =2|X=−1)=1/2 P(Y =1|X=1)=1/2 P(Y =2|X=1)=1/4 P(X = 1) = 2/ 5 (a) Find the marginal probability mass function (pmf) of Y . (b) Sketch the cumulative distribution function (cdf) of Y . (c) Compute the expected value E(Y ) of Y . (d) Compute the conditional expectation...
LEVEL ONE Lists and indexing Create a list x that spans 1 to 100 using 5...
LEVEL ONE Lists and indexing Create a list x that spans 1 to 100 using 5 unit increments.                                 Hint: Use the inline for and range(). Create a list y containing the elements from x that are divisible by 2.                                 Hint: Explore the for … if … statement. Do b. in another manner. Hint: Use , e.g., array() and where() from numpy. LEVEL TWO Arrays in numpy Create an array x that contains a random set of 100 uniform(0,10)...
(2pt) A variable that takes on the values of 0 or 1 and is used to...
(2pt) A variable that takes on the values of 0 or 1 and is used to incorporate the effect of categorical variables in a regression model is called a. a constant variable b. a dummy variable c. an interaction d. None of these alternatives are correct. (1pt) The model y= β0+β1x1+β2x12+ε is called a. First-order model with one predictor variable b. Second-order model with two predictor variables c. Second-order model with one predictor variable d. None of these alternatives is...
(a) Let X be a continuous random variable which only takes on positive values on the...
(a) Let X be a continuous random variable which only takes on positive values on the interval [1, 4]. If P(X) = (√ x + √ 1 x )C 2 for all x in this interval, compute the value of C. (b) Let X be a random variable with normal distribution. Let z represent the z-score for X, and let a be a positive number. Prove that P(z < |a|) = P(z < a) + P(z > −a) − 1.
Exercise: Variance of the uniform Suppose that the random variable X takes values in the set...
Exercise: Variance of the uniform Suppose that the random variable X takes values in the set {0,2,4,6,…,2n} (the even integers between 0 and 2n, inclusive), with each value having the same probability. What is the variance of X? Hint: Consider the random variable Y=X/2 and recall that the variance of a uniform random variable on the set {0,1,…,n} is equal to n(n+2)/12. Var(X)=
Let W be a random variable that takes values 1 to 6 with equal probability. a)...
Let W be a random variable that takes values 1 to 6 with equal probability. a) Write the PMF. 
 b) Calculate the mean and variance of W and z=10 W.
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable...
In C++ Create a dynamic array of 100 integer values named myNums. Use a pointer variable (like ptr) which points to this array. Use this pointer variable to initialize the myNums array from 2 to 200 and then display the array elements. Delete the dynamic array myNums at the end. You just need to write part of the program.
Q.1: create a python function that takes two integers from the user ( pass them from...
Q.1: create a python function that takes two integers from the user ( pass them from the main program). The function performs the addition operation and returns the result. In the program, ask the user to guess the result. Then, the program compares the user input ( guess) with the returned value, and displays a message that tells if the user guessing is correct or not. --define the function here --- # the program starts here print(“ This program tests...
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1...
Is there a fourth degree polynomial that takes these values? x 1 -2 0 3 -1 7 y -2 -56 -2 4 -16 376
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function:...
STAT 150 Homework 23. Random Variable X takes integer values and has the Moment Generating Function: Mx(t)= 4/(2-e^t)  -  6/(3-e^t). 1) What is the variance of X. 2) Find the probability P(X ≤ 2).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT