Finding whether number 4 is prime or not? [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
Here is my program, inside for loop Math.sqrt(n) - I think here is error.
import java.util.Scanner;
public class PrimeNoCheck {
public static void main(String[] args) {
Scanner s = new Scanner(System.in);
System.out.print("Enter a number : ");
int n = s.nextInt();
if (isPrime(n)) {
System.out.println(n + " is a prime number");
} else {
System.out.println(n + " is not a prime number");
}
}
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
}
for (int i = 2; i < Math.sqrt(n); i++) {
if (n % i == 0) {
return false;
}
}
return true;
}
}

the algorithm is:
To check if n is a prime number, you have to try to divide it by all the numbers, from 2 (1 is useless), to sqrt(n), included.
You don't have to check after sqrt(n), because, if n can be divided by X,
greater than sqrt(n), it also can be divided by Y (n/X), lesser than sqrt(n), or by Z, lesser than Y, if Y is not prime.
The error in you code is that you stop just before sqrt(n).
With your code 4, 9, 25, ... are seen as prime, and every prime ^2 also.
just Replace that: <= instead of <
for (int i = 2; i <= Math.sqrt(n); i++) {

Related

What should I add or remove on my factorial calculator code? [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 years ago.
Improve this question
I'm new to coding, so I've been getting confused on what I should add or remove on my code. When I entered the factorial of 2, it also prints out the factorial 1, when I only want the factorial of 2 to print out.
import java.util.Scanner;
public class FactorialCalculator {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int i, fact = 1, number;
System.out.println("<------ Factorial Calculator ------>");
System.out.print("Enter a positive integer: ");
number = input.nextInt();
if (number <= 2) {
for (i = 1; i<=number; i++) {
fact = fact * i;
System.out.println("2! = 1 x 2 ");
System.out.println("The Factorial of 2 is: "+fact);
}
}
}
}
output:
<------ Factorial Calculator ------>
Enter a positive integer: 2
The Factorial of 2 is: 1
The Factorial of 2 is: 2
Move your print statement out of the loop.
if (number <= 2) {
for (i = 1; i<=number; i++) {
fact = fact * i;
}
System.out.println("The Factorial of 2 is: "+fact);
}
But you may want to allow larger values than just 2. And don't forget that both 0! and 1! are equal to 1.
Try it like this.
int fact = 1; //starting point for all factorials.
if (number >= 2) {
for (int i = 2; i <= number; i++) {
fact = fact * i;
}
}
System.out.println(number + "! = " + fact);
I think you were confusing <= 2 vs >= 2. <= 2 would only allow values 2, 1, 0, -1, etc to enter the loop. What you wanted was to allow values >= 2 or 2, 3, 4, ...

Why 1534236469 can't pass [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I think 1534236469 out of range! Leetcode: 7. Reverse Integer
I can't pass the test input 1534236469. why? the return range is[Integer.MAX_VALUE, Integer.MIN_VALUE],others should return zero
class Solution {
public int reverse(int x) {
if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE)
return 0;
int ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
return ans;
}
}
Thank for your help
The reverse of 1534236469 is 9646324351, which is larger than Integer.MAX_VALUE, so your code will result in numeric overflow and an incorrect result.
You can use long instead of int to fix the problem.
EDIT:
Your added if(x > Integer.MAX_VALUE || x < Integer.MIN_VALUE) condition is pointless, since x is int, so it will never be outside the valid range of ints.
Even if x is within the valid range, the reverse of x may be outside the range. If you want to detect that the reversed x is too large and return 0, you should use long internally:
class Solution {
public int reverse(int x) {
long ans = 0;
while(x != 0) {
ans = ans * 10 + (x % 10);
x /= 10;
}
if(ans > Integer.MAX_VALUE || ans < Integer.MIN_VALUE) {
return 0;
} else {
return (int) ans;
}
}
}

