Please put your code in code tags so that we can read it. Also, give something more explanatory than “it doesn’t work”.
I doubt whether your search loop will be able to pick up repeated prime factors.
If you are picking off factors from the lowest first then you DON’T have to test that they are individually prime because you have already picked off lower factors.
Until you put your code in code tags so that we can see indentation and loop structure I would refrain from further comment.
Hi, thanks for replying. By "it doesn't work" I mean when I run the code, it doesn't show any errors in my code but it won't give any outputs at all, just blank. I'm sorry, but can you please tell me how to put my code in code tags? I'd really appreciate it
You can either edit your original post or create a new one and then use the Format menu (the first item in <>). This will put the requisite tags in for you.
The format menu doesn’t function for an initial post, so either edit that quickly or manually put the tags in. You will see what they are once you have used the format menu.
This is C++, maybe it looks like C because I've only learned the basics(started learning about 5 days ago). I'm sorry but I don't understand any of your suggested solutions, I haven't learnt that far 😭 it's ok tho, I will try to find another way to solve this problem
This is C++, maybe it looks like C because I've only learned the basics
printf is more of a C way of outputting things to the console.
cout << p << "^" << ar[p];
cout, as shown, is the modern C++ way to output to the screen. scanf is also C, with C++ using std::cin >>:
1 2 3 4 5 6
int main()
{
int N;
cin >> N;
PF(N);
}
You're also using "#include <cstdio>", which gives you access to the C library. Whatever tutorial you're following is NOT taking advantage of C++'s features and benefits over C.
Prime factorization isn't as complex as your code is making it, you can look at a reference here:
1. main should look like this otherwise all you produce is a blinking cursor.
1 2 3 4 5 6 7 8
int main()
{
int N;
printf ("Enter a number: ");
scanf("%d", &N);
PF(N);
}
2. To get the prime factors then consider the following as a straight forward way. (It doesn't mean you have to copy it but draw on the ideas.) https://www.geeksforgeeks.org/prime-factor/
@sultan04,
You keep dividing y by p, so that eventually it is likely to be 1. Thus your second major loop (limited by p<=y) is unlikely ever to run and so it won’t print anything.
There is no need to tick off found prime factors in ar[]. If you want to put anything in that array ... put your prime factors in it!
Your method is flawed. As I said earlier, you will fail to pick up repeated prime factors and you do NOT explicitly have to check whether a potential factor is prime because you have already covered any smaller factors.
Please re-think your method. A good way would be to do it by hand. Try factorising 360 (which is 2*2*2*3*3*5) and also 13 (which is prime).
factoring numbers is considered a difficult problem .. some encryption is based off that idea, that finding one of the 2 factors of 2 gigantic prime numbers multiplied together is aggravating.
whatever you do is likely going to end up being brute force:
for all the prime numbers up to the sqrt of the number,
see if it will divide into the number. If it does, try again until it won't. Update number each time by dividing by the factor you found.
so to factor all unsigned 64 bit integers, you can find all the primes that fit in 32 bits (this is PDQ on today's computers with a good sieve), and then just crunch along.
you can find the factors (but not prime factors) very quickly up to 100 or maybe a bit beyond with std::gcd and a couple of magic constants:
614889782588491410ULL
3749562977351496827ULL
which is really effective for typical non crypto use cases of small numbers with lots of 2,3,5,7 factors. Numbers that don't have a factor in 2-100 will end up back in the brute force loops.
#include <iostream>
#include <vector>
usingnamespace std;
vector<unsignedlonglong> factorise( unsignedlonglong N )
{
vector<unsignedlonglong> result; if ( N <= 1 ) return result;
for ( unsignedlonglong i = 2; i * i <= N; i += 1 + ( i > 2 ) )
{
while ( N % i == 0 )
{
result.push_back( i );
N /= i;
}
}
if ( N > 1 ) result.push_back( N );
return result;
}
int main()
{
unsignedlonglong tests[] = { 2, 9, 13, 169, 360, 3600, 12345678910111213 };
for ( auto N : tests )
{
cout << N << " =";
int num = 0;
for ( auto e : factorise( N ) ) cout << ( ++num == 1 ? " " : " x " ) << e;
cout << '\n';
}
}
2 = 2
9 = 3 x 3
13 = 13
169 = 13 x 13
360 = 2 x 2 x 2 x 3 x 3 x 5
3600 = 2 x 2 x 2 x 2 x 3 x 3 x 5 x 5
12345678910111213 = 113 x 125693 x 869211457
Thanks a lot guys, it's working just as I hoped now. I still don't know what I did wrong in my code but I've found a new code that doesnt include a 1000001 sized array, in fact I didnt need to check if the divisor is a prime at all cuz once a number has been factorized completely by its prime factors (starting from 2,3,so on) the number wont be divisible by their multiples anymore hehe
Omg that makes senseee thats why it didnt work beforeee the y will have already become 1 once ive done running the first loop, thats why the next loop doesnt run, since p = 2 which is > y = 1... thank you so much this kept me up at night lol