folks, I've been struggling to figure out the algorithm to get the list of all of the prime factors of the given number (in my case, the given number is myNumber = 14). For example,
14 = 2 × 7
15 = 3 × 5
645 = 3 × 5 × 43
646 = 2 × 17 × 19
But my code is running infinitely and I'm not pretty sure if my algorithm works fine. Could smb take a look or give me a hand how to see the problem? Thanks in advance!
import java.util.*;
public class DistinctFactors {
public static final List<Integer> myList = new ArrayList<>();
public static void main(String[] args){
int result = 1;
int myNumber = 14;
int i = 2;
while(result != myNumber){
if(isPrime(i)){
myList.add(i);
result *= i;
}
i++;
}
for(int j = 0; i < myList.size(); j++){
System.out.print(myList.get(j) + " ");
}
}
private static boolean isPrime(int number){
for(int i = 2; i < number; i++){
if(number % 2 == 0){
return false;
}
}
return true;
}
}
I mean, let's look at what the values of result and i will be.
Pass 1: r = 1, i = 2
Pass 2: r = 2, i = 3
Pass 3: r = 6, i = 4
Pass 4: r = 6, i = 5
Pass 5: r = 30, i = 6
From this point on, r will only increase, and it's already greater than 14. So of course this loop will never terminate.
Your method is also extremely wrong. I have no idea why you chose this way to try and get prime factors.
Not to mention, even your isPrime method is kind of dumb. It checks all the way up to the number you're checking, which is extremely wasteful.
To check if a number n is prime, you should instead compute the square root; if it is an integer, then the number is obviously not prime. Otherwise, take the floor of that sqrt(n) - let's call it k - and run the loop up to k. If n is not prime, you will find a divisor in that range; if you find none, n is prime.
(That is an O(log(n)) method. The best method is the one that involves checking whether the number satisfies Fermat's Little Theorem for random values, which is constant time).
EDIT: Well, not exactly constant time if you don't consider certain operations O(1). For huge numbers it's much better than the other method
Your current loop just checks whether the number is even a bunch of times...
Related
the question is "Write a function, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit."
public static void main(String[] args){
System.out.println(persistence(39));
//System.out.println(persistence(999));
}
public static int persistence(long n) {
long m = 1, r = n;
if (r / 10 == 0) {
return 0;
}
for(r = n; r!= 0; r /=10){
m *= r % 10;
}
//System.out.println(m);
return persistence(m) + 1;
}
I understand that the if statement is for when its finally a single digit and it'll return 0. If i could get an explanation on the m variable and what its there for. What the for loop does and when it returns persistence(m) why there is a + 1 on it.
The calculation will comes likes this .
let us understand the problem statement.
Write a function, persistence, that takes in a positive parameter num
and returns its multiplicative persistence, which is the number of
times you must multiply the digits in num until you reach a single
digit."
Say : 39
which is the number of times you must multiply the digits in num
until you reach a single digit.
So, we need to do like this to satisfy the above statement.
39 = 3*9 = 27 (1 time) - persistance(39)
27 = 2*7 = 14 (2rd time) - persistance(27)
14 = 1*4 = 4 (3rd time)- persistance(14)
So, according to the problem statement we come to the single digit.
you can take reference of the below, understand it .
why there is a + 1 on it.
to count the number of times the recursive function done the calculation.
I was trying a programming problem, the statement is
If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Like this we have to find the sum of multiples for 't' test cases with 'n' value each, I have tried to find the solution and my code is
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
long t,n,sum;
Scanner in=new Scanner(System.in);
t=in.nextLong();
for(int i=0;i<t;i++)
{
sum=0;
n=in.nextLong();
long l3=0,l5=0,l15=0;
for(int j=3;j>0;j--)
if((n-j)%3==0&&j<n)
{
l3=n-j;
break;
}
for(int j=5;j>0;j--)
if((n-j)%5==0&&j<n)
{
l5=n-j;
break;
}
for(int j=15;j>0;j--)
if((n-j)%15==0&&j<n)
{
l15=n-j;
break;
}
sum+=(float)(((float)l3/(float)3)/(float)2)*(float)(l3+3);
sum+=(float)(((float)l5/(float)5)/(float)2)*(float)(l5+5);
sum-=(float)(((float)l15/(float)15)/(float)2)*(float)(l15+15);
System.out.println(sum);
}
}
}
And the input I gave was,
12
10
11
12
13
1000
1001
1002
1003
100000000
100000001
100000002
100000003
Here 12 is the number of test cases.
And the output I got was
23
33
33
45
233168
234168
234168
235170
2333333593784320
2333333593784320
2333333593784320
2333333593784320
The problem here is the answer is correct for values in the test case 10,11,12,13,1000,1001,1002,1003 but the output is wrong for remaining bigger inputs. I cant find what i am missing. Could you please help me on why I am getting this kind of wrong result and how to rectify it.
You can get a higher precision and a larger numbers by using BigDecimal and BigInteger:
package test;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
long t,n;
BigInteger sum;
Scanner in=new Scanner(System.in);
t=in.nextLong();
for(int i=0;i<t;i++)
{
sum = BigInteger.ZERO;
n=in.nextLong();
long l3=0,l5=0,l15=0;
for(int j=3;j>0;j--)
if((n-j)%3==0&&j<n)
{
l3=n-j;
break;
}
for(int j=5;j>0;j--)
if((n-j)%5==0&&j<n)
{
l5=n-j;
break;
}
for(int j=15;j>0;j--)
if((n-j)%15==0&&j<n)
{
l15=n-j;
break;
}
BigDecimal x = BigDecimal.valueOf(l3)
.divide(BigDecimal.valueOf(6))
.multiply(BigDecimal.valueOf(l3+3));
sum=sum.add(x.toBigIntegerExact());
x = BigDecimal.valueOf(l5)
.divide(BigDecimal.valueOf(10))
.multiply(BigDecimal.valueOf(l5+5));
sum=sum.add(x.toBigIntegerExact());
x = BigDecimal.valueOf(l15)
.divide(BigDecimal.valueOf(30))
.multiply(BigDecimal.valueOf(l15+15));
sum=sum.subtract(x.toBigIntegerExact());
System.out.println(sum);
}
}
}
Without deeper analysis of your code I would guess the problem is that you are using float, which has a quite short value range. Could you try double instead? Not sure what the correct answer would be, but at least you get different results (I just tried)
The solution looks too complex for such a task. I don't understand why do you need to perform the divisions at the end. You can try the following code:
int t = <some number> // the upper bound
int[] dividers = [...] // those are divisors against which we have to test
long sum = 0;
// check all the number up the bound
for (int number = 1; number < t; number++) {
for (int i = 0; i < dividers.length; i++) {
if (number % dividers[i] == 0) {
// the number is divisible without remainder -> add it to the sum
sum += number;
break;
}
}
}
The idea is to iterate all the numbers you want to check and see if they are divisible by some of the N dividers. If you find a number, you add it to the sum and continue with the next one.
Edit:
After the clarifications from OP, I came up with another way to do this.
int t = <some number> // the upper bound
int dividers = [3, 5];
int dividerProduct = dividers[0] * dividers[1];
long sum = calculateSumForDivider(dividers[0], t) + calculateSumForDivider(dividers[1], t) - calculateSumForDivider(dividerProduct, t);
public static int calculateSumForDivider(int divider, int number) {
int n = number / divider;
return divider * n * (n + 1) / 2;
}
What is the logic behind all this?
By dividing the target number, we can calculate how many times does the divider "fit" in the target. This is also the number of numbers in the interval [1, number] that are divisible by the divider. Let's see an example:
t = 10, divider = 3
10 / 3 = 3, so they are 3 numbers in the interval [1, 10], divisible by 3
the numbers are: 1 * 3, 2 * 3, 3 * 3
if we calculate the sum we get 1 * 3 + 2 * 3 + 3 * 3 = 3 * (1 + 2 + 3) = 18
analogically, for = 10, divider = 5
10 / 5 = 2
1 * 5 + 2 * 5 = 5 * (1 + 2) = 15
As a conclusion, we have the following formula for the sum:
sum = divider * n * (n + 1) / 2
where n is the result of the division.
The gotcha here is that numbers, divisible by both 3 and 5 (in other words divisible by 15) are going to be added twice to the sum. To correct this we use the same formula as above to calculate their sum and subtract it from the resulting some, reaching the result.
This solution will only work well for 2 dividers, since with multiple the number of numbers that will be added multiple times to the sum will grow exponentially. F.e. if we want to divide be 3, 4 or 5, we will need to take care of 12, 15, 20, 60, etc.
This will also not work if the two one of the dividers is a power of the other, like 3 and 9. In that case we only need the numbers, divisible by 3.
I am not sure about your algorithm because the simplest one should be like:
sum=0;
n=100100000;
for(int j=1;j<n;j++)
if(j%3==0 || j%5==0)
{
sum+=j;
}
System.out.println(sum);
System.out.println(n*n);
And to get a better idea on what should be the result, it is always less than n*n.
The output I found was:
2338002249916668
10020010000000000
So, as your results where less than n*n, if you are sure about you algorithm, then they are correct.
I'm supposed to find the prime factorization of this number: 600851475.
Prime factorization, according to my teacher and this website, http://www.mathsisfun.com/prime-factorization.html, are prime numbers that multiplied give you that number. So for example 12, even though its factors are 2,3,4,6, the prime factors WON'T be just 2 & 3, but 2,2,3.
I have the algorithm to find a prime factor allready, but I can't find a way to loop so that it keeps finding the rest until there are no more prime factors.
This is what i got:
public class primeFactors {
public static void main(String[] args) {
int d= 600851475;
int i = 2;
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
}
}
and it prints this: 3.
If i copy paste it multiple times it does print different things:
public class primeFactors {
public static void main(String[] args) {
int d= 600851475;
int i = 2;
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
if (d%i!=0) {i++;}
if (d%i==0) {d=d/i;}
System.out.println(i);
}
}
That one prints: 3, 3, 4, 5, 5.
How can I do this with loops? I tried with do while loops( do { if section} while (d>i) {print i} ), but it doesn't work. I also tried with for loop (i=2;i<=d;i++) & it doesn't work. It gives me composite numbers too.
HELP PLEASE!!
Since this is an assignment, the most I'll give you is a general direction: the easiest way to write this will be to try candidate divisors and reduce. For example:
130 - try 2, it divides, so reduce
65 - maybe there's another 2 in there? try 2 again. It doesn't divide, so move on
65 - try 3 - no. 4? no. 5? yes, it divides, so reduce.
13 - Is there another 5 in there? no. try 6, 7, 8, 9, 10, 11, 12. Okay, you're done.
So you need to try candidate divisors in a loop, and you need an inner loop to make sure you cast out any repeated factors (for example, 525 will have the prime factors 3, 5, and 7 but you still want to get rid of that second 5). That should get you on the right track.
Clearly, there will be more efficient ways to write this, but if you're stuck, start with the simplest possible thing that could work, and get that working.
You only need something like this (Perhaps this is not the most efficient way but it was pretty straight forward and easier to understand) :
int d= 600851475;
for (int i = 2 ; i < (d / 2) ; i++){
if(isPrime(i)){
if(d % i ==0){
System.out.println(i + "Is a prime factor of " + d);
}
}
}
But first you will need to have this method that check if it is a prime number or not
public static boolean isPrime(int n){
for(int i = 2 ; i < n ; i++){
if(n % i == 0){
return false;
}
}
return true;
}
Try to use a for loop.
for (int i = 2; i <= d; i++)
{
//implement if statement here
}
This should point you in the right direction.
You have the right idea. You need to divide each factor until it no longer divides.
// this will print all prime divisors for n
for (int i = 2; n != 1; i++)
{
while (n % i == 0)
{
System.out.println (i);
n /= i;
}
}
How to find the prime factorization of a number?
Here is what I tried:
def p_factorization(n):
#Finding the factors of n.
num=[i for i in range(1,n+1) if n%i==0]
#Finding the prime factors of n.
#Now prime_num checks to see if any of the factors of 36 have more than two "subfactors".
prime_num=[i for i in range(2,max(num)) if len([j for j in range(1,i+1) if i%j==0])<=2 and max(num)%i==0]
return prime_num
#Explanation:
#The num list is self-explanatory. It finds all of the factors of the number n.
#The prime_num list comprehension is where it gets complicated:
#The i represents all of the numbers in the range of the original factors that we found,
#then we know that a number is prime if it has two or fewer factors, hence the if condition
#applying to the len of the factors of i, which is being iterated through.
#For example, 4 is not a prime number because the length of the list of factors is
#3 [1,2,4].
And of course, we still need max(num) to be divisible by i.
You are very welcome!
http://projecteuler.net/problem=1
Hey. I'm a high schooler trying to get a good grasp on programming problems, so I visited Project Euler. For Problem 1, I wrote up some code in Java that would of solved it, but something is evidently going wrong. Can I get some insight as to what?
Explanation:
I stop everything at the index value of 332 because Java counts from 0, and 333 * 3 is 999 which is below 1,000. Apples is a seperate class with pretty much the same code, although it counts for 5. At the end, I manually add together the two answers, but it wasn't right. What am I doing wrong?
The two final sums are:
Three: 164838
Five: 97515
public class Learning {
public static void main(String[] args){
int three[] = new int[333];
int counter = 0;
three[332] = 0;
int totalthree = 0;
int threeincrementer = 1;
int grandtotal;
boolean run = true;
boolean runagain = true;
for (counter = 1; counter<=332; counter++){
three[counter] = 3 * counter;
if (!(three[332] == 0)){
System.out.println("Finished three.");
while (run == true){
totalthree = totalthree + three[threeincrementer];
threeincrementer++;
if (threeincrementer >= 332){
run = false;
System.out.println("Three final is: " + totalthree);
}
}
}
if (runagain == true){
apples ApplesObject = new apples();
ApplesObject.rerun(0);
runagain = false;
}
}
}
}
Some numbers are at the same time multiplication of 3 AND 5 like 15 so you shouldn't separately calculate sum of all multiplications of 3 and multiplications of 5 and than add them because you will end up doing something like
sum3 = 3,6,9,12,15,...
sum5 = 5,10,15,...
so first sum3 will include 15, and sum5 will also include it which means you will add 15 two times. Now to balance your calculations you will need to subtract from your sum3+sum5 sum which will add all multiplications of 15
sum15 = 15,30,45,...
So using your approach your final formula should look like sum3+sum5-sum15.
But simpler solution for this problem can look like
sum = 0
for each X in 1...999
if (X is multiplication of 3) OR (X is multiplication of 5)
add X to sum
To check if some number X is multiplication of number Y you can use modulo operator % (example reminder = X % Y) which finds the remainder of division of one number by another.
You can find more Java operators here
Problem:
Each new term in the Fibonacci sequence is generated by adding the
previous two terms.
By starting with 1 and 2, the first 10 terms will
be:
1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
By considering the terms in the Fibonacci sequence whose values do not
exceed four million, find the sum of the even-valued terms.
My code: (which works fine)
public static void main(String[] agrs){
int prevFirst=0;
int prevSecond=1;
int bound=4_000_000;
int evenSum=0;
boolean exceed=false; //when fib numbers > bound
while(!exceed){
int newFib=prevFirst + prevSecond;
prevFirst = prevSecond;
prevSecond = newFib;
if(newFib > bound){
exceed=true;
break;
}
if(newFib % 2 == 0){
evenSum += newFib;
}
}
System.out.println(evenSum);
}
I'm looking for a more efficient algorithm to do this question. Any hints?
When taking the following rules into account:
even + even = even
even + odd = odd
odd + even = odd
odd + odd = even
The parity of the first Fibonacci numbers is:
o o e o o e o o e ...
Thus basically, you simply need to do steps of three. Which is:
(1,1,2)
(3,5,8)
(13,21,34)
Given (a,b,c) this is (b+c,b+2*c,2*b+3*c).
This means we only need to store the two last numbers, and calculate given (a,b), (a+2*b,2*a+3*b).
Thus (1,2) -> (5,8) -> (21,34) -> ... and always return the last one.
This will work faster than a "filter"-approach because that uses the if-statement which reduces pipelining.
The resulting code is:
int b = 1;
int c = 2, d;
long sum = 0;
while(c < 4000000) {
sum += c;
d = b+(c<<0x01);
c = d+b+c;
b = d;
}
System.out.println(sum);
Or the jdoodle (with benchmarking, takes 5 microseconds with cold start, and on average 50 nanoseconds, based on the average of 1M times). Of course the number of instructions in the loop is larger. But the loop is repeated one third of the times.
You can't improve it much more, any improvement that you'll do will be negligible as well as depended on the OS you're running on.
Example:
Running your code in a loop 1M times on my Mac too 73-75ms (ran it a few times).
Changing the condition:
if(newFib % 2 == 0){
to:
if((newFib & 1) == 0){
and running it again a few times I got 51-54ms.
If you'll run the same thing on a different OS you might (and
probably will) get different results.
even if we'll consider the above as an improvement, divide ~20ms in 1M and the "improvement" that you'll get for a single run is meaningless (~20 nanos).
assuming consecutive Fibonacci numbers
a, b,
c = a + b,
d = a + 2b,
e = 2a + 3b,
f = 3a + 5b,
g = 5a + 8b = a + 4(a + 2b) = a + 4d,
it would seem more efficient to use
ef0 = 0, ef1 = 2, efn = efn-2 + 4 efn-1
as I mentioned in my comment there is really no need to further improvement.
I did some measurements
looped 1000000 times the whole thing
measure time in [ms]
ms / 1000000 = ns
so single pass times [ns] are these:
[176 ns] - exploit that even numbers are every third
[179 ns] - &1 instead of %2
[169 ns] - &1 instead of %2 and eliminated if by -,^,&
[edit1] new code
[105 ns] - exploit that even numbers are every third + derived double iteration of fibonaci
[edit2] new code
[76 ns] - decreased operand count to lower overhead and heap trashing
the last one clearly wins on mine machine (although I would expect that the first one will be best)
all was tested on Win7 x64 AMD A8-5500 3.2GHz
App with no threads 32-bit compiler BDS2006 Trubo C++
1,2 are nicely mentioned in Answers here already so I comment just 3:
s+=a&(-((a^1)&1));
(a^1) negates lovest bit
((a^1)&1) is 1 for even and 0 for odd a
-((a^1)&1)) is -1 for even and 0 for odd a
-1 is 0xFFFFFFFF so anding number by it will not change it
0 is 0x00000000 so anding number by it will be 0
hence no need for if
also instead of xor (^) you can use negation (!) but that is much slower on mine machine
OK here is the code (do not read further if you want to code it your self):
//---------------------------------------------------------------------------
int euler002()
{
// Each new term in the Fibonacci sequence is generated by adding the previous two terms.
// By starting with 1 and 2, the first 10 terms will be: 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, ...
// By considering the terms in the Fibonacci sequence whose values do not exceed four million, find the sum of the even-valued terms.
int a,a0=0,a1=1,s=0,N=4000000;
/*
//1. [176 ns]
a=a0+a1; a0=a1; a1=a; // odd
a=a0+a1; a0=a1; a1=a; // even
for (;a<N;)
{
s+=a;
a=a0+a1; a0=a1; a1=a; // odd
a=a0+a1; a0=a1; a1=a; // odd
a=a0+a1; a0=a1; a1=a; // even
}
//2. [179 ns]
for (;;)
{
a=a0+a1; a0=a1; a1=a;
if (a>=N) break;
if ((a&1)==0) s+=a;
}
//3. [169 ns]
for (;;)
{
a=a0+a1; a0=a1; a1=a;
if (a>=N) break;
s+=a&(-((a^1)&1));
}
//4. [105 ns] // [edit1]
a0+=a1; a1+=a0; a=a1; // 2x
for (;a<N;)
{
s+=a; a0+=a1; a1+=a0; // 2x
a=a0+a1; a0=a1; a1=a; // 1x
}
*/
//5. [76 ns] //[ edit2]
a0+=a1; a1+=a0; // 2x
for (;a1<N;)
{
s+=a1; a0+=a1; a1+=a0; // 2x
a=a0; a0=a1; a1+=a; // 1x
}
return s;
}
//---------------------------------------------------------------------------
[edit1] faster code add
CommuSoft suggested to iterate more then 1 number per iteration of fibonaci to minimize operations.
nice idea but code in his comment does not give correct answers
I tweaked a little mine so here is the result:
[105 ns] - exploit that even numbers are every third + derived double iteration of fibonaci
this is almost twice the speedup of 1. from which it is derived
look for [edit1] in code or look for //4.
[edit2] even faster code add
- just reorder of some variable and operation use for more speed
- [76 ns] decreased operand count to lower overhead and heap trashing
if you check Fibonacci series, for even numbers 2 8 34 144 610 you can see that there is a fantastic relation between even numbers, for example:
34 = 4*8 + 2,
144 = 34*4 + 8,
610 = 144*4 + 34;
this means that next even in Fibonacci can be expressed like below
Even(n)=4*Even(n-1)+E(n-2);
in Java
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int t = in.nextInt();
for(int a0 = 0; a0 < t; a0++){
long n = in.nextLong();
long a=2;
long b=8;
long c=0;
long sum=10;
while(b<n)
{
sum +=c;
c=b*4+a;
a=b;
b=c;
}
System.out.println(sum);
}
}
F(n) be the nth Fibonnaci number i.e F(n)=F(n-1)+F(n-2)
Lets say that F(n) is even, then
F(n) = F(n-1) + F(n-2) = F(n-2) + F(n-3) + F(n-2)
F(n) = 2F(n-2) + F(n-3)
--This proves the point that every third term is even (if F(n-3) is even, then F(n) must be even too)
F(n) = 2[F(n-3) + F(n-4)] + F(n-3)
= 3F(n-3) + 2F(n-4)
= 3F(n-3) + 2F(n-5) + 2F(n-6)
From eq.1:
F(n-3) = 2F(n-5) + F(n-6)
2F(n-5) = F(n-3) - F(n-6)
F(n) = 3F(n-3) + [F(n-3) - F(n-6)] + 2F(n-6)
= 4F(n-3) + F(n-6)
If the sequence of even numbers consists of every third number (n, n-3, n-6, ...)
Even Fibonacci sequence:
E(k) = 4E(k-1) + E(k-2)
Fib Sequence F= {0,1,1,2,3,5,8.....}
Even Fib Sequence E={0,2,8,.....}
CODE:
public static long findEvenFibSum(long n){
long term1=0;
long term2=2;
long curr=0;
long sum=term1+term2;
while((curr=(4*term2+term1))<=n){
sum+=curr;
term1=term2;
term2=curr;
}
return sum;
}
The answer for project Euler problem 2 is(in Java):
int x = 0;
int y = 1;
int z = x + y;
int sumeven = 0;
while(z < 4000000){
x = y;
y = z;
z = x + y;
if(z % 2 == 0){
sumeven += z; /// OR sumeven = sumeven + z
}
}
System.out.printf("sum of the even-valued terms: %d \n", sumeven);
This is the easiest answer.