Why is my algorithm not giving the expected output? - java

I am trying to create an Java implementation of the Sieve of Eratosthenes algoritm.
I have the following code, which runs, although gives an incorrect output.
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0;
int currentPrime = test.get(i);
boolean completed = false;
i++;
//do {
while((completed == false) && (i < test.size())) {
if(i >= test.size()) {
completed = true;
} else if((temp <= sqrtOfNum) ) {
removeMultiples(currentPrime);
}
i++;
if (i < test.size()) {
currentPrime = test.get(i);
}
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
for (Integer toTest : test) {
if (!(((toTest) % primeToTest) == 0)) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}
An example of the output given by the program is as follows:
What number would you like the prime numbers to be generated to?
10
Prime numbers upto: 10: [3, 5, 9]
Obviously the output for the above example should be:
Prime numbers upto: 10: [2, 3, 5, 7]

You initialize test to be [2,3,4,5...], set currentPrime to 2 (test[0]), remove multiples of this (removing the 2). I believe a similar things happens when i gets to be 2 and test[2] = 7.
This does not happen with 3 and 5 because you are using i to advance through test, but are also removing items from test so that the values i references changes (because the value in that position has changed). So at the end of the first time through the while loop, i has been advanced to 2 without ever eliminated multiples of 3 or 5 (which you'd see if you used a bigger maxNumber).

The Sieve of Eratosthenes algorithm says that when you consider a prime currentPrime you have to mark as non-primes all its multiples except from itself. In your removeMultiples function you are removing also currentPrime.
The way you iterate in getPrimeList seems also a bit odd to me. I think you might get rid of the completed variable and of some i >= test.size() testing.
Try something like:
import java.util.ArrayList;
public class sieveOfEratosthenes {
private static final ArrayList<Integer> test = new ArrayList<>();
public static void main (String [] args) {
java.util.Scanner tempInput = new java.util.Scanner(System.in);
System.out.println("What number would you like the prime numbers to be generated to?");
int maxPrime = tempInput.nextInt();
for(int i = 2; i <= maxPrime; i++) {
test.add(i);
}
getPrimeList(maxPrime);
}
private static void getPrimeList(int maxNumber) {
int sqrtOfNum = (int) Math.sqrt(maxNumber);
int temp = 0, i = 0, current_prime = 0;
//do {
while(current_prime <= sqrtOfNum && i < test.size()) {
current_prime = test.get(i);
removeMultiples(current_prime);
i++;
}
//}while(completed == false && (i < test.size()));
System.out.println("Prime numbers upto: " + maxNumber + ": " + test);
}
private static void removeMultiples(int primeToTest) {
ArrayList<Integer> temp = new ArrayList<>();
tmp.add(primeToTest);
for (Integer toTest : test) {
if (toTest%primeToTest != 0) {
temp.add(toTest);
}
}
test.clear();
test.addAll(temp);
}
}

Related

getting the prime numbers scanner [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 last year.
Improve this question
I am using the below code and it's giving me the option to enter the final value but I need to give input of two values and also the total count of prime numbers between that range.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter two positive integers: ");
int input = scanner.nextInt();
List<Integer> primes = new ArrayList<>();
// loop through the numbers one by one
for (int i = 2; i < input; i++) {
boolean isPrimeNumber = true;
// check to see if the number is prime
for (int j = 2; j < i; j++) {
if (i % j == 0) {
isPrimeNumber = false;
break; // exit the inner for loop
}
}
// print the number if prime
if (isPrimeNumber) {
primes.add(i);
}
}
System.out.println("The number of prime is: " + primes.size());
System.out.println(primes.toString());
}
Your but fine but not exactly as you've described.
scanner.nextInt() - can produce only one int value (or causes InputMismatchException if input isn't an int).
You said 'i need to give input of two values' - to accomplish it you may have to read these values separately using nextInt() or you can read both as a line, then split the line, and parse to int separately.
The first option is definitely easier.
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter two positive integers: ");
System.out.print("start = ");
int start = scanner.nextInt();
System.out.print("end = ");
int end = scanner.nextInt();
List<Integer> primes = new ArrayList<>();
// loop through the numbers from start to end inclusive
for (int i = start; i <= end; i++) {
// check if i is prime
if (isPrime(i)) {
primes.add(i);
}
}
System.out.println("The number of prime is: " + primes.size());
System.out.println(primes);
}
private static boolean isPrime(int i) {
boolean isPrime = true;
// check to see if the number is prime
for (int j = 2; j <= i / 2; j++) {
if (i % j == 0) {
isPrime = false;
break; // exit the inner for loop
}
}
return i != 1 && isPrime;
}
Let us know, please, is that what you wanted to achieve?
MORE PERFORMANT IMPLEMENTATION
In this implementation, all functionality for finding primes resides in the separate class PrimeUtil.
The constructor of this class is private and to create an instance of this class, it provides a static method getInstance() which accepts two integer numbers representing the desired range.
This is done in order to initialize the object by invoking the init() method and return a fully-fledged object primeUtil, containing the result to the caller.
Method init() populates primes list. All discovered prime numbers will be added to it.
primes list acts as cash. There's no need to rediscover primes over and over again like it's dome in the first solution. That drastically improves the performance.
Another optimization is the way to determine the next probable prime implemented in the method getNextCandidate().
The mathematical logic behind this method is based on the fact that all numbers, that are evenly divisible by 2 and 3 are already eliminated because 2 and 3, are added to the primes list by default and there is no need to consider any number that is divisible 2 or 3. From this fact we can make a conclusion that the remainder of division by 6 for every potential prime cant be equal: 0; 2; 3; 4. I.e. valid prime must fulfill the condition prime % 6 == 1 || prime % 6 == 5.
main
public static void main(String[] args) {
printPrimes();
}
public static void printPrimes() {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter two positive integers: ");
System.out.print("start = ");
int start = scanner.nextInt();
System.out.print("end = ");
int end = scanner.nextInt();
List<Integer> primes = PrimeUtil.getInstance(start, end).getResult();
System.out.println("The number of prime is: " + primes.size());
System.out.println(primes);
}
PrimeUtil class
import java.util.ArrayList;
import java.util.List;
import static java.lang.Math.*;
import static java.util.stream.Collectors.toUnmodifiableList;
public class PrimeUtil {
private final List<Integer> primes;
private final int loBound;
private final int hiBound;
private PrimeUtil(int loBound, int hiBound) {
this.primes = new ArrayList<>();
this.loBound = loBound;
this.hiBound = hiBound;
}
public static PrimeUtil getInstance(int loBound, int hiBound) {
PrimeUtil primeUtil = new PrimeUtil(max(loBound, 2), hiBound);
primeUtil.init();
return primeUtil;
}
private void init() {
if (loBound <= 2) primes.add(2);
if (loBound <= 3) primes.add(3);
int candidate = 5;
while (candidate <= hiBound) {
if (isPrime(candidate)) {
primes.add(candidate);
}
candidate = getNextCandidate(candidate);
}
}
private boolean isPrime(int candidate) {
boolean isPrime = true;
for (int i = 0; i < primes.size() && primes.get(i) <= sqrt(candidate); i++) {
if (candidate % primes.get(i) == 0) {
isPrime = false;
break;
}
}
return isPrime;
}
private int getNextCandidate(int candidate) {
return candidate % 6 == 5 ? candidate + 2 : candidate + 4;
}
public List<Integer> getResult() {
return primes.stream()
.dropWhile(i -> i < loBound)
.collect(toUnmodifiableList());
}
}
All you need is to ask the user for two integer numbers using Scanner two times.
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Please enter two positive integers:");
System.out.print("low range: ");
int lo = scan.nextInt();
System.out.print("high range: ");
int hi = scan.nextInt();
List<Integer> primes = getPrimeNumbers(lo, hi);
System.out.println("The number of prime is: " + primes.size());
System.out.println(primes);
}
private static List<Integer> getPrimeNumbers(int lo, int hi) {
List<Integer> primes = new ArrayList<>();
for (int i = lo; i <= hi; i++)
if (isPrime(i))
primes.add(i);
return primes;
}
private static boolean isPrime(int val) {
if (val == 1)
return false;
if (val == 2 || val == 3)
return true;
for (int i = 2, sqrt = (int)Math.sqrt(val); i <= sqrt; i++)
if (val % i == 0)
return false;
return true;
}
use two Scanners to get second input
you can remove the list and adding elements to it and use a counter if you want only the number of prime numbers :)
So the solution would be :
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Please enter the first positive integer: ");
int firstNumber = scanner.nextInt();
System.out.println("Please enter the second positive integer: ");
// you should have another scanner hence an other text output to invite user
int secondNumber = scanner.nextInt();
List<Integer> primes = new ArrayList<>(); // we do not really need this if we want only the number of prime number and not the list but i'll use it for you anyway
for(int i = firstNumber; i <= secondNumber; i++) //the use the input variables
{
if (isPrime(i)){ // we made a function here to make things clear. it's role to tell weather a number is a prime or not
primes.add(i); // we add prime numbers to this list as you used a list. You can go for an easier solution just a counter++ each time ;)
}
}
System.out.println("The number of prime numbers in this range is: " + primes.size());
}
static boolean isPrime(int num){
if (num <= 1)
return false;
for(int i = 2; i * i <= num; i++)
// If a divisor of n exists
if (num % i == 0)
return false;
return true;
}

