Largest prime factor - java

I have a little problem with my java code! The question is: The prime factors of 13195 are 5, 7, 13 and 29.
What is the largest prime factor of the number 600851475143 ?
My code somehow doesn't work!! What's wrong? Thanks for your help!
public class Example_1 {
public static void main (String[] args){
{
System.out.println(largestPrimeFactor(600851475143));
}
}
private static long largestPrimeFactor(long number) {
long result = 0;
for(long x = 2;x<number;x++){
if(number % x == 0){
for( long y = 2; y < x ; y++ ){
if( x % y == 0){
break;
}
else{
result = x;
}
}
}
}
return result;
}
}

for( long y = 2; y < x ; y++ ){
if( x % y == 0){
break;
}
else{
result = x;
}
}
Here you are trying to test whether x is prime. However, if you follow this loop logic through, you will see that it translates to the following:
If any y is found not to be a factor of x before it is discovered whether or not x is prime, then x is prime.
Refactor the else to outside the loop.
boolean xIsPrime = true;
for( long y = 2; y < x ; y++ ){
if( x % y == 0){
xIsPrime = false;
break;
}
}
if( xIsPrime ){
result = x;
}
Compilation problems:
The method largestPrimeFactor appears to be outside of a class.
The constant 600851475143 is too large for an int. Postfix with L: 600851475143L.
Note that the algorithm you've written is suboptimal and that is why it may run for a long time when given large inputs.

public class Example_1 {
public static void main (String[] args){
System.out.println(largestPrimeFactor(600851475143L));
}
private static long largestPrimeFactor(long number) {
long result = 0;
if (number % 2 == 0){
result = 2;
while (number % 2 == 0)
number /= 2;
}
for(long x = 3; number > 1; x += 2)
if (number % x == 0){
result = x;
while (number % x == 0)
number /= x;
}
return result;
}
}

Related

Java C double the value of the every even position of the value and sum up issue

