almost lost everything there :/

This commit is contained in:
ldlda committed 2024-10-24 02:34:49 +07:00
1 parent 2930b7217d
commit ad3ce0bd48
6 files changed
+217 -6

No files matched your search

+36
View File
@@ -0,0 +1,36 @@
#include <stdio.h>
int calculate_iter(int n)
{
int c = 0;
while (n > 0)
{
c += n % 10;
n /= 10;
}
return c;
}
int calculate_recur(int n)
{
if (n <= 0)
return 0;
return n % 10 + calculate_iter(n / 10);
}
// both complexity O(logn)
int main(int argc, char const *argv[])
{
int w;
printf("number: ");
scanf("%d", &w);
int a = calculate_iter(w);
int b = calculate_recur(w);
if (a != b)
printf("oops! %d != %d\n", a, b);
else
printf(" yay! %d == %d\n", a, b);
// main(argc, argv);
return 0;
}
+53
View File
@@ -0,0 +1,53 @@
#include <stdio.h>
#include <stdbool.h>
bool _sphenic(int num, int cnt, int pr)
{
// Base case: If we have already found 3 distinct primes, check if the number is fully divided.
if (cnt == 3)
return num == 1;
// Try dividing the number by the next prime factor.
while (++pr <= num)
{
if (num % pr == 0)
{
num /= pr;
// If the same prime factor divides it again, it's not sphenic.
if (num % pr == 0)
return false;
// Recursively check for the next prime and increase the count of distinct primes.
return _sphenic(num, cnt + 1, pr);
}
}
// If we run out of primes without finding 3 distinct ones, it's not sphenic.
return false;
}
bool rsphenic(int n)
{
// Sphenic numbers are always greater than or equal to 30.
if (n < 30)
return false;
// Initialize the recursion to start with prime checking from 2.
return _sphenic(n, 0, 1);
}
int main()
{
int N = 200;
for (int i = 1; i < N; i++)
{
// printf("%d\n", i);
if (rsphenic(i))
printf("%d ", i);
}
return 0;
}