Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
sol1.c File Reference

Problem 3 solution More...

#include <math.h>
#include <stdio.h>
Include dependency graph for sol1.c:

Functions

char isprime (int no)
 Check if the given number is prime.
 
int main ()
 Main function.
 

Detailed Description

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.

Function Documentation

◆ isprime()

char isprime ( int  no)

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()

int main ( void  )

Main function.

40{
41 int maxNumber = 0;
42 int n = 0;
43 int n1;
44 scanf("%d", &n);
45 if (isprime(n) == 1)
46 printf("%d", n);
47 else
48 {
49 while (n % 2 == 0)
50 {
51 n = n / 2;
52 }
53 if (isprime(n) == 1)
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 {
64 if (isprime((int)(n / i)) == 1)
65 {
66 maxNumber = n / i;
67 break;
68 }
69 else if (isprime(i) == 1)
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
Here is the call graph for this function: