-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path279.perfect-squares-math.c
58 lines (46 loc) · 1011 Bytes
/
279.perfect-squares-math.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
/**
279. Perfect Squares
Given a positive integer n, find the least number of perfect square numbers (for example, 1, 4, 9, 16, ...) which head to n.
Example 1:
Input: n = 12
Output: 3
Explanation: 12 = 4 + 4 + 4.
Example 2:
Input: n = 13
Output: 2
Explanation: 13 = 4 + 9.
*/
// gcc 279.perfect-squares-math.c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define SIZE 1024
int isSquare(int n)
{
int tmp = (int)sqrt((double)n);
return n == tmp * tmp;
}
int numSquares(int n)
{
// four-square and three-square theorems.
while (n % 4 == 0)
n /= 4;
if (n % 8 == 7)
return 4;
if (isSquare(n))
return 1;
// enumeration to check if the number can be decomposed into sum of two squares.
for (int i = 1; i * i <= n; ++i)
{
if (isSquare(n - i * i))
return 2;
}
// bottom case of three-square theorem.
return 3;
}
int main()
{
int res = numSquares(29);
printf("res: %d\n", res);
return 0;
}