I want to ask about what mistake I had make because i want to sum up the value in the odd position into Sumlast variable and the sum of even position value into Sumlastwo variable.
However I am required to double up the every value in even position then separate them into 2 digit like 9X2 = 18 ---> 1+8
For the odd value have no issue but when it reach the even position there is some issue.
Example I had did the input : 81
Output:
7
1
But when I type more digit like :9181
it become output:
27
2
it suppose to be (9X2) , (8X2) --> 18 , 16 = 1+8+1+6 = 16
output:
16
2
public static void main(String[] args) {
int c = 1;
int Sumlast = 0;
int Sumlasttwo = 0;
int numeven = 0;
Scanner myscanner = new Scanner(System.in);
System.out.print("Please enter your 8 digit number credit card: ");
String num = myscanner.nextLine();
if(num.length() != 8) //check the number of digit is 8
{
int test = Integer.parseInt(num);
while(test != 0)
{
if(c%2 == 0) //even
{
numeven = test * 2;
while(numeven > 0)
{
Sumlasttwo += (numeven%10);
numeven /= 10;
}
}
else //odd
{
Sumlast += test%10;
}
test /= 10;
c++;
}
System.out.println(Sumlasttwo);
System.out.println(Sumlast);
}
}
You forgot to take the digit from test in the even-case.
int test = Integer.parseInt(num);
while (test != 0)
{
int digit = test % 10;
if (c % 2 == 0) //even
{
int numeven = digit * 2;
while (numeven > 0)
{
Sumlasttwo += numeven % 10;
numeven /= 10;
}
}
else //odd
{
Sumlast += digit;
}
test /= 10;
c++;
}
Also it is more readable to declare numeven as near to its usage as possible.
Sumlast in java conventionally is written sumLast. Also } else { and ...) { is such a convention, but for instance not in C, and not as holy as camel-case names.
Debugging would have helped.
Using a help method would have made the code better:
boolean even = true;
while (test != 0) {
int digit = test % 10;
if (even) {
sumEven += digitsSum(digit * 2);
} else {
sumOdd += digit;
}
test /= 10;
even = !even;
}
private static void digitSum(int n) {
int sum = 0;
while (n > 0) {
sum += n % 10;
n /= 10;
}
return sum;
}

Finding the smith number between given range

I will get to the point quickly. Basically smith numbers are: Composite number the sum of whose digits is the sum of the digits of its prime factors (excluding 1). (The primes are excluded since they trivially satisfy this condition). One example of a Smith number is the beast number 666=2·3·3·37, since 6+6+6=2+3+3+(3+7)=18.
what i've tried:
In a for loop first i get the sum of the current number's(i) digits
In same loop i try to get the sum of the number's prime factors digits.
I've made another method to check if current number that is going to proccessed in for loop is prime or not,if its prime it will be excluded
But my code is seems to not working can you guys help out?
public static void main(String[] args) {
smithInrange(1, 50);
}
public static void smithInrange(int start_val, int end_val) {
for (int i = start_val; i < end_val; i++) {
if(!isPrime(i)) { //since we banned prime numbers from this process i don't include them
int for_digit_sum = i, digit = 0, digit_sum = 0, for_factor_purpose = i, smith_sum = 0;
int first = 0, second = 0, last = 0;
// System.out.println("current number is" + i);
while (for_digit_sum > 0) { // in this while loop i get the sum of current number's digits
digit = for_digit_sum % 10;
digit_sum += digit;
for_digit_sum /= 10;
}
// System.out.println("digit sum is"+digit_sum);
while (for_factor_purpose % 2 == 0) { // i divide the current number to 2 until it became an odd number
first += 2;
for_factor_purpose /= 2;
}
// System.out.println("the first sum is " + first);
for (int j = 3; j < Math.sqrt(for_factor_purpose); j += 2) {
while (for_factor_purpose % j == 0) { // this while loop is for getting the digit sum of every prime
// factor that j has
int inner_digit = 0, inner_temp = j, inner_digit_sum = 0;
while (inner_temp > 0) {
inner_digit = inner_temp % 10;
second += inner_digit;
inner_temp /= 10;
}
// System.out.println("the second sum is " + second);
for_factor_purpose /= j;
}
}
int last_temp = for_factor_purpose, last_digit = 0, last_digit_sum = 0;
if (for_factor_purpose > 2) {
while (last_temp > 0) {
last_digit = last_temp % 10;
last += last_digit;
last_temp /= 10;
}
// System.out.println("last is " + last);
}
smith_sum = first + second + last;
// System.out.println("smith num is "+ smith_sum);
// System.out.println(smith_sum);
if (smith_sum == digit_sum) {
System.out.println("the num founded is" + i);
}
}
}
}
public static boolean isPrime(int i) {
int sqrt = (int) Math.sqrt(i) + 1;
for (int k = 2; k < sqrt; k++) {
if (i % k == 0) {
// number is perfectly divisible - no prime
return false;
}
}
return true;
}
the output is:
the num founded is4
the num founded is9
the num founded is22
the num founded is25
the num founded is27
the num founded is49
how ever the smith number between this range(1 and 50) are:
4, 22 and 27
edit:I_ve found the problem which is :
Math.sqrt(for_factor_purpose) it seems i should add 1 to it to eliminate square numbers. Thanks to you guys i've see sthe solution on other perspectives.
Keep coding!
Main loop for printing Smith numbers.
for (int i = 3; i < 10000; i++) {
if (isSmith(i)) {
System.out.println(i + " is a Smith number.");
}
}
The test method to determine if the supplied number is a Smith number. The list of primes is only increased if the last prime is smaller in magnitude than the number under test.
static boolean isSmith(int v) {
int sum = 0;
int save = v;
int lastPrime = primes.get(primes.size() - 1);
if (lastPrime < v) {
genPrimes(v);
}
outer:
for (int p : primes) {
while (save > 1) {
if (save % p != 0) {
continue outer;
}
sum += sumOfDigits(p);
save /= p;
}
break;
}
return sum == sumOfDigits(v) && !primes.contains(v);
}
Helper method to sum the digits of a number.
static int sumOfDigits(int i) {
return String.valueOf(i).chars().map(c -> c - '0').sum();
}
And the prime generator. It uses the list as it is created to determine if a given
number is a prime.
static List<Integer> primes = new ArrayList<>(List.of(2, 3));
static void genPrimes(int max) {
int next = primes.get(primes.size() - 1);
outer:
while (next <= max) {
next += 2;
for (int p : primes) {
if (next % p == 0) {
continue outer;
}
if (p * p > next) {
break;
}
}
primes.add(next);
}
}
}
I do not want to spoil the answer finding, but just some simpler code snippets,
making everything simpler, and more readable.
public boolean isSmith(int a) {
if (a < 2) return false;
int factor = findDivisor(a);
if (factor == a) return false;
int sum = digitSum(a);
// loop:
a /= factor;
sum -= digitSum(factor);
...
}
boolean isPrime(int a){
for(int i = 2; i*i <= a; i++) {
if (a % i == 0) {
return false;
}
}
return true;
}
int findDivisor(int a){
for(int i = 2; i*i <= a; i++) {
if (a % i == 0) {
return i;
}
}
return a;
}
int digitSum(int a) {
if (a < 10) {
return a;
}
int digit = a % 10;
int rest = a / 10;
return digit + digitSum(rest);
}
As you see integer division 23 / 10 == 2, and modulo (remainder) %: 23 % 10 == 3 can simplify things.
Instead of isPrime, finding factor(s) is more logical. In fact the best solution is not using findDivisor, but immediately find all factors
int factorsSum = 0;
int factorsCount = 0;
for(int i = 2; i*i <= a; i++) {
while (a % i == 0) {
factorsSum += digitSum(i);
a /= i;
factorsCount++;
}
}
// The remaining factor >= sqrt(original a) must be a prime.
// (It cannot contain smaller factors.)
factorsSum += digitSum(a);
factorsCount++;
Here is the code. If you need further help, please let me know. The code is pretty self explanatory and a decent bit was taken from your code but if you need me to explain it let me know.
In short, I created methods to check if a number is a smith number and then checked each int in the range.
import java.util.*;
public class MyClass {
public static void main(String args[]) {
System.out.println(smithInRange)
}
public int factor;
public boolean smithInRange(int a, int b){
for (int i=Math.min(a,b);i<=Math.max(a,b);i++) if(isSmith(i)) return true;
return false;
}
public boolean isSmith(int a){
if(a<2) return false;
if(isPrime(a)) return false;
int digits=0;
int factors=0;
String x=a+¨" ";
for(int i=0;i<x.length()-1;i++) digits+= Integer.parseInt(x.substring(i,i+1));
ArrayList<Integer> pF = new ArrayList<Integer>();
pF.add(a);
while(!aIsPrime(pF)){
int num = pF.get(pF.size-1)
pF.remove(pF.size()-1);
pF.add(factor);
pF.add(num/factor)
}
for(int i: pF){
if((factors+"").length()==1)factors+= i;
else{
String ss= i+" ";
int nums=0;
for(int j=0;j<ss.length()-1;j++){
nums+=Integer.parseInt(ss.substring(j,j+1));
}
}
}
return (factors==digits);
}
public boolean isPrime(int a){
for(int i=2;i<=(int)Math.sqrt(a),i++){
String s = (double)a/(double)i+"";
if(s.substring(s.length()-2).equals(".0")){
return false;
factor = i;
}
}
return true;
}
public boolean aIsPrime(ArrayList<int> a){
for(int i: a) if (!isPrime(a)) return false;
return true;
}
}

