first commit
This commit is contained in:
30
ended/C05/ex00/ft_iterative_factorial.c
Normal file
30
ended/C05/ex00/ft_iterative_factorial.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 11:34:45 by ajoly #+# #+# */
|
||||
/* Updated: 2022/07/27 11:34:47 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_factorial(int nb)
|
||||
{
|
||||
int i;
|
||||
int result;
|
||||
|
||||
i = 1;
|
||||
result = 1;
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
if (nb == 0 || nb == 1)
|
||||
return (1);
|
||||
while (i <= nb)
|
||||
{
|
||||
result = i * result;
|
||||
i++;
|
||||
}
|
||||
return (result);
|
||||
}
|
20
ended/C05/ex01/ft_recursive_factorial.c
Normal file
20
ended/C05/ex01/ft_recursive_factorial.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 14:51:29 by ajoly #+# #+# */
|
||||
/* Updated: 2022/08/01 13:39:06 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_factorial(int nb)
|
||||
{
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
if (nb == 1 || nb == 0)
|
||||
return (1);
|
||||
return (nb * ft_recursive_factorial(nb - 1));
|
||||
}
|
28
ended/C05/ex02/ft_iterative_power.c
Normal file
28
ended/C05/ex02/ft_iterative_power.c
Normal file
@ -0,0 +1,28 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 16:42:03 by ajoly #+# #+# */
|
||||
/* Updated: 2022/07/27 16:42:04 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_power(int nb, int power)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
if (power < 0)
|
||||
return (0);
|
||||
else if (power == 0)
|
||||
return (1);
|
||||
while (power > 0)
|
||||
{
|
||||
i = i * nb;
|
||||
power--;
|
||||
}
|
||||
return (i);
|
||||
}
|
20
ended/C05/ex03/ft_recursive_power.c
Normal file
20
ended/C05/ex03/ft_recursive_power.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 18:32:22 by ajoly #+# #+# */
|
||||
/* Updated: 2022/07/27 18:51:35 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_power(int nb, int power)
|
||||
{
|
||||
if (power < 0)
|
||||
return (0);
|
||||
else if (power == 0)
|
||||
return (1);
|
||||
return (nb * ft_recursive_power(nb, power - 1));
|
||||
}
|
22
ended/C05/ex04/ft_fibonacci.c
Normal file
22
ended/C05/ex04/ft_fibonacci.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_fibonacci.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 18:51:47 by ajoly #+# #+# */
|
||||
/* Updated: 2022/07/27 18:51:48 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_fibonacci(int index)
|
||||
{
|
||||
if (index < 0)
|
||||
return (-1);
|
||||
if (index == 0)
|
||||
return (0);
|
||||
if (index == 1)
|
||||
return (1);
|
||||
return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
|
||||
}
|
29
ended/C05/ex05/ft_sqrt.c
Normal file
29
ended/C05/ex05/ft_sqrt.c
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sqrt.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/27 21:41:03 by ajoly #+# #+# */
|
||||
/* Updated: 2022/08/01 15:57:14 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_sqrt(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 1;
|
||||
while (i * i < nb && i <= nb / 2)
|
||||
i++;
|
||||
if ((nb % i) == 0)
|
||||
return (i);
|
||||
return (0);
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
int main()
|
||||
{
|
||||
printf("%d", ft_sqrt(__INT_MAX__));
|
||||
}
|
27
ended/C05/ex06/ft_is_prime.c
Normal file
27
ended/C05/ex06/ft_is_prime.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/28 10:27:29 by ajoly #+# #+# */
|
||||
/* Updated: 2022/08/01 15:58:00 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 2;
|
||||
if (nb <= 1)
|
||||
return (0);
|
||||
while (i <= nb / i)
|
||||
{
|
||||
if (nb % i == 0)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
39
ended/C05/ex07/ft_find_next_prime.c
Normal file
39
ended/C05/ex07/ft_find_next_prime.c
Normal file
@ -0,0 +1,39 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_find_next_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: ajoly <ajoly@student.42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/29 10:45:00 by ajoly #+# #+# */
|
||||
/* Updated: 2022/08/01 15:57:50 by ajoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 2;
|
||||
if (nb <= 1)
|
||||
return (0);
|
||||
while (i <= nb / i)
|
||||
{
|
||||
if (nb % i == 0)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_find_next_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = nb;
|
||||
while (ft_is_prime(i) != 1)
|
||||
{
|
||||
i++;
|
||||
}
|
||||
return (i);
|
||||
}
|
Reference in New Issue
Block a user