+ 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 ?

18th Oct 2020, 10:17 AM
Briana
Briana - avatar
33 Antworten
+ 5
18th Oct 2020, 2:43 PM
LastSecond959
LastSecond959 - avatar
+ 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?
18th Oct 2020, 10:26 AM
LastSecond959
LastSecond959 - avatar
+ 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).
18th Oct 2020, 10:31 AM
LastSecond959
LastSecond959 - avatar
+ 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; }
18th Oct 2020, 2:18 PM
Jayakrishna 🇮🇳
+ 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..!
18th Oct 2020, 2:32 PM
Jayakrishna 🇮🇳
+ 1
LastSecond959 I don't understand 'function' yet :(
18th Oct 2020, 2:54 PM
Briana
Briana - avatar
+ 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
18th Oct 2020, 3:01 PM
Jayakrishna 🇮🇳
+ 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).
18th Oct 2020, 3:03 PM
LastSecond959
LastSecond959 - avatar
+ 1
Brianna you're welcome...
18th Oct 2020, 3:09 PM
Jayakrishna 🇮🇳
+ 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.
18th Oct 2020, 3:15 PM
LastSecond959
LastSecond959 - avatar
+ 1
LastSecond959 ok..ngerti. Makasihh
18th Oct 2020, 3:26 PM
Briana
Briana - avatar
+ 1
𝐂𝐚𝐧 𝐚𝐧𝐲𝐨𝐧𝐞 𝐜𝐥𝐞𝐚𝐫 𝐦𝐞 𝐰𝐡𝐚𝐭 𝐢𝐬 𝐚𝐫𝐠𝐮𝐦𝐞𝐧𝐭𝐬 𝐚𝐧𝐝 𝐩𝐚𝐫𝐚𝐦𝐞𝐭𝐞𝐫𝐬 𝐢𝐧 𝐂.
19th Oct 2020, 5:27 PM
T.K.SANTHOSH
T.K.SANTHOSH - avatar
+ 1
devanille thank you :))
20th Oct 2020, 11:06 AM
Briana
Briana - avatar
0
LastSecond959 omg..ketemu kating
18th Oct 2020, 10:28 AM
Briana
Briana - avatar
0
B24?
18th Oct 2020, 10:28 AM
LastSecond959
LastSecond959 - avatar
0
Iya
18th Oct 2020, 10:29 AM
Briana
Briana - avatar
0
LastSecond959 how to make array from output?
18th Oct 2020, 11:17 AM
Briana
Briana - avatar
0
Use DM for chat pls.. Is it how to make array from output? Or how to make output from array? Required sample output?
18th Oct 2020, 12:30 PM
Jayakrishna 🇮🇳
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
18th Oct 2020, 1:11 PM
Briana
Briana - avatar
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 ?
18th Oct 2020, 1:20 PM
Jayakrishna 🇮🇳