How do I assign array values with a while loop ?

I'm trying to make a while loop that iterates through every long number possible and add every prime number it encounters into an the primes array. Since the while loop is supposed to run until the length of primes is 200, I expect the primes array to be filled with the first 200 prime numbers. Instead I get all zeroes. I have successfully gotten 20 rows of 10 characters each with a space in between them. How may I get them to be the actual prime numbers though?
public class PrimeGenerator {
public static void main(String[] args) {
long primes[] = new long[200];
while (primes.length > 200){
for(long y = 2; y < Long.MAX_VALUE; y++) {
int primeCounter = 0;
if (isPrime(y) == true){
primes[primeCounter] = y;
primeCounter++;
}
}
}
for (int i = 0; i < 20; i++) {
int primeCounter = 0;
for(int p = 0; p < 10; p++) {
System.out.print(primes[primeCounter] + " ");
primeCounter++;
}
System.out.println();
}
}
public static boolean isPrime(long number) {
if (number % 2 == 0)
return false;
if (number == 2)
return true;
for(int x = 3; x*x <= number; x+=2) {
if (number%x == 0)
return false;
}
return true;
}
}
primes.length is always 200 so the while loop is never entered.
The while loop is useless. Just add a condition to the for loop that would exit when the entire array has been assigned. Also move the initialization of primeCounter to be outside the for loop. Otherwise all the primes will be assigned to primes[0].
long primes[] = new long[200];
int primeCounter = 0;
for(long y = 2; y < Long.MAX_VALUE && primeCounter < 200; y++) {
if (isPrime(y) == true){
primes[primeCounter] = y;
primeCounter++;
}
}
for (int i = 0; i < primes.length; i++) {
System.out.print(primes[i]);
if ((i+1) % 10 == 0)
System.out.println();
}
EDIT :
As Sweeper commented, you should also fix your isPrime method, since it returns false for 2 :
public static boolean isPrime(long number) {
if (number == 2)
return true;
if (number % 2 == 0)
return false;
for(int x = 3; x*x <= number; x+=2) {
if (number%x == 0)
return false;
}
return true;
}
this code down
long primes[] = new long[200];
while (primes.length > 200){
means
while (200 > 200){
or the same as
while (false){
so your loop is NEVER executed!
because you did:
while (primes.length > 200)
and the length of the array is always 200,you never get into the while loop , and the zero in the array are coming because when you create array of "long" it initialized him with zeros
Firstly, the length of an array doesn't change. So, when you are testing for primes.length > 200 this will always be false, and the loop is never even entered. Therefore all values in the array are left at the default value of 0.
For doing this I would doing something like the following:
int primeCounter = 0;
long current = 0L;
while(primeCounter < primes.length){
if(isPrime(current)){
primes[primeCounter] = current;
primeCounter++;
}
current++;
}
An array's length never changes. If you declared an array to have a length of 200, it will always have a length of 200. Because of this, your while loop is never executed, not even once.
There are a lot of other errors in the code, so I tried to create a solution with as few changes as possible:
public static void main(String[] args) {
int primeCounter = 0;
long nextPossiblePrime = 2;
long primes[] = new long[200];
while (primeCounter < 200) {
for (long y = nextPossiblePrime; y < Long.MAX_VALUE; y++) {
if (isPrime(y) == true) {
primes[primeCounter] = y;
primeCounter++;
nextPossiblePrime = y + 1;
break;
}
}
}
primeCounter = 0;
for (int i = 0; i < 20; i++) {
for (int p = 0; p < 10; p++) {
System.out.print(primes[primeCounter] + " ");
primeCounter++;
}
System.out.println();
}
}
public static boolean isPrime(long number) {
if (number == 2 || number == 3)
return true;
if (number % 2 == 0)
return false;
for (int x = 3; x * x <= number; x += 2) {
if (number % x == 0)
return false;
}
return true;
}
The first problem is that you created two primeCounters, which is not needed. I removed the extra one and moved the scope of it to the scope of the method. The next problem is that your first for loop doesn't remember the prime number that it is on and it doesn't stop when it has found one so it will keep adding the 200th prime to the array. I fixed this by adding a nextPossiblePrime variable that stores what number should the program check next. The last problem is that your isPrime method is written incorrectly. I fixed it for you!
Here's another (cleaner) solution, which still uses a while loop:
public static void main(String[] args) {
ArrayList<Long> primes = new ArrayList<>();
long y = 2;
while (y < Long.MAX_VALUE && primes.size() < 200) {
if (isPrime(y) == true){
primes.add(y);
}
y++;
}
for (int i = 0; i < primes.size(); i++) {
System.out.print(primes.get(i) + " ");
if ((i+1) % 10 == 0)
System.out.println();
}
}
public static boolean isPrime(long number) {
if (number == 2 || number == 3)
return true;
if (number % 2 == 0)
return false;
for (int x = 3; x * x <= number; x += 2) {
if (number % x == 0)
return false;
}
return true;
}

Why is my method repeating when I run

public class PalindromicPrimes {
public static void main (String[] args) {
userInt();
System.out.println("The palindromic primes less than " + userInt() +
" are:");
for (int i = 0; i <= userInt(); i++) {
if (isPrime() && isPalindrome()) {
System.out.println(i);
}
}
}
private static boolean isPrime() {
if (userInt() == 2 || userInt() == 3) {
return true;
}
if (userInt() % 2 == 0) {
return false;
}
int sqrt = (int) Math.sqrt(userInt()) + 1;
for (int i = 3; i < sqrt; i += 2) {
if (userInt() % i == 0) {
return false;
}
}
return true;
}
private static boolean isPalindrome() {
if (userInt() < 0)
return false;
int div = 1;
while (userInt() / div >= 10) {
div *= 10;
}
while (userInt() != 0) {
int x = userInt();
int l = x / div;
int r = x % 10;
if (l != r)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
private static int userInt() {
Scanner s = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int userInt = s.nextInt();
return userInt;
}
}
is there a different way of getting the user input? or can I keep it this way?
when it runs it just keeps prompting the user input.
rearrange it like this:
public static void main (String[] args) {
//get it and save it here!
int userValue = userInt();
System.out.println("The palindromic primes less than " + userValue +
" are:");
for (int i = 0; i <= userValue; i++) {
if (isPrime(userValue) && isPalindrome(userValue)) {
System.out.println(i);
}
}
}
then also update all the methods that care about this "userInt" value.
Every time you call userInt() you're telling the code to get a new value from the command line.
Try this:
public static void main (String[] args) {
int value = userInt();
System.out.println("The palindromic primes less than " + value +
" are:");
for (int i = 0; i <= value; i++) {
if (isPrime() && isPalindrome()) {
System.out.println(i);
}
}
}
The term userInt() is a function invocation that prompts the user for input. Odds are you only want to do this once. You're doing it multiple times.
You should store the result of userInt() in a variable.
int typed = userInt();
And then use this variable to reference what the user typed instead of calling userInt() again.
System.out.println("The palindromic primes less than " + typed +
" are:");
for(int i = 0; i < typed; i++) ...
You keep calling userInt(). That is the problem.
I don't understand your logic. So I have not modified that code. But the code runs.
import java.util.Scanner;
public class PalindromicPrimes {
public static void main (String[] args) {
int x = userInt();
System.out.println("The palindromic primes less than " + x +
" are:");
for (int i = 0; i <= x; i++) {
if (isPrime(i) && isPalindrome(i)) {
System.out.println(i);
}
}
}
private static boolean isPrime(int a) {
if (a == 2 || a == 3) {
return true;
}
if (a % 2 == 0) {
return false;
}
int sqrt = (int) Math.sqrt(a) + 1;
for (int i = 3; i < sqrt; i += 2) {
if (a % i == 0) {
return false;
}
}
return true;
}
private static boolean isPalindrome(int a) {
if (a < 0)
return false;
int div = 1;
while (a / div >= 10) {
div *= 10;
}
while (a != 0) {
int x = a;
int l = x / div;
int r = x % 10;
if (l != r)
return false;
x = (x % div) / 10;
div /= 100;
}
return true;
}
private static int userInt() {
Scanner s = new Scanner(System.in);
System.out.print("Enter a positive integer: ");
int userInteger = s.nextInt();
return userInteger;
}
}
Remember, don't use the same names for variable and function. In the function userInt(), you have used a variable int userInt, to get the result from the scanner. This might be aa recursive call sometimes. Be careful with that.

Java several conditions with the help of for loop

I wonder if it is possible to have minimal code for this:
for (int x = 1; x < 10; x++){
/*I want to replace this condition with (x%number == 0)
instead of writing out condition for every number, but
it did not work with for (int number = 1; number <= 3; number++)
in (x%number == 0), as it prints out every x and number
*/
if ((x%1) == 0 && (x%2) == 0 & (x%3) == 0){
System.out.println(success!);
}
}
I think
x % a == 0 && x % b == 0 && x % c == 0
is equalent to
x % (a * b * c) == 0
UPDATE
Multiplication is incorrect, you need to use LCM: x % lcm(a, b, c)
Have a look :
for (int x = 1; x < 10; x++){
boolean flag = false;
for(int num = 1; num <= 3; num++){
if ((x%num) == 0 ){
flag = true;
}else{
flag = false;
break;
}
}
if(flag){
System.out.println(x + " success!");
}
}
OUTPUT :
6 success!
I know the code is looking a little horrified but will work for any value of x and num
This is what you'd need to make a comp sci professor happy:
for (int x = 1; x < 10; x++){
boolean success = true;
for (int number = 1; number <= 3; number++) {
if ((x % number) != 0) {
success = false;
}
}
if (success) {
System.out.println("success!");
}
}
although note: (x % 1) is always 0.
This is what you'd need to make me happy, according to my rule of "avoid nested loops":
for (int x = 1; x < 10; x++) {
if (testNumber(x))
System.out.println(x + " success!");
}
}
private static boolean testNumber(int x) {
for (int number = 1; number <= 3; number++) {
if ((x % number) != 0) {
return false;
}
}
return true;
}

Categories