-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmath.c
62 lines (48 loc) · 1.05 KB
/
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
59
60
61
62
// Licensed under the MIT License.
#include <math.h>
#include <stdarg.h>
#include "euler.h"
long math_natural_sum(long n)
{
return n * (n + 1) / 2;
}
long long math_reverse(long long n)
{
long long x = 0;
for (long long y = n; y; y /= 10)
{
x = (x * 10) + y % 10;
}
return x;
}
bool math_is_palindrome(long long n)
{
return n == math_reverse(n);
}
bool math_is_polygonal(int s, long x, long* approxN)
{
double sM2 = s - 2;
double sM4 = sM2 - 2;
double n = (sqrt(8 * sM2 * x + sM4 * sM4) + sM4) / (2 * sM2);
long localApproxN = n;
if (approxN)
{
*approxN = localApproxN;
}
return n == localApproxN;
}
long long math_length(long long b, long long a)
{
return 1 + a * log10(b);
}
unsigned long long math_concat_impl(int left, int right, ...)
{
unsigned long long result = left;
va_list argl;
for (va_start(argl, right); right; right = va_arg(argl, int))
{
result = result * pow(10, math_length(right, 1)) + right;
}
va_end(argl);
return result;
}