Prime Factorization Function Output - c++

I made this function that calculates the prime factorization of a number (n) which is obtained from the user. I am having issues with it due to the fact that It does not print the same factor more than once.
For Example:
The Prime Factorization of 3960 is:
11 5 3 3 2 2 2
However my program only prints out:
11 5 3 2
Can anyone help me to identify the cause and help me find a solution?
void primefact(int n)
{
Stack f;
assert(n >= 0);
bool prime;
for(int d = 2; d <= n; d++) // Test for factors > 1
{
if(n % d == 0)
{
prime = true;
for(int j = 2; j < d; j++) // Test for prime
{
if(d % j == 0) // It is not prime
prime = false;
}
if(prime)
f.push(d);
}
}
while(!f.empty())
{
cout << f.top() << endl;
f.pop();
}
}

You have to loop over the same prime as long as it divides the input.

Can anyone help me to identify the cause?
You're checking whether n is divisible by d, but then you move on to the next value. If n is divisible by d and d is prime, you need to actually divide n by d and check d again.
Let's take 12 as an example. Prime factors are [3, 2, 2]. Your code does this:
n = 12, d = 2
n % d == 0? Yes. Push d. d = d + 1
n % d == 0? Yes. Push d. d = d + 1
n % d == 0? No. d = d + 1
n % d == 0? No. d = d + 1
n % d == 0? No. d = d + 1
n % d == 0? No. d = d + 1
// and so on until d == n
You want code that does this:
n = 12, d = 2
n % d == 0? Yes. Push d. n = n/d // n is 6, d is 2
n % d == 0? Yes. Push d. n = n/d // n is 3, d is 2
n % d == 0? No. d = d + 1 // n is 3, d is 3
n % d == 0? Yes. Push d. n = n/d // n is 1 so you're done

You probably know yourself that your algorithm is far from optimal. So this won't hurt the performance much. Replace
if(prime)
f.push(d);
with
if (prime)
{
for (int d1 = d; n % d1 == 0; d1 *= d)
f.push(d);
}

Simplest code for prime factorization:-
for ( int i = 2; i <= num; ++i )
{
while ( num % i == 0 )
{
num /= i;
std::cout << i << std::endl;
}
}

Related

Is there an efficient way to generate number of factors of N less than X?

I am a beginner in the field of programming. I just want to find the number of factors / divisors of a positive integer N less than X. (X itself is a factor of N). I have a naive approach which doesn't work good for queries on N,X.
Here is my approach
int Divisors(int n, int x) {
int ans = 0;
if (x < sqrt(n)) {
for (int i = 1; i < x; i++) {
if (n % i == 0) {
ans++;
}
}
} else
for (int i = 1; i <= sqrt(n); i++) {
if (n % i == 0) {
if (n / i == i && i < x)
ans++;
else {
if (i < x)
ans++;
if (n / i < x)
ans++;
}
}
}
return ans;
}
Is there some efficient way to do this? Kindly help me out!
The actual problem I'm trying to solve :
Given some M and N I need to iterate through all positive integers less than or equal to N(1 <= i <= N) and I need to count how many numbers less than the current number (i) exists such that they divide the last multiple of i that is less than or equal to M (i.e., M - M % i) and finally find the sum of all counts.
Example
Given N = 5 and M = 10
Ans : 6
Explanation :
i = 1 count = 0
i = 2 count = 1 (10 % 1 = 0)
i = 3 count = 1 (9 % 1 = 0)
i = 4 count = 2 (8 % 1 = 0, 8 % 2 = 0)
i = 5 count = 2 (10 % 1 = 0, 10 % 2 = 0)
Therefore sum of all counts = 6
The wording of the question is a bit confusing.
I'm assuming you are finding the size of the set of all factors/divisors, D, of a number n that are less than a number x, where x is a factor of n.
An easier way of doing this is to iterate from all numbers 1 through x, exclusive of x, and use the modulo operator %.
Code:
int NumOfDiv(int x, int n){
int count = 0;
for(int i=1; i<x; i++){
if(n % i == 0) //This indicates that i divides n, having a remainder of 0,
look up % as it is very useful with number theory
count++;
}
return count;
}
Example:
int TestNum = NumOfDiv(4,12)
TestNum would have the value of 3

Code Chef DSA Learning Series : Multiple of 3