Check for perfect number using boolean

I have problem with my Java coding to find the perfect number using boolean method. I want to print out like this:
Example :
‘6 is a perfect number. 6 is the sum of 1, 2, 3’
Otherwise :
‘9 is not a perfect number’
But I don't know how to make the coding for "6 is the sum of 1, 2, 3". Can anyone help me?
Here is my coding :
import java.util.Scanner;
public class trial
{
public static void main(String[] args)
{
// TODO Auto-generated method stub
Scanner perfect = new Scanner(System.in);
System.out.print("Enter any integer number : ");
int n = perfect.nextInt();
if(isPerfectNumber(n))
{
System.out.println(n+" is a perfect number");
}
else
{
System.out.println(n+" is not a perfect number");
}
}
public static boolean isPerfectNumber(int n)
{
int sum = 0;
for (int i=1; i<n; i++)
{
if (n%i == 0)
{
sum = sum + i;
}
}
if (sum == n)
{
return true;
}
else
{
return false;
}
}
}
Well, you already know how to get the factors of the number, as per the if (n%i == 0) line.
So, hint only since this is almost certainly class work.
At the same point you add the factor to the sum, you should add it to a list of some description and have that made available to the calling function.
One possibility would be to return a list, the second and subsequent elements being all the factors of the given number, and the first element being the sum of those.
So, for 6, you would get the list {6, 1, 2, 3}, 12 would give you {16, 1, 2, 3, 4, 6}, and 7 would give you {7, 1}.
That way, you simply have to check the original number against the first element and, if they're equal, print out the other elements. In other words, pseudo-code such as:
input num
factorList = getFactorList(num)
if factorList[0] == num:
print num, " is perfect, factors are:"
for idx = 1 to factorList.size() - 1 inclusive:
print " ", factorList[idx]
println "."
else:
println num, " is not perfect."
I'd change isPerfectNumber to return a List of the factors that make n or null if it's not a perfect number. Then you have a single result that both contains factors and can be used to determine if n is perfect:
public static void main(String[] args) {
Scanner perfect = new Scanner(System.in);
System.out.print("Enter any integer number : ");
int n = perfect.nextInt();
List<Integer> factors = getPerfectFactors(n);
if (factors != null) {
System.out.println
(n + " is a perfect number. It's the sum of" +
factors.stream()
.map(String::valueOf)
.collect(Collectors.joining(", "));
} else {
System.out.println(n+" is not a perfect number");
}
}
public static List<Ingeger> getPerfectFactors(int n) {
int sum = 0;
List<Ingeger> factors = new LinkedList<>();
for (int i = 1; i < n; i++) {
if (n % i == 0) {
sum += i;
factors.add(i);
}
if (sum > n) { // Early return optimization, not material to the solution
return null;
}
}
if (sum == n) {
return factors;
} else {
return null;
}
}
Well in the for loop if condition if modulus is true then you know the i has to be kept in track somehow.
So I would
declare an arraylist of integers to keep track of summation numbers.
ArrayList<Integer> numbs = new ArrayList<Integer>();
Then
public static boolean isPerfectNumber(int n)
{
int sum = 0;
for (int i=1; i<n; i++)
{
if (n%i == 0)
{
numbs.add(i); //here keep track of those summation numbers
sum = sum + i;
}
}
if (sum == n)
{
return true;
}
else
{
return false;
}
}
Now while printing you can do like
System.out.println(n + " is a perfect number. " + n + " is sum of " + StringUtils.join(numbs, ","));
Don't forget to import StringUtils: import org.apache.commons.lang3.StringUtils commons-lang3 library.

Number trailing zeros in factorial in java

import java.util.Scanner;
import java.io.*;
class factorial {
void fact(int a) {
int i;
int ar[] = new int[10000];
int fact = 1, count = 0;
for (i = 1; i <= a; i++) {
fact = fact * i;
}
String str1 = Integer.toString(fact);
int len = str1.length();
i = 0;
do {
ar[i] = fact % 10;
fact /= 10;
i++;
} while (fact != 0);
for (i = 0; i < len; i++) {
if (ar[i] == 0) {
count = count + 1;
}
}
System.out.println(count);
}
public static void main(String...ab) {
int a;
Scanner input = new Scanner(System.in);
a = input.nextInt();
factorial ob = new factorial();
ob.fact(a);
}
}
This code is work up to a = 10 but after enter number larger then a = 16 it gives wrong answer.
Please help.
As I am not able to post this question if I dont add more info for this question but I assume that the info I provide above is enough to under stand what I want.
Like many of these mathematical puzzles, you are expected to simplify the problem to make it practical. You need to find how many powers of ten in a factorial, not calculate a factorial and then find the number of trailing zeros.
The simplest solution is to count the number of powers of five. The reason you only need to count powers of five is that there is plenty of even numbers in between then to make a 10. For example, 5! has one 0, 10! has 2, 15! has three, 20! has four, and 25! has not five but six as 25 = 5 * 5.
In short you only need calculate the number of powers of five between 1 and N.
// floor(N/5) + floor(N/25) + floor(N/125) + floor(N/625) ...
public static long powersOfTenForFactorial(long n) {
long sum = 0;
while (n >= 5) {
n /= 5;
sum += n;
}
return sum;
}
Note: This will calculate the trailing zeros of Long.MAX_VALUE! in a faction of a second, whereas trying this with BigInteger wouldn't fit, no matter how much memory you had.
Please Note, this is not the mathematical solution as others suggested, this is just a refactoring of what he had initially...
Here I just used BigInteger in place of Int, and simplified your code abit. Your solution is still not optimal. I thought I would just show you what a refactored version of what you posted may look like. Also there was a bug in your initial function. It returned the number of zeros in the whole number instead of just the number of trailing zeros.
import java.math.BigInteger;
import java.util.Scanner;
class factorial {
public static void main(String... ab) {
Scanner input = new Scanner(System.in);
int a = input.nextInt();
fact(a);
}
private static void fact(int a) {
BigInteger fact = BigInteger.ONE;
int i, count = 0;
for (i = 1; i <= a; i++) {
fact = fact.multiply(new BigInteger(Integer.toString(i)));
}
String str1 = fact.toString();
for(int j = str1.length() - 1; j > -1; j--) {
if(Character.digit(str1.charAt(j), 10) != 0) {
System.out.println(count);
break;
} else {
count++;
}
}
}
}
Without using factorial
public class TrailingZero {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(trailingZeroes(9247));
}
public static int trailingZeroes(int a) {
int countTwo = 0;
int countFive = 0;
for (int i = a; i > 1; i--) {
int local = i;
while (local > 1) {
if (local % 2 != 0) {
break;
}
local = local / 2;
countTwo++;
}
while (local > 1) {
if (local % 5 != 0) {
break;
} else {
local = local / 5;
countFive++;
}
}
}
return Math.min(countTwo, countFive);
}}