Understanding code to find the largest prime factor of a number [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
I found this code on an old youtube video and am having the hardest time understanding the logical sequence for the output. I was hoping any of you can help clarify.
Let's assume I pass a value of 21 to the getLargestPrime method.
In line 17 the conditional statement checks whether 21 % 2 != 0, that's True. So it carries another iteration of the code. But this time m = 3, which 21 % 3 != 0, that's actually False. Why is the code still executing the else statement? This is question #1.
Question #2
How can the if statement inside the else ever execute? When does number become == 1?
I know that this is probably super basic for you guys but for some reason I cannot follow the sequence in the output.
public class LargestPrime {
public static int getLargestPrime(int number){
if(number < 0){
return -1;
}
int m = 2;
int ans = 0;
int numbern;
if(number == 1){
System.out.println("This number is not a prime");
} else{
while (ans == 0){
if(number % m !=0){
m = m +1;
System.out.println(m + " value of m");
} else {
numbern = number;
number = number / m;
if(number == 1){
System.out.println(numbern + " is the largest prime factor of your number");
ans++;
}
}
}
} return number;
}
}
This is the output:
3 value of m
4 value of m
5 value of m
6 value of m
7 value of m
7 is the largest prime factor of your number
1
21%3!=0 returns false. Because of this the jvm executes the code after else. In e.g. 21%2!=0, that is true, it executes the code after if.
It is 1, if number is as m.
This is when your calculation gets to an end because there cannot be any more numbers after your number.

How can I check for prime numbers? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
What is the simplest way to do this, preferably without lists or maps?
I've tried turning the wikipedia pseudocode into real code but I don't understand the second half after the new line at all.
function is_prime(n : integer)
if n ≤ 1
return false
else if n ≤ 3
return true
else if n mod 2 = 0 or n mod 3 = 0
return false
let i ← 5
while i×i ≤ n
if n mod i = 0 or n mod (i + 2) = 0
return false
i ← i + 6
return true
First off, here's that function converted to java:
public static boolean isPrime(int n) {
if (n <= 1) {
return false;
} else if (n <= 3) {
return true;
} else if (n % 2 == 0 || n % 3 == 0) {
return false;
}
int i = 5;
while (i * i <= n) {
if (n % i == 0 || n % (i + 2) == 0) {
return false;
}
i += 6;
}
return true;
}
The second half is the part that iterates from the lowest unchecked number (5, because multiples of 2 are already checked, as are 0, 1, and 3) to the square root of n (anything greater than the square root would have to be multiplied by something less than the square root to equal n), checking each number i to see if n is evenly divisible by i.
try this code
public static boolean isPrime(int n) {
for (int i = 2; i < n - 1; i++) {
if(n%i==0)
return false;
}
return true;
}
public static void main(String[] args) {
System.out.println(isPrime(39));
}

Multiply the digits at odd position by 1 and at even position by 3 [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 9 years ago.
Improve this question
I am doing a question and it wants me to get 12 digits to multiply. If the first digit is odd, it has to multiply by 1 otherwise the digit multiply by 3.My program is working. However, the coding is quite long. May I ask if there is any other method?
import java.util.Scanner;
public class ass2a
{
public static void main(String []args)
{
Scanner reader = new Scanner(System.in);
String input,b;
long checked;
System.out.print("Please enter the 12 digit:");
input = reader.nextLine();
long one,two,three,four,five,six,seven,eight,nine,ten,eleven,twevle;
checked =Long.parseLong(input);
one= checked / 100000000000L;
two = (checked % 100000000000L)/10000000000L;
three =(checked % 10000000000L)/ 1000000000L;
System.out.println(one);
System.out.println(two);
System.out.println(three);
if((input.length() < 12) || (input.length() > 12))
{
System.out.println("The entered digits is not equal to 12.");
System.exit(0);
}
else
{
System.out.println("Calculating...");
}
if (one % 2 ==0)
{
one = one * 1;
}
else
{
one = one * 3;
System.out.println("one is "+one);
}
if (two % 2 ==0)
{
two = two * 1;
}
else
{
two = two * 3;
System.out.println("two is "+two);
}
if (three % 2 ==0)
{
three = three * 1;
}
else
{
three = three * 3;
System.out.println("three is "+three);
}
System.out.println(one);
System.out.println(two);
System.out.println(three);
long sum = one +two+three;
System.out.println(sum);
}
}
Instead of creating individual variables, you could store the values in an array with length 12 and iterate that array:
long[] digits = new long[12];
// Add the input to the array
...
for(int i=0;i<digits.length;i++){
if(digits[i]%2==0){
digits[i] *= 3;
}
}
This is not complete code, but just an example to get you started. You can read up on Java arrays.

Categories

Resources