Consider a very long K-digit number N with digits d0, d1, ..., dK-1 (in decimal notation; d0 is the most significant and dK-1 the least significant digit). This number is so large that we can't give it to you on the input explicitly; instead, you are only given its starting digits and a way to construct the remainder of the number.
Specifically, you are given d0 and d1; for each i ≥ 2, di is the sum of all preceding (more significant) digits, modulo 10 — more formally, the following formula must hold:
Determine if N is a multiple of 3.
Input
The first line of the input contains a single integer T denoting the number of test cases. The description of T test cases follows.
The first and only line of each test case contains three space-separated integers K, d0 and d1.
Output
For each test case, print a single line containing the string "YES" (without quotes) if the number N is a multiple of 3 or "NO" (without quotes) otherwise.
Constraints
1 ≤ T ≤ 1000
2 ≤ K ≤ 1012
1 ≤ d0 ≤ 9
0 ≤ d1 ≤ 9
Example
Input:
3
5 3 4
13 8 1
760399384224 5 1
Output:
NO
YES
YES
Explanation
Example case 1: The whole number N is 34748, which is not divisible by 3, so the answer is NO.
Example case 2: The whole number N is 8198624862486, which is divisible by 3, so the answer is YES.
Question Ended
In this question, in the example test case given, we have k=760399384224, d0=5, and d1=1. Now we know that a number is multiple of 3 if the sum of it’s digits is a multiple of 3. So applying it here, we separate the number n into 3 parts,
Part 1: First 3 digits -> 516 , (5+1+6) mod 3 ==0, so rem1 = 0.
Part 2: Next will be (k-3)/4 times repetition of (2486) ,or,rem2 = ((2+4+8+6)*((k-3)/4))%3= 1
Part 3: the last (k-3)%4 =1 digits which will be 2 (from 2486 repetition) , so rem3 = 2%3=2
So the final answer should be (rem1+rem2+rem3)%3
and I wrote the following code for this logic:
#include<iostream>
#define ll long long
using namespace std;
int main()
{
int t;
cin>>t;
while(t--)
{
ll k;
cin>>k;
int d0,d1;
cin>>d0>>d1;
int d2 = (d0+d1)%10;
int d[4];
d[0] = (d0+d1+d2)%10;
d[1] = (2*d[0])%10;
d[2] = (2*d[1])%10;
d[3] = (2*d[2])%10;
ll rem1 = (d0+d1+d2)%3,rem2,rem3=0;
rem2 = (20*(((k-3)/4)%3))%3;
ll x = (k-3)%4;
if(x!=0)
{
for(int i=0; i<x; ++i)
rem3+=d[i];
rem3 = rem3%3;
}
else
rem3 =0;
if(k==2)
{
rem1 = (d0+d1)%3;
rem2=0;
rem3=0;
}
if((rem1+rem2+rem3)%3==0)
cout<<"YES"<<endl;
else
cout<<"NO"<<endl;
}
}
Now they’re giving me WA in their test cases. And I cant think of a possible test cases which doesn’t work with this code. So somebody help me out please.
Here's an apparent mismatch. You can probably use this JavaScript code to find more:
function f(k, d0, d1){
let d2 = (d0+d1)%10;
let d = new Array(4).fill(0);
d[0] = (d0+d1+d2)%10;
d[1] = (2*d[0])%10;
d[2] = (2*d[1])%10;
d[3] = (2*d[2])%10;
let rem1 = (d0+d1+d2)%3, rem2, rem3=0;
rem2 = (20*((~~((k-3)/4))%3))%3;
let x = (k-3)%4;
if(x!=0)
{
for(let i=0; i<x; ++i)
rem3+=d[i];
rem3 = rem3%3;
}
else
rem3 =0;
if(k==2)
{
rem1 = (d0+d1)%3;
rem2=0;
rem3=0;
}
if((rem1+rem2+rem3)%3==0)
return true
return false
}
function bruteForce(k, d0, d1){
let d = (d0 + d1) % 10;
let n = 10 * d0 + d1;
for (let i=3; i<=k; i++){
n = 10 * n + d;
d = (2 * d) % 10;
}
return [n % 3 == 0, n];
}
let str = "";
str += "Examples:\n";
str += "(5, 3, 4)\n" + bruteForce(5, 3, 4) + "\n\n";
str += "(13, 8, 1)\n" + bruteForce(13, 8, 1) + "\n\n"
var k = 7;
var mismatch = false;
for (let i=1; i<10; i++){
for (let j=0; j<10; j++){
const _f = f(k, i, j);
const _bruteForce = bruteForce(k, i, j);
if (_bruteForce[0] != _f){
str += "Mismatch:\n" +
`(${ k }, ${ i }, ${ j })\n` +
"f: " + _f +
"\nbruteForce: " + _bruteForce + "\n\n";
mismatch = true;
}
if (mismatch)
break;
}
if (mismatch)
break;
}
console.log(str);
#include <stdio.h>
void isMulti3(long long int K, long long int d0, long long int d1) {
long long int mod1 = (d0 + d1) % 10;
long long int sum_mod1 = d0 + d1 + mod1;
long long int rep = (K-3)/4;
long long int mod2[4];
mod2[0] = (2*mod1) % 10;
mod2[1] = (4*mod1) % 10;
mod2[2] = (8*mod1) % 10;
mod2[3] = (6*mod1) % 10;
long long int sum_mod2 = 0;
for(int i = 0; i < 4; i++) {
sum_mod2 += mod2[i];
}
long long int unrep = (K - 3) % 4;
long long int sum_mod3 = 0;
for(int i = 0; i < unrep; i++) sum_mod3 += mod2[i];
long long int isMod1 = sum_mod1 % 3;
long long int isMod2 = ((rep%3)*(sum_mod2%3)) % 3;
long long int isMod3 = sum_mod3 % 3;
if((isMod1 + (isMod2 + isMod3)%3)% 3 == 0) printf("YES\n");
else printf("NO\n");
}
int main() {
int T;
scanf("%d", &T);
for(int i = 0; i < T; i++) {
long long int K;
long long int d0, d1;
scanf("%lld %lld %lld", &K, &d0, &d1);
if(K == 2) {
if((d0 + d1) % 3 == 0) printf("YES\n");
else printf("NO\n");
}
else isMulti3(K, d0, d1);
}
}
I think you are the same guy who asked the question on the codechef discussion portal.
I answered it there , and I am sharing the link.
[https://discuss.codechef.com/t/dsa-learning-series-multiple-of-3/77174/4?u=nazishkaunain][1]
So the point where you are mistaken is:
You might use the fact:
(a+b+c)mod 3 != a mod 3 + b mod 3 + c mod 3;
But (a + b + c) mod 3 = (a mod 3 + ( b mod 3 + c mod 3) mod 3) mod 3;
And a number n ( n = a + b + c) is divisible by 3 only if n mod 3 = 0 => (a + b + c) mod 3 = 0.

Lagrange's Four Square theorem under modulus

Given an integer n, print out all possible combinations of integer values of A,B,C and D which solve the equation A^2+B^2+C^2+D^2=N under modulus user given integer p
int sum(int n)
{
int a, b, c, d, na, nb, nc, nd;
int count = 0;
for (a = 0, na = n; a * a <= na; a++)
{
for (b = a, nb = na - a * a; b * b <= nb; b++)
{
for (c = b, nc = nb - b * b; c * c <= nc; c++) {
nd = nc - c * c;
d = sqrt (nd);
if (d * d == nd)
{
cout<< a<< b<< c<< d;
count++;
}
}
}
}
cout<<"Found solutions :"<< count;
}
I want the values of a,b,c,d to be between 0 to (p-1).
I want to output the respective 4 numbers when squared and summed under modulo P make that respective product modulo P
For eg - 1 x 2 x 3 x 4 x 5 mod 100 = 120 mod 100 = ( 8^2 + 6^2 + 4^2 + 2^2 ) mod 100
Note :value of p can be either prime or non-prime.
How do I reflect it in the above code ?

Condensing nested loops

I'm trying to convert these 3 loops:
for (a = 1; a < amax; a++) {
for (b = 1; b < bmax; b++) {
for (c = 1; c < cmax; c++) {
...
}
}
}
to a single loop.
I've tried this:
for (abc = 0; abc < (amax * bmax * cmax); ++abc)
{
a = abc / (bmax * cmax) + 1;
b = (abc % (bmax * cmax)) / cmax + 1;
c = (abc % (bmax * cmax)) % cmax + 1;
...
}
however it is not equivalent. Where's the logic error?
The a loop has amax-1 iterations, not amax iterations. Ditto for the b and c loops. So, the single loop should have (amax-1)*(bmax-1)*(cmax-1) iterations.
To extract the a, b and c values treat the single loop index as a mixed base number (the bases you multiplied to find the number of iterations), that is, simple integer division and remainder operations.
Add 1 to each of the resulting values.
Your first loop runs far less than second loop.
Imagine
int amax = 3;
int bmax = 3;
int cmax = 3;
Your first loop has 2, 2, 2 = 8 iterations.
Second loop will run through 0 to < (3 * 3* 3 = 27) i.e. 27 times
Also there are few more issues in the computation of a,b,c checkout the following/ Notice abc starts at 1 and the condition is <=:-
(Code in c - haven't touched c++ since ages)
int x = 1;
amax-=1;
bmax-=1;
cmax-=1;
int a = 1, b = 1, c = 1;
for (int abc = 1; abc <= (amax * bmax * cmax); ++abc)
{
c = abc % cmax;
c = c != 0 ? c : cmax;
var m = ' a='+a+' b='+b+' c='+c+' ::::'+(x++);
printf("%s\n", m);
a = abc < (bmax*cmax) || abc % ((bmax*cmax)) != 0 ? a : (a + 1) % amax;
a = a != 0 ? a : amax;
b = abc < cmax || abc % (cmax) != 0 ? b : (b + 1) % bmax;
b = b != 0 ? b : bmax;
}

For loop divide numbers

I'm an amateur when it comes to C++ but I've already received a task which is over my knowledge.
Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.
EDIT:(18.10.15)
Turns out I didn't understood my task correctly. Here it is:
"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."
For example, I enter 100 and 200. In this interval there's 153.
153 = 1^3 + 5^3 + 3^3 (1+125+27)
Programme shows 153.
cin >> n;
cin >> m;
for (int i=n; i<=m; i++)
{
for (int k=n; k<=i; k++)
{
a = n % 10; //for example, I enter 153, then a=3
f = n /= 10; //f=15
b = f % 10; //b=5
f = f /= 10; //f=1
c = f % 10; //c=1
f = f /= 10;
d = f % 10;
for (int j=1; j<=5; j++)
{
a = a * a;
b = b * b;
c = c * c;
d = d * d;
if (a + b + c + d == n)
{
cout << n << endl;
}
}
}
}
Any help will be appreciated.
Task is to enter numbers n,m. Programme must take it as an interval, in which it checks if there is any number which is a sum of numbers with the same exponent.
Assuming the range is given as [n, m), then here's your program:
return (n != m);
Any number can be seen as a sum of numbers with the same exponent. For example:
0 = 0 ^ 3 + 0 ^ 3 + 0 ^ 3
1 = 1 ^ 3 + 0 ^ 3
2 = 1 ^ 3 + 1 ^ 3
3 = 1 ^ 3 + 1 ^ 3 + 1 ^ 3
and so on. This is true even for negative numbers.
So in any non-empty range there exists at least 1 such number.
"All I know is how to get the programm to check each number separately"
"Programme must not use arrays."
for (int i = n; i <= m; i++) {
...
int x = (int)Math.log10(i);
int rest = i;
for (int p = x; p>=0; p--) {
int digit = rest / (int)Math.pow(10,p);
rest = i % (int)Math.pow(10,p);
//3802 = 3*10^3 + 8*10^2 + 0*10^1 + 2*10^0
}
}
...
Sorry, is Java no C++
Sorry that I answer so late and that I phrased my question poorly - English isn't my native language.
But turns out I didn't understood my task correctly. Here it is:
"User enter two numbers. Programme takes it as the interval in which it checks all the numbers. If there's a number in interval which all digit's sum of SAME exponent is that number, then programme shows it."
For example, I enter 100 and 200. In this interval there's 153.
153 = 1^3 + 5^3 + 3^3 (1+125+27)
Programme shows 153.
cin >> n;
cin >> m;
for (int i=n; i<=m; i++)
{
for (int k=n; k<=i; k++)
{
a = n % 10; //for example, I enter 153, then a=3
f = n /= 10; //f=15
b = f % 10; //b=5
f = f /= 10; //f=1
c = f % 10; //c=1
f = f /= 10;
d = f % 10;
for (int j=1; j<=5; j++)
{
a = a * a;
b = b * b;
c = c * c;
d = d * d;
if (a + b + c + d == n)
{
cout << n << endl;
}
}
}
}