Can 5 numbers equal a target number using addition, subtraction, or multiplication

I'm working on a game, much like the Math Dice problem, albeit a bit different. The user rolls a 20 sided die, then 5 more dice following that. To make things simpler, the user cannot reorder the dice, so if they roll 1 2 3 4 5, they can't do operations like 1 + 3 + 2 + 5 + 4. The question is if, using addition, subtraction, and multiplication, can they reach the target number from the 20 sided die?
Now, I know how to do this, just generate a permutation of all possible addition, subtraction, and multiplication of the 5 numbers, but it's the implementing of the solution that's getting me. I've hit a roadblock after a couple tries, so any help is appreciated.
edit: This is my current implementation, without the multiplication, and it isn't working quite right.
import java.util.ArrayList;
import java.util.Scanner;
public class targetDice {
public static void main(String[] args) {
ArrayList<Integer> rolls = new ArrayList<Integer>(); // Array to hold the rolls
ArrayList<Integer> d20 = new ArrayList<Integer>(); // Array to hold all the d20 rolls
Scanner sc = new Scanner(System.in);
int answer = 0;
String record = "";
while (sc.hasNextInt()) {
d20.add(sc.nextInt()); // Adds the d20 rolls
rolls.add(sc.nextInt()); // Adds the first roll
rolls.add(sc.nextInt()); // Adds the second roll
rolls.add(sc.nextInt()); // Adds the third roll
rolls.add(sc.nextInt()); // Adds the fourth roll
rolls.add(sc.nextInt()); // Adds the fifth roll
} // End while loop
for (int i = 0; i < d20.size(); i++) { // Number of times we need to compute: number of d20 rolls
answer = rolls.get(0);
for (int j = 0; j < rolls.subList(0, 5).size(); j++) { // Go through each roll given
if (d20.get(i) > answer || d20.get(i).equals(answer)) { // If the d20 roll is higher than the first roll or if it's equal
answer += rolls.get(j);// then take the running total and add it
record += " + ";
} else if (d20.get(i) < answer) {
answer -= rolls.get(j);
record += " - ";
}
}
System.out.println(answer);
//TODO: This if else block is our final product. It should be fine.
if (answer == d20.get(i)) // If the combo is equal the d20 roll
System.out.println("Solution"); // Print solution
else
System.out.println("No Solution"); // Otherwise print no solution
rolls.subList(0, 5).clear(); // Clears out the first 5 elements to make coding easier
answer = 0; // Reset the answer var
System.out.println(record);
} // End For loop
} // End main
} // End class
It's set up so that the user can do the rolls more than once, if they were to try this game 3 times, they can do all three then get all three answers at once.
If you want to see it in a different way, here's the pastebin: http://pastebin.com/PRB0NKpN
edit 2: Here's my final solution. A bit bruce-forcey.
import java.util.ArrayList;
import java.util.Scanner;
public class testClass {
public static void main(String[] args) {
ArrayList<Integer> d20 = new ArrayList<Integer>();
ArrayList<Integer> rolls = new ArrayList<Integer>();
Scanner sc = new Scanner(System.in);
while (sc.hasNextInt()) {
d20.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
rolls.add(sc.nextInt());
}
int num1 = 0, num2 = 0, num3 = 0, num4 = 0;
for (int x = 0; x < d20.size(); x++) {
int wright = 0, rong = 0;
for (int i = 1; i < 4; i++) {
for (int j = 1; j < 4; j++) {
for (int k = 1; k < 4; k++) {
for (int m = 1; m < 4; m++) {
if (i == 1) {
num1 = rolls.get(0) + rolls.get(1);
} else if (i == 2) {
num1 = rolls.get(0) - rolls.get(1);
} else if (i == 3) {
num1 = rolls.get(0) * rolls.get(1);
}
if (j == 1) {
num2 = num1 + rolls.get(2);
} else if (j == 2) {
num2 = num1 - rolls.get(2);
} else if (j == 3) {
num2 = num1 - rolls.get(2);
}
if (k == 1) {
num3 = num2 + rolls.get(3);
} else if (k == 2) {
num3 = num2 - rolls.get(3);
} else if (k == 3) {
num3 = num2 * rolls.get(3);
}
if (m == 1) {
num4 = num3 + rolls.get(4);
} else if (m == 2) {
num4 = num3 - rolls.get(4);
} else if (m == 3) {
num4 = num3 * rolls.get(4);
}
if (d20.get(x) == num4) {
wright = 1;
}
}
}
}
}
if (wright == 1)
System.out.println("Case " + (x+1) + ": Solution");
else
System.out.println("Case " + (x+1) + ": No Solution");
rolls.subList(0, 5).clear();
}
}
}
I see you find answer by yourself, but I also tried to solve your problem, and I decide to post here an another solution in any case:
import java.util.ArrayList;
import java.util.Scanner;
public class Test {
public static void main(String[] args){
Test test = new Test();
test.combineOperators();
Scanner scanner = new Scanner(System.in);
int result = scanner.nextInt(); //get input
int[] numbers = new int[5];
for(int i = 0; i <5; i++){
numbers[i] = scanner.nextInt();
}
ArrayList<Integer> results = test.operationsOnArrays(numbers, test.combineOperators()); //check for results
if(results.contains(result)){
System.out.println(result + " is a possible solution");
}else{
System.out.println(result + " is not a possible solution");
}
}
public ArrayList<String[]> combineOperators(){ //create all possible combinations of operators
String[] signs = {"+","-","*"};
ArrayList<String[]> combinations = new ArrayList<String[]>();
for(String a : signs){
for (String b : signs){
for(String c : signs){
for(String d: signs){
String[]temp = {a,b,c,d};
combinations.add(temp);
}
}
}
}
return combinations;
}
public ArrayList operationsOnArrays(int[] num, ArrayList<String[]> combinations){ //do the math with every combination on given ints
ArrayList<Integer> list = new ArrayList<Integer>();
for(String[] operators : combinations){ //for every operators combination
int result = num[0];
for(int i = 0; i<=3 ; i++){
result = doTheMath(operators[i],result,num[i+1]); // take two ints and operator
}
list.add(result);
}
return list;
}
public int doTheMath(String operator, int prev, int next){ // it take two ints from input array, and do operation
if(operator.equals("+")){ // determined by a taken operator
return prev + next;
}else if(operator.equals("-")){
return prev - next;
}else if(operator.equals("*")){
return prev *next;
}
return 0;
}
}
I think that this way, it is vary simple to expand, for more numbers or operator, or even to implement reordering of input numbers.

