+ 5
[C Language] prime factorization .. Example : How to make output 2 2 2 3 7 7 become 2^3 * 3^1 * 7^2 ?
[C Language] prime factorization Example : How to make output 2 2 2 3 7 7 become 2^3 * 3^1 * 7^2 ?
33 Rรฉponses
+ 5
Brianna, I tried improving Jayakrishna๐ฎ๐ณ's code to this.
https://code.sololearn.com/cNdg2MgRUZzc/?ref=app
+ 2
Wow, I just got the same question from a friend (not a friend actually, he's my junior). Just count the duplicates of it and print the number ^ duplicates * ...
Oh you're from Indonesia, that problem is for the weekly homework isn't it?
+ 2
Naisu, good luck.
For anyone else reading this and don't understand what we're talking about, it's just that the OP is in the same college as me (the OP is my junior).
+ 2
#include <stdio.h>
int main() {
int n,i;
scanf("%d",&n);
int count=0,tag=0, chr=0;
for(i=2;i<=n/2;i++)
{
while(n%i==0)
{
tag=1;
count++;
// printf("%d ",i); //comment this
n=n/i;
}
if(tag){
if(chr) printf("*"); //update
printf("%d^%d ",i,count);
count=0;
tag=0;
chr=1
}
}
return 0;
}
+ 1
If for loop, for i you can get values 4,6,8.. Also and those not primes and not n%i==0 so you should avoid those printing.. When n%i==0 set tag=1 and execute if statement otherwise set tag=0 to not execute if.. Check this by setting tag=1 always... You get incorrect answers.. Similar tag a tag for printing *. Comment inner printf. Hope it clears..!
+ 1
LastSecond959 I don't understand 'function' yet :(
+ 1
Take another variable as lik int chr=0;
In if clouse, add like
if(chr) printf("*");
Next In same if after printing a 1st output set chr=1;
So next for every output, * also gets printed..
Edit: Brianna
I updated code above, see
+ 1
Brianna I updated the code, there's no need to do a prime check after the most-inner for-loop since it also checks whether the N is prime or not. Logically said, if it is not a prime, N will be 1 after the for-loop, if not, it's obviously a prime, just print it (no need to check it because we already know it must be a prime).
+ 1
Brianna you're welcome...
+ 1
Brianna I'll explain my code in our language so you can easily understand (hopefully).
Di penjelasan ini gw bakal cuekin testcase ya, jd klo gw blg for-loop itu mksdnya for-loop yg di dlm testcase (yg y=2).
Jd for-loop itu bakal ngambil faktorisasi prima dari si N & secara bersamaan ngecek apakah N itu prima ato bkn. Kalo N itu dari awal bkn prima, dia pasti habis dibagi sama si for-loop yg artinya di akhir N pasti udh jd 1. Kalo dia dari awal prima atau berakhir jd prima, tinggal print aja angka itu. Itulah knp gw taro `if (N>1)`, krn 1 itu penanda klo dia "habis dibagi". Klo bingung codingan gw, chat aja (pm), jgn di sini.
+ 1
LastSecond959 ok..ngerti. Makasihh
+ 1
๐๐๐ง ๐๐ง๐ฒ๐จ๐ง๐ ๐๐ฅ๐๐๐ซ ๐ฆ๐ ๐ฐ๐ก๐๐ญ ๐ข๐ฌ ๐๐ซ๐ ๐ฎ๐ฆ๐๐ง๐ญ๐ฌ ๐๐ง๐ ๐ฉ๐๐ซ๐๐ฆ๐๐ญ๐๐ซ๐ฌ ๐ข๐ง ๐.
+ 1
devanille thank you :))
0
LastSecond959 omg..ketemu kating
0
B24?
0
Iya
0
LastSecond959 how to make array from output?
0
Use DM for chat pls..
Is it how to make array from output? Or how to make output from array?
Required sample output?
0
Jayakrishna๐ฎ๐ณ make array from output i think. This is prime factorization, i already find how to solve the prime factorization. Example: prime factorization from 1176 is 2 2 2 3 7 7. How to change it into 2^3 * 3^1 * 7^2? I don't know how to make duplicate from the output
0
1176/2 = 588
588/2 = 294
294/2 = 147
147/3 = 49
49/7 = 7
7/7 =1
(7*7) * (3) * (2*2*2) = 1176
All factors are primes..
Brianna ?