Problem 3 solution
More...
#include <math.h>
#include <stdio.h>
|
char | isprime (int no) |
| Check if the given number is prime.
|
|
int | main () |
| Main function.
|
|
Problem 3 solution
Problem:
The prime factors of 13195 are 5,7,13 and 29. What is the largest prime factor of a given number N? e.g. for 10, largest prime factor = 5. For 17, largest prime factor = 17.
◆ isprime()
Check if the given number is prime.
16{
17 int sq;
18
19 if (no == 2)
20 {
21 return 1;
22 }
23 else if (no % 2 == 0)
24 {
25 return 0;
26 }
27 sq = ((int)(sqrt(no))) + 1;
28 for (int i = 3; i < sq; i += 2)
29 {
30 if (no % i == 0)
31 {
32 return 0;
33 }
34 }
35 return 1;
36}
◆ main()
Main function.
40{
41 int maxNumber = 0;
42 int n = 0;
43 int n1;
44 scanf("%d", &n);
46 printf("%d", n);
47 else
48 {
49 while (n % 2 == 0)
50 {
51 n = n / 2;
52 }
54 {
55 printf("%d\n", n);
56 }
57 else
58 {
59 n1 = ((int)(sqrt(n))) + 1;
60 for (int i = 3; i < n1; i += 2)
61 {
62 if (n % i == 0)
63 {
65 {
66 maxNumber = n / i;
67 break;
68 }
70 {
71 maxNumber = i;
72 }
73 }
74 }
75 printf("%d\n", maxNumber);
76 }
77 }
78 return 0;
79}
char isprime(int no)
Check if the given number is prime.
Definition sol1.c:15