Finding factors of a given integer

I have something like this down:
int f = 120;
for(int ff = 1; ff <= f; ff++){
while (f % ff != 0){
}
Is there anything wrong with my loop to find factors? I'm really confused as to the workings of for and while statements, so chances are they are completely wrong.
After this, how would I go about assigning variables to said factors?
The following code will return a list of all factors of a given number:
public ArrayList<Integer> findFactors(int num) {
ArrayList<Integer> factors = new ArrayList<Integer>();
// Skip two if the number is odd
int incrementer = num % 2 == 0 ? 1 : 2;
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
// If there is no remainder, then the number is a factor.
if (num % i == 0) {
factors.add(i);
// Skip duplicates
if (i != num / i) {
factors.add(num / i);
}
}
}
// Sort the list of factors
Collections.sort(factors);
return factors;
}
This answer improves Sharad Dargan's answer in two ways:
Based on an idea used in this answer, you can speed up the solution by determining the value to increment by, based on whether the number is even or odd.
Add the following line of code before the for loop:
int incrementer = num % 2 == 0 ? 1 : 2;
Then change the last part of the loop to:
i += incrementer
If the number is odd, it then will skip all even numbers, rather than always incrementing by one no matter what.
Sharad stores the upper limit value in a variable and then uses that variable in the for loop:
int upperlimit = (int)(Math.sqrt(a));
...
for(int i = 1; i <= upperlimit; i+= 1)
Instead, place Math.sqrt(num) directly in the for loop and skip the upper limit variable:
for (int i = 1; i <= Math.sqrt(num); i += incrementer) {
This will allow you to skip the casting part of the code, creating cleaner code.
Some JUnit test cases you can then use:
#Test
public void test12() {
FindFactors find = new FindFactors();
int num = 12;
List<Integer> factors = Arrays.asList(1, 2, 3, 4, 6, 12);
assertEquals(factors, find.findFactors(num));
}
#Test
public void test1000000() {
FindFactors find = new FindFactors();
int num = 1000000;
List<Integer> factors = Arrays.asList(1, 2, 4, 5, 8, 10, 16, 20, 25, 32, 40, 50, 64, 80, 100, 125, 160, 200,
250, 320, 400, 500, 625, 800, 1000, 1250, 1600, 2000, 2500, 3125, 4000, 5000, 6250, 8000, 10000, 12500,
15625, 20000, 25000, 31250, 40000, 50000, 62500, 100000, 125000, 200000, 250000, 500000, 1000000);
assertEquals(factors, find.findFactors(num));
}
#Test
public void test1() {
FindFactors find = new FindFactors();
int num = 1;
List<Integer> factors = Arrays.asList(1);
assertEquals(factors, find.findFactors(num));
}
#Test
public void test0() {
FindFactors find = new FindFactors();
int num = 0;
List<Integer> factors = new ArrayList<Integer>();
assertEquals(factors, find.findFactors(num));
}
Here is how to get all factors of the given number.
public class Factors {
public static void main(String[] args){
int n = 420;
for(int i=2; i<=n; i++){
while(n%i==0){
System.out.println(i + "| " + n);
System.out.println(" -----");
n = n/i;
}
}
}
}
Output:
2| 420
-----
2| 210
-----
3| 105
-----
5| 35
-----
7| 7
-----
public class Solution {
public ArrayList<Integer> allFactors(int a) {
int upperlimit = (int)(Math.sqrt(a));
ArrayList<Integer> factors = new ArrayList<Integer>();
for(int i=1;i <= upperlimit; i+= 1){
if(a%i == 0){
factors.add(i);
if(i != a/i){
factors.add(a/i);
}
}
}
Collections.sort(factors);
return factors;
}
}
The above solution simply works like calculating prime factors.
The difference being for every prime factor we keep calculating the other part of the product i.e the reqd number.
In order to find the factors of a given number, you only need to check upto the square root of the given number.
For example, in order to find the factors of 6, you only need to check till 2.45 (√6). The factors of 6 will be 1 and 2, and their converse numbers, i.e. 3 and 6.
I have made a program that determines the factors of a given number and displays them. Here is the necessary code:
Scanner input = new Scanner(System.in);
System.out.print("Enter integer: ");
long num = input.nextLong();
for(long i = 1; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
System.out.println(i);
if(i != num/i) {
System.out.println(num/i);
}
}
}
You just need this program to find the factors of a given number. However, if you want to take it a step further and display the factors arranged in ascending order, then the necessary code is as follows:
Scanner input = new Scanner(System.in);
System.out.print("Enter integer: ");
long num = input.nextLong();
ArrayList<Long> list1 = new ArrayList<>(), list2 = new ArrayList<>();
long currentTime = System.currentTimeMillis();
for(long i = 1; i <= Math.sqrt(num); i++) {
if(num % i == 0) {
list1.add(i);
if(i != num/i) {
list2.add(num/i);
}
}
}
int n1 = list1.size() - 1;
int n2 = list2.size() - 1;
for(int i = 0; i <= n1; i++) {
System.out.println(list1.get(i));
}
for(int i = n2; i >= 0; i--) {
System.out.println(list2.get(i));
}
What this does: This program stores the factors of the number upto the number's square root in one list (list1), and the converse of these numbers in another list (list2). It then prints the elements of both lists (as shown).
There's nothing wrong with your for loop, but a while loop is the wrong thing to be using here.
The logic of your for loop is:
Set ff to 1.
Keep going while ff <= f.
After you've done everything in the for loop, add 1 to ff.
This looks like it is exactly as you want.
The while loop isn't right, though. It will continue to do whatever code you write there for as long as ff is a factor of f, so unless you change them in the while code, you'll get an infinite loop. However, changing that to an if statement will give you what you want.
Since you're checking for factors, you don't actually need to check all possibilities up to f - only up to the square root of f. Whenever you find that ff is a factor, output both ff and f/ff as factors, unless f is a sqare number.
public static void printFactors(int number) {
if (number < 1 )
System.out.println("Invalid Value");
for (int i = 1 ; i <= number ; ++i) {
if ( number % i == 0)
System.out.println(i);
}
}
}
It looks like you are not going to do something with either f or ff in your while loop? If so, the expression f%ff != 0 is either false (and then it will go to the next in the for loop), or it is true, and it will end up in an infinite loop.
Are you sure you need the while like this?
Slightly modified solution: You can first check if variable x is divisible by variable y. If yes, we will count 1 and will repeat this process. For the loop counter, x/y is used and you should check x>0 to avoid repetition when x becomes zero but loop is not finished yet.
public class Factor {
public static void main(String[] args) {
int x = 48;
int x1 = x;
int y = 2;
int k = x / y;
int j = 0;
for (int i = 1; i < k; i++) {
if ((x % y) == 0 && x > 0)
j++;
x = x / 2;
}
System.out.println(+x1 + " is a factor of " + y + " for " + j
+ " times.");
}
}
I got all the factors just fine with this (I just modified the algorithm in the question).
int num1 = 120;
for(int num2=1;num2<=num1;num2++)
{
if (num1%num2 != 0)
System.out.println(num2);
}
import java.util.Scanner;
public class Factors
{
Scanner scn=new Scanner(System.in);
int num=scn.nextInt();
public void findFactor()
{
System.out.println("Factors are");
System.out.println("1");
for(int i=2;i<=num;i++)
{
if(num%i==0)
{
num=num/i;
System.out.println(i);
i=2;
}
}
}
public static void main(String[] args)
{
while(1==1)
{
System.out.println("Enter a Number");
Factors fct=new Factors();
fct.findFactor();
}
}
}
Utilizing Streams introduced in Java 8, the following will print the factors for a given number.
int input = 1500;
IntStream.rangeClosed(1, input)
.filter(e -> input % e == 0)
.forEach(System.out::println);
This is how you write it yourself like a boss. Needs to add if statements to handle one and two, but besides that; this method is as sexy as it gets
public static void primerize(int n){
boolean reduced = false;
while(n > 2){
if(n%2 == 0){
System.out.println(2 + "," + n/2);
n /= 2;
}
else{
int i = isPrime(n);
if(i == n && reduced == false){
System.out.println(1 + "," + n);
n /= n;
}
else if(i == n){
n/= n;
}
else{
System.out.println(i + "," + n/i);
n = i;
reduced = true;
}
}
}}
public static int isPrime(int n){
for(int i = (n/3); i > 0; i--){
if(i == 1){
return n;
}
else if(n%i == 0){
return i;
}
}
return 0;}
This code will give you the factors.
ArrayList<Integer> arr = new ArrayList<>();
int x=48;
int y=1;
while(x!=1)
{
if(x%y==0)
{
x=x/y;
arr.add(y);
if(y==1)
{
y++;
}
}
else
{
y+=1;
}
}
System.out.println(arr);
Easiest way using recursive function
public static int factorial(int n){
if(n!=1)
return n*factorial(n-1);
return 1;
}

Categories