Fcuked up
This commit is contained in:
parent
96f749c28f
commit
3818e2252b
|
@ -1,42 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_iterative_factorial.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 16:54:02 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/31 22:39:15 by tischmid ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
int ft_iterative_factorial(int nb)
|
|
||||||
{
|
|
||||||
int fact;
|
|
||||||
|
|
||||||
if (nb < 0)
|
|
||||||
return (0);
|
|
||||||
fact = 1;
|
|
||||||
while (--nb > 0)
|
|
||||||
fact *= (nb + 1);
|
|
||||||
return (fact);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("-10: %d\n", ft_iterative_factorial(-10));
|
|
||||||
printf("-1: %d\n", ft_iterative_factorial(-1));
|
|
||||||
printf("0: %d\n", ft_iterative_factorial(0));
|
|
||||||
printf("1: %d\n", ft_iterative_factorial(1));
|
|
||||||
printf("2: %d\n", ft_iterative_factorial(2));
|
|
||||||
printf("3: %d\n", ft_iterative_factorial(3));
|
|
||||||
printf("4: %d\n", ft_iterative_factorial(4));
|
|
||||||
printf("5: %d\n", ft_iterative_factorial(5));
|
|
||||||
printf("10: %d\n", ft_iterative_factorial(10));
|
|
||||||
printf("15: %d\n", ft_iterative_factorial(15));
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,37 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_recursive_factorial.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 16:54:29 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 16:54:30 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
int ft_recursive_factorial(int nb)
|
|
||||||
{
|
|
||||||
if (nb < 1)
|
|
||||||
return (nb == 0);
|
|
||||||
return (nb * ft_recursive_factorial(nb - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("-10: %d\n", ft_recursive_factorial(-10));
|
|
||||||
printf("-1: %d\n", ft_recursive_factorial(-1));
|
|
||||||
printf("0: %d\n", ft_recursive_factorial(0));
|
|
||||||
printf("1: %d\n", ft_recursive_factorial(1));
|
|
||||||
printf("2: %d\n", ft_recursive_factorial(2));
|
|
||||||
printf("3: %d\n", ft_recursive_factorial(3));
|
|
||||||
printf("4: %d\n", ft_recursive_factorial(4));
|
|
||||||
printf("5: %d\n", ft_recursive_factorial(5));
|
|
||||||
printf("10: %d\n", ft_recursive_factorial(10));
|
|
||||||
printf("15: %d\n", ft_recursive_factorial(15));
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,95 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_iterative_power.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 16:55:32 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 16:55:33 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
int ft_iterative_power(int nb, int power)
|
|
||||||
{
|
|
||||||
int res;
|
|
||||||
|
|
||||||
res = 1;
|
|
||||||
if (power < 1)
|
|
||||||
return (power == 0);
|
|
||||||
while (power--)
|
|
||||||
res *= nb;
|
|
||||||
return (res);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("base: <-3>, power <-2>, result: <%d>\n", ft_iterative_power(-3, -2));
|
|
||||||
printf("base: <-2>, power <-2>, result: <%d>\n", ft_iterative_power(-2, -2));
|
|
||||||
printf("base: <-1>, power <-2>, result: <%d>\n", ft_iterative_power(-1, -2));
|
|
||||||
printf("base: <0>, power <-2>, result: <%d>\n", ft_iterative_power(0, -2));
|
|
||||||
printf("base: <1>, power <-2>, result: <%d>\n", ft_iterative_power(1, -2));
|
|
||||||
printf("base: <2>, power <-2>, result: <%d>\n", ft_iterative_power(2, -2));
|
|
||||||
printf("base: <3>, power <-2>, result: <%d>\n", ft_iterative_power(3, -2));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <-1>, result: <%d>\n", ft_iterative_power(-3, -1));
|
|
||||||
printf("base: <-2>, power <-1>, result: <%d>\n", ft_iterative_power(-2, -1));
|
|
||||||
printf("base: <-1>, power <-1>, result: <%d>\n", ft_iterative_power(-1, -1));
|
|
||||||
printf("base: <0>, power <-1>, result: <%d>\n", ft_iterative_power(0, -1));
|
|
||||||
printf("base: <1>, power <-1>, result: <%d>\n", ft_iterative_power(1, -1));
|
|
||||||
printf("base: <2>, power <-1>, result: <%d>\n", ft_iterative_power(2, -1));
|
|
||||||
printf("base: <3>, power <-1>, result: <%d>\n", ft_iterative_power(3, -1));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power: <0>, result: <%d>\n", ft_iterative_power(-3, 0));
|
|
||||||
printf("base: <-2>, power <0>, result: <%d>\n", ft_iterative_power(-2, 0));
|
|
||||||
printf("base: <-1>, power <0>, result: <%d>\n", ft_iterative_power(-1, 0));
|
|
||||||
printf("base: <0>, power <0>, result: <%d>\n", ft_iterative_power(0, 0));
|
|
||||||
printf("base: <1>, power <0>, result: <%d>\n", ft_iterative_power(1, 0));
|
|
||||||
printf("base: <2>, power <0>, result: <%d>\n", ft_iterative_power(2, 0));
|
|
||||||
printf("base: <3>, power <0>, result: <%d>\n", ft_iterative_power(3, 0));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <1>, result: <%d>\n", ft_iterative_power(-3, 1));
|
|
||||||
printf("base: <-2>, power <1>, result: <%d>\n", ft_iterative_power(-2, 1));
|
|
||||||
printf("base: <-1>, power <1>, result: <%d>\n", ft_iterative_power(-1, 1));
|
|
||||||
printf("base: <0>, power <1>, result: <%d>\n", ft_iterative_power(0, 1));
|
|
||||||
printf("base: <1>, power <1>, result: <%d>\n", ft_iterative_power(1, 1));
|
|
||||||
printf("base: <2>, power <1>, result: <%d>\n", ft_iterative_power(2, 1));
|
|
||||||
printf("base: <3>, power <1>, result: <%d>\n", ft_iterative_power(3, 1));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <2>, result: <%d>\n", ft_iterative_power(-3, 2));
|
|
||||||
printf("base: <-2>, power <2>, result: <%d>\n", ft_iterative_power(-2, 2));
|
|
||||||
printf("base: <-1>, power <2>, result: <%d>\n", ft_iterative_power(-1, 2));
|
|
||||||
printf("base: <0>, power <2>, result: <%d>\n", ft_iterative_power(0, 2));
|
|
||||||
printf("base: <1>, power <2>, result: <%d>\n", ft_iterative_power(1, 2));
|
|
||||||
printf("base: <2>, power <2>, result: <%d>\n", ft_iterative_power(2, 2));
|
|
||||||
printf("base: <3>, power <2>, result: <%d>\n", ft_iterative_power(3, 2));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <3>, result: <%d>\n", ft_iterative_power(-3, 3));
|
|
||||||
printf("base: <-2>, power <3>, result: <%d>\n", ft_iterative_power(-2, 3));
|
|
||||||
printf("base: <-1>, power <3>, result: <%d>\n", ft_iterative_power(-1, 3));
|
|
||||||
printf("base: <0>, power <3>, result: <%d>\n", ft_iterative_power(0, 3));
|
|
||||||
printf("base: <1>, power <3>, result: <%d>\n", ft_iterative_power(1, 3));
|
|
||||||
printf("base: <2>, power <3>, result: <%d>\n", ft_iterative_power(2, 3));
|
|
||||||
printf("base: <3>, power <3>, result: <%d>\n", ft_iterative_power(3, 3));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <10>, result: <%d>\n", ft_iterative_power(-3, 10));
|
|
||||||
printf("base: <-2>, power <10>, result: <%d>\n", ft_iterative_power(-2, 10));
|
|
||||||
printf("base: <-1>, power <10>, result: <%d>\n", ft_iterative_power(-1, 10));
|
|
||||||
printf("base: <0>, power <10>, result: <%d>\n", ft_iterative_power(0, 10));
|
|
||||||
printf("base: <1>, power <10>, result: <%d>\n", ft_iterative_power(1, 10));
|
|
||||||
printf("base: <2>, power <10>, result: <%d>\n", ft_iterative_power(2, 10));
|
|
||||||
printf("base: <3>, power <10>, result: <%d>\n", ft_iterative_power(3, 10));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,90 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_recursive_power.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 16:56:00 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 16:56:01 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
int ft_recursive_power(int nb, int power)
|
|
||||||
{
|
|
||||||
if (power < 1)
|
|
||||||
return (power == 0);
|
|
||||||
return (nb * ft_recursive_power(nb, power - 1));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("base: <-3>, power <-2>, result: <%d>\n", ft_recursive_power(-3, -2));
|
|
||||||
printf("base: <-2>, power <-2>, result: <%d>\n", ft_recursive_power(-2, -2));
|
|
||||||
printf("base: <-1>, power <-2>, result: <%d>\n", ft_recursive_power(-1, -2));
|
|
||||||
printf("base: <0>, power <-2>, result: <%d>\n", ft_recursive_power(0, -2));
|
|
||||||
printf("base: <1>, power <-2>, result: <%d>\n", ft_recursive_power(1, -2));
|
|
||||||
printf("base: <2>, power <-2>, result: <%d>\n", ft_recursive_power(2, -2));
|
|
||||||
printf("base: <3>, power <-2>, result: <%d>\n", ft_recursive_power(3, -2));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <-1>, result: <%d>\n", ft_recursive_power(-3, -1));
|
|
||||||
printf("base: <-2>, power <-1>, result: <%d>\n", ft_recursive_power(-2, -1));
|
|
||||||
printf("base: <-1>, power <-1>, result: <%d>\n", ft_recursive_power(-1, -1));
|
|
||||||
printf("base: <0>, power <-1>, result: <%d>\n", ft_recursive_power(0, -1));
|
|
||||||
printf("base: <1>, power <-1>, result: <%d>\n", ft_recursive_power(1, -1));
|
|
||||||
printf("base: <2>, power <-1>, result: <%d>\n", ft_recursive_power(2, -1));
|
|
||||||
printf("base: <3>, power <-1>, result: <%d>\n", ft_recursive_power(3, -1));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power: <0>, result: <%d>\n", ft_recursive_power(-3, 0));
|
|
||||||
printf("base: <-2>, power <0>, result: <%d>\n", ft_recursive_power(-2, 0));
|
|
||||||
printf("base: <-1>, power <0>, result: <%d>\n", ft_recursive_power(-1, 0));
|
|
||||||
printf("base: <0>, power <0>, result: <%d>\n", ft_recursive_power(0, 0));
|
|
||||||
printf("base: <1>, power <0>, result: <%d>\n", ft_recursive_power(1, 0));
|
|
||||||
printf("base: <2>, power <0>, result: <%d>\n", ft_recursive_power(2, 0));
|
|
||||||
printf("base: <3>, power <0>, result: <%d>\n", ft_recursive_power(3, 0));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <1>, result: <%d>\n", ft_recursive_power(-3, 1));
|
|
||||||
printf("base: <-2>, power <1>, result: <%d>\n", ft_recursive_power(-2, 1));
|
|
||||||
printf("base: <-1>, power <1>, result: <%d>\n", ft_recursive_power(-1, 1));
|
|
||||||
printf("base: <0>, power <1>, result: <%d>\n", ft_recursive_power(0, 1));
|
|
||||||
printf("base: <1>, power <1>, result: <%d>\n", ft_recursive_power(1, 1));
|
|
||||||
printf("base: <2>, power <1>, result: <%d>\n", ft_recursive_power(2, 1));
|
|
||||||
printf("base: <3>, power <1>, result: <%d>\n", ft_recursive_power(3, 1));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <2>, result: <%d>\n", ft_recursive_power(-3, 2));
|
|
||||||
printf("base: <-2>, power <2>, result: <%d>\n", ft_recursive_power(-2, 2));
|
|
||||||
printf("base: <-1>, power <2>, result: <%d>\n", ft_recursive_power(-1, 2));
|
|
||||||
printf("base: <0>, power <2>, result: <%d>\n", ft_recursive_power(0, 2));
|
|
||||||
printf("base: <1>, power <2>, result: <%d>\n", ft_recursive_power(1, 2));
|
|
||||||
printf("base: <2>, power <2>, result: <%d>\n", ft_recursive_power(2, 2));
|
|
||||||
printf("base: <3>, power <2>, result: <%d>\n", ft_recursive_power(3, 2));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <3>, result: <%d>\n", ft_recursive_power(-3, 3));
|
|
||||||
printf("base: <-2>, power <3>, result: <%d>\n", ft_recursive_power(-2, 3));
|
|
||||||
printf("base: <-1>, power <3>, result: <%d>\n", ft_recursive_power(-1, 3));
|
|
||||||
printf("base: <0>, power <3>, result: <%d>\n", ft_recursive_power(0, 3));
|
|
||||||
printf("base: <1>, power <3>, result: <%d>\n", ft_recursive_power(1, 3));
|
|
||||||
printf("base: <2>, power <3>, result: <%d>\n", ft_recursive_power(2, 3));
|
|
||||||
printf("base: <3>, power <3>, result: <%d>\n", ft_recursive_power(3, 3));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
printf("base: <-3>, power <10>, result: <%d>\n", ft_recursive_power(-3, 10));
|
|
||||||
printf("base: <-2>, power <10>, result: <%d>\n", ft_recursive_power(-2, 10));
|
|
||||||
printf("base: <-1>, power <10>, result: <%d>\n", ft_recursive_power(-1, 10));
|
|
||||||
printf("base: <0>, power <10>, result: <%d>\n", ft_recursive_power(0, 10));
|
|
||||||
printf("base: <1>, power <10>, result: <%d>\n", ft_recursive_power(1, 10));
|
|
||||||
printf("base: <2>, power <10>, result: <%d>\n", ft_recursive_power(2, 10));
|
|
||||||
printf("base: <3>, power <10>, result: <%d>\n", ft_recursive_power(3, 10));
|
|
||||||
printf("\n");
|
|
||||||
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,60 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_fibonacci.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 00:36:21 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 00:36:22 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <stdlib.h>
|
|
||||||
#define NUM_TYPE int
|
|
||||||
|
|
||||||
NUM_TYPE memoized_fibonacci(unsigned int index, NUM_TYPE *cache)
|
|
||||||
{
|
|
||||||
if (cache[index] == (NUM_TYPE) -1)
|
|
||||||
cache[index] = memoized_fibonacci(index - 1, cache)
|
|
||||||
+ memoized_fibonacci(index - 2, cache);
|
|
||||||
return (cache[index]);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_fibonacci(int index)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
NUM_TYPE *cache;
|
|
||||||
NUM_TYPE fibn;
|
|
||||||
|
|
||||||
if (index < 0)
|
|
||||||
return (-1);
|
|
||||||
cache = malloc(sizeof(NUM_TYPE) * (index + 1));
|
|
||||||
cache[0] = 0;
|
|
||||||
cache[1] = 1;
|
|
||||||
i = 2;
|
|
||||||
while (i < index + 1)
|
|
||||||
cache[i++] = (NUM_TYPE)(-1);
|
|
||||||
fibn = memoized_fibonacci((unsigned int) index, cache);
|
|
||||||
i = 0;
|
|
||||||
free(cache);
|
|
||||||
return (fibn);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
#define UNSIGNED_LONG_LONG_MAX_FIB 93
|
|
||||||
#define INT_MAX_FIB 46
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = -12;
|
|
||||||
while (++i <= INT_MAX_FIB + 1)
|
|
||||||
printf("%d-th fib num: %d\n", i, ft_fibonacci(i));
|
|
||||||
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,64 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_sqrt.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 01:42:42 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 01:42:45 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#define INT_MAX_SQRT 46340
|
|
||||||
|
|
||||||
int ft_sqrt(int nb)
|
|
||||||
{
|
|
||||||
int sqrt;
|
|
||||||
int old_sqrt;
|
|
||||||
int max;
|
|
||||||
int min;
|
|
||||||
|
|
||||||
min = 0;
|
|
||||||
max = nb;
|
|
||||||
if (nb > INT_MAX_SQRT)
|
|
||||||
max = INT_MAX_SQRT;
|
|
||||||
sqrt = max;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
old_sqrt = sqrt;
|
|
||||||
if (sqrt * sqrt == nb)
|
|
||||||
return (sqrt);
|
|
||||||
if (sqrt * sqrt > nb)
|
|
||||||
max = sqrt;
|
|
||||||
if (sqrt * sqrt < nb)
|
|
||||||
min = sqrt;
|
|
||||||
sqrt = (max + min) / 2;
|
|
||||||
if (sqrt == old_sqrt)
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
printf("%d, %d\n", 0, ft_sqrt(0));
|
|
||||||
i = -20;
|
|
||||||
while (++i <= 4000)
|
|
||||||
if (ft_sqrt(i))
|
|
||||||
printf("%d, %d\n", i, ft_sqrt(i));
|
|
||||||
printf("%d, %d\n", INT_MIN, ft_sqrt(INT_MIN));
|
|
||||||
printf("%d, %d\n", INT_MAX, ft_sqrt(INT_MAX));
|
|
||||||
i = INT_MAX;
|
|
||||||
while (--i > INT_MAX - 1000000)
|
|
||||||
if (ft_sqrt(i))
|
|
||||||
printf("%d, %d\n", i, ft_sqrt(i));
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,116 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_is_prime.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 16:48:48 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 16:48:49 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#define INT_MAX_SQRT 46340
|
|
||||||
#define FIRST_PRIMES_LEN 2
|
|
||||||
#define SIEVE_LEN 2
|
|
||||||
#define WHEEL 6
|
|
||||||
// #define FIRST_PRIMES_LEN 3
|
|
||||||
// #define SIEVE_LEN 8
|
|
||||||
// #define WHEEL 30
|
|
||||||
|
|
||||||
int ft_int_sqrt(int nb)
|
|
||||||
{
|
|
||||||
int sqrt;
|
|
||||||
int old_sqrt;
|
|
||||||
int max;
|
|
||||||
int min;
|
|
||||||
|
|
||||||
min = 0;
|
|
||||||
max = nb;
|
|
||||||
if (nb > INT_MAX_SQRT)
|
|
||||||
max = INT_MAX_SQRT;
|
|
||||||
sqrt = max;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
old_sqrt = sqrt;
|
|
||||||
if (sqrt * sqrt == nb)
|
|
||||||
return (sqrt);
|
|
||||||
if (sqrt * sqrt > nb)
|
|
||||||
max = sqrt;
|
|
||||||
if (sqrt * sqrt < nb)
|
|
||||||
min = sqrt;
|
|
||||||
sqrt = (max + min) / 2;
|
|
||||||
if (sqrt == old_sqrt)
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
return (sqrt);
|
|
||||||
}
|
|
||||||
|
|
||||||
int first_divisor(int nb)
|
|
||||||
{
|
|
||||||
int wheel;
|
|
||||||
int idx;
|
|
||||||
int int_sqrt;
|
|
||||||
int first_primes[FIRST_PRIMES_LEN];
|
|
||||||
int sieve6[SIEVE_LEN];
|
|
||||||
|
|
||||||
first_primes[0] = 2;
|
|
||||||
first_primes[1] = 3;
|
|
||||||
sieve6[0] = 5;
|
|
||||||
sieve6[1] = 7;
|
|
||||||
idx = -1;
|
|
||||||
while (++idx < FIRST_PRIMES_LEN)
|
|
||||||
if (nb % first_primes[idx] == 0)
|
|
||||||
return (first_primes[idx]);
|
|
||||||
int_sqrt = ft_int_sqrt(nb);
|
|
||||||
wheel = 0;
|
|
||||||
while (wheel < int_sqrt)
|
|
||||||
{
|
|
||||||
idx = -1;
|
|
||||||
while (++idx < SIEVE_LEN)
|
|
||||||
if (nb % (sieve6[idx] + wheel) == 0)
|
|
||||||
return (sieve6[idx] + wheel);
|
|
||||||
wheel += WHEEL;
|
|
||||||
}
|
|
||||||
return (nb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_is_prime(int nb)
|
|
||||||
{
|
|
||||||
if (nb < 2)
|
|
||||||
return (0);
|
|
||||||
return (first_divisor(nb) == nb);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
int prime_count;
|
|
||||||
|
|
||||||
prime_count = 0;
|
|
||||||
i = -10;
|
|
||||||
while (++i <= INT_MAX / 1000)
|
|
||||||
{
|
|
||||||
if (ft_is_prime(i))
|
|
||||||
{
|
|
||||||
// printf("Prime: %d\n", i);
|
|
||||||
++prime_count;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
printf("Prime count up until %d: %d\n", INT_MAX / 1000, prime_count);
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
// first_primes[2] = 5;
|
|
||||||
// sieve30[0] = 7;
|
|
||||||
// sieve30[1] = 11;
|
|
||||||
// sieve30[2] = 13;
|
|
||||||
// sieve30[3] = 17;
|
|
||||||
// sieve30[4] = 19;
|
|
||||||
// sieve30[5] = 23;
|
|
||||||
// sieve30[6] = 29;
|
|
||||||
// sieve30[7] = 31;
|
|
||||||
*/ ////
|
|
|
@ -1,126 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_find_next_prime.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 17:03:18 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 17:03:19 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#define INT_MAX_SQRT 46340
|
|
||||||
#define FIRST_PRIMES_LEN 2
|
|
||||||
#define SIEVE_LEN 2
|
|
||||||
#define WHEEL 6
|
|
||||||
|
|
||||||
int ft_int_sqrt(int nb)
|
|
||||||
{
|
|
||||||
int sqrt;
|
|
||||||
int old_sqrt;
|
|
||||||
int max;
|
|
||||||
int min;
|
|
||||||
|
|
||||||
min = 0;
|
|
||||||
max = nb;
|
|
||||||
if (nb > INT_MAX_SQRT)
|
|
||||||
max = INT_MAX_SQRT;
|
|
||||||
sqrt = max;
|
|
||||||
while (1)
|
|
||||||
{
|
|
||||||
old_sqrt = sqrt;
|
|
||||||
if (sqrt * sqrt == nb)
|
|
||||||
return (sqrt);
|
|
||||||
if (sqrt * sqrt > nb)
|
|
||||||
max = sqrt;
|
|
||||||
if (sqrt * sqrt < nb)
|
|
||||||
min = sqrt;
|
|
||||||
sqrt = (max + min) / 2;
|
|
||||||
if (sqrt == old_sqrt)
|
|
||||||
break ;
|
|
||||||
}
|
|
||||||
return (sqrt);
|
|
||||||
}
|
|
||||||
|
|
||||||
int first_divisor(int nb)
|
|
||||||
{
|
|
||||||
int wheel;
|
|
||||||
int idx;
|
|
||||||
int int_sqrt;
|
|
||||||
int first_primes[FIRST_PRIMES_LEN];
|
|
||||||
int sieve6[SIEVE_LEN];
|
|
||||||
|
|
||||||
first_primes[0] = 2;
|
|
||||||
first_primes[1] = 3;
|
|
||||||
sieve6[0] = 5;
|
|
||||||
sieve6[1] = 7;
|
|
||||||
idx = -1;
|
|
||||||
while (++idx < FIRST_PRIMES_LEN)
|
|
||||||
if (nb % first_primes[idx] == 0)
|
|
||||||
return (first_primes[idx]);
|
|
||||||
int_sqrt = ft_int_sqrt(nb);
|
|
||||||
wheel = 0;
|
|
||||||
while (wheel < int_sqrt)
|
|
||||||
{
|
|
||||||
idx = -1;
|
|
||||||
while (++idx < SIEVE_LEN)
|
|
||||||
if (nb % (sieve6[idx] + wheel) == 0)
|
|
||||||
return (sieve6[idx] + wheel);
|
|
||||||
wheel += WHEEL;
|
|
||||||
}
|
|
||||||
return (nb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_is_prime(int nb)
|
|
||||||
{
|
|
||||||
if (nb < 2)
|
|
||||||
return (0);
|
|
||||||
return (first_divisor(nb) == nb);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_find_next_prime(int nb)
|
|
||||||
{
|
|
||||||
--nb;
|
|
||||||
while (++nb)
|
|
||||||
if (ft_is_prime(nb))
|
|
||||||
return (nb);
|
|
||||||
return (2);
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
#include <limits.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MIN,
|
|
||||||
ft_find_next_prime(INT_MIN));
|
|
||||||
printf("Given %d, next prime is %d\n", -2, ft_find_next_prime(-2));
|
|
||||||
printf("Given %d, next prime is %d\n", -1, ft_find_next_prime(-1));
|
|
||||||
printf("Given %d, next prime is %d\n", 0, ft_find_next_prime(0));
|
|
||||||
printf("Given %d, next prime is %d\n", 1, ft_find_next_prime(1));
|
|
||||||
printf("Given %d, next prime is %d\n", 2, ft_find_next_prime(2));
|
|
||||||
printf("Given %d, next prime is %d\n", 3, ft_find_next_prime(3));
|
|
||||||
printf("Given %d, next prime is %d\n", 4, ft_find_next_prime(4));
|
|
||||||
printf("Given %d, next prime is %d\n", 5, ft_find_next_prime(5));
|
|
||||||
printf("Given %d, next prime is %d\n", 6, ft_find_next_prime(6));
|
|
||||||
printf("Given %d, next prime is %d\n", 7, ft_find_next_prime(7));
|
|
||||||
printf("Given %d, next prime is %d\n", 8, ft_find_next_prime(8));
|
|
||||||
printf("Given %d, next prime is %d\n", 9, ft_find_next_prime(9));
|
|
||||||
printf("Given %d, next prime is %d\n", 10, ft_find_next_prime(10));
|
|
||||||
printf("Given %d, next prime is %d\n", 11, ft_find_next_prime(11));
|
|
||||||
printf("Given %d, next prime is %d\n", 12, ft_find_next_prime(12));
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MAX - 1000,
|
|
||||||
ft_find_next_prime(INT_MAX - 1000));
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MAX - 3,
|
|
||||||
ft_find_next_prime(INT_MAX - 3));
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MAX - 2,
|
|
||||||
ft_find_next_prime(INT_MAX - 2));
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MAX - 1,
|
|
||||||
ft_find_next_prime(INT_MAX - 1));
|
|
||||||
printf("Given %d, next prime is %d\n", INT_MAX,
|
|
||||||
ft_find_next_prime(INT_MAX));
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
|
@ -1,93 +0,0 @@
|
||||||
/* ************************************************************************** */
|
|
||||||
/* */
|
|
||||||
/* ::: :::::::: */
|
|
||||||
/* ft_ten_queens_puzzle.c :+: :+: :+: */
|
|
||||||
/* +:+ +:+ +:+ */
|
|
||||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
|
||||||
/* +#+#+#+#+#+ +#+ */
|
|
||||||
/* Created: 2023/03/30 17:05:04 by tosuman #+# #+# */
|
|
||||||
/* Updated: 2023/03/30 17:05:06 by tosuman ### ########.fr */
|
|
||||||
/* */
|
|
||||||
/* ************************************************************************** */
|
|
||||||
|
|
||||||
#include <unistd.h>
|
|
||||||
#include <stdio.h>
|
|
||||||
#define SIZE 10
|
|
||||||
|
|
||||||
void ft_putchar(char c)
|
|
||||||
{
|
|
||||||
write(1, &c, 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// print the current board and return sols+1
|
|
||||||
int new_sol(int cols[SIZE], int sols)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
while (++i < SIZE)
|
|
||||||
ft_putchar(cols[i] + '0');
|
|
||||||
ft_putchar('\n');
|
|
||||||
return (sols + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int is_valid_board(int cols[SIZE], int col)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = col;
|
|
||||||
while (--i >= 0)
|
|
||||||
if (cols[i] == cols[col])
|
|
||||||
return (0);
|
|
||||||
i = 0;
|
|
||||||
while (++i <= cols[col])
|
|
||||||
if (cols[col - i] == cols[col] - i && i <= col)
|
|
||||||
return (0);
|
|
||||||
i = 0;
|
|
||||||
while (++i < SIZE - cols[col] && i <= col)
|
|
||||||
{
|
|
||||||
if (cols[col - i] == cols[col] + i)
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
return (1);
|
|
||||||
}
|
|
||||||
|
|
||||||
int backtrack(int cols[SIZE], int col, int sols)
|
|
||||||
{
|
|
||||||
int i;
|
|
||||||
|
|
||||||
if (col >= SIZE)
|
|
||||||
{
|
|
||||||
return (new_sol(cols, sols));
|
|
||||||
}
|
|
||||||
i = -1;
|
|
||||||
while (++i < SIZE)
|
|
||||||
{
|
|
||||||
cols[col] = i;
|
|
||||||
if (is_valid_board(cols, col))
|
|
||||||
sols += backtrack(cols, col + 1, 0);
|
|
||||||
cols[col] = -1;
|
|
||||||
}
|
|
||||||
return (sols);
|
|
||||||
}
|
|
||||||
|
|
||||||
int ft_ten_queens_puzzle(void)
|
|
||||||
{
|
|
||||||
int cols[SIZE];
|
|
||||||
int i;
|
|
||||||
|
|
||||||
i = -1;
|
|
||||||
while (++i < SIZE)
|
|
||||||
cols[i] = -1;
|
|
||||||
return (backtrack(cols, 0, 0));
|
|
||||||
}
|
|
||||||
|
|
||||||
/* ////
|
|
||||||
#include <stdio.h>
|
|
||||||
|
|
||||||
int main(void)
|
|
||||||
{
|
|
||||||
printf("%d\n", ft_ten_queens_puzzle());
|
|
||||||
return (0);
|
|
||||||
}
|
|
||||||
*/ ////
|
|
Loading…
Reference in New Issue