first commit
This commit is contained in:
30
C05/ex00/ft_iterative_factorial.c
Normal file
30
C05/ex00/ft_iterative_factorial.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 20:19:51 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/31 14:43:40 by adjoly ### ########.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;
|
||||
i++;
|
||||
}
|
||||
return (result);
|
||||
}
|
20
C05/ex01/ft_recursive_factorial.c
Normal file
20
C05/ex01/ft_recursive_factorial.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 20:36:35 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/23 23:07:46 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_factorial(int nb)
|
||||
{
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
if (nb == 1 || nb == 0)
|
||||
return (1);
|
||||
return (ft_recursive_factorial(nb - 1) * nb);
|
||||
}
|
30
C05/ex02/ft_iterative_power.c
Normal file
30
C05/ex02/ft_iterative_power.c
Normal file
@ -0,0 +1,30 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 22:29:27 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/25 19:40:26 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_power(int nb, int power)
|
||||
{
|
||||
int result;
|
||||
|
||||
if (power < 0)
|
||||
return (0);
|
||||
if (power == 0)
|
||||
return (1);
|
||||
if (power == 1)
|
||||
return (nb);
|
||||
result = nb;
|
||||
while (power > 1)
|
||||
{
|
||||
result *= nb;
|
||||
power--;
|
||||
}
|
||||
return (result);
|
||||
}
|
22
C05/ex03/ft_recursive_power.c
Normal file
22
C05/ex03/ft_recursive_power.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 22:18:51 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/25 19:41:15 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_power(int nb, int power)
|
||||
{
|
||||
if (power < 0)
|
||||
return (0);
|
||||
if (power == 0)
|
||||
return (1);
|
||||
if (power == 1)
|
||||
return (nb);
|
||||
return (ft_recursive_power(nb, power - 1) * nb);
|
||||
}
|
24
C05/ex04/ft_fibonacci.c
Normal file
24
C05/ex04/ft_fibonacci.c
Normal file
@ -0,0 +1,24 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_fibonacci.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 22:53:47 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/31 16:58:43 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_fibonacci(int index)
|
||||
{
|
||||
if (index < 0)
|
||||
return (-1);
|
||||
if (index == 0)
|
||||
return (0);
|
||||
if (index == 1)
|
||||
return (1);
|
||||
if (index == 2)
|
||||
return (1);
|
||||
return (ft_fibonacci(index - 2) + ft_fibonacci(index - 1));
|
||||
}
|
25
C05/ex05/ft_sqrt.c
Normal file
25
C05/ex05/ft_sqrt.c
Normal file
@ -0,0 +1,25 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sqrt.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 22:18:51 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/25 19:41:30 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_sqrt(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 0;
|
||||
while (i <= 46340)
|
||||
{
|
||||
if (i * i == nb)
|
||||
return (i);
|
||||
i++;
|
||||
}
|
||||
return (0);
|
||||
}
|
BIN
C05/ex06/.ft_is_prime.c.swp
Normal file
BIN
C05/ex06/.ft_is_prime.c.swp
Normal file
Binary file not shown.
29
C05/ex06/ft_is_prime.c
Normal file
29
C05/ex06/ft_is_prime.c
Normal file
@ -0,0 +1,29 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 23:24:05 by adjoly #+# #+# */
|
||||
/* Updated: 2023/08/02 10:20:17 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (nb <= 0 || nb == 1)
|
||||
return (0);
|
||||
i = 2;
|
||||
if (nb == 2)
|
||||
return (1);
|
||||
while (i < nb / 2)
|
||||
{
|
||||
if (nb % i == 0)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
43
C05/ex07/ft_find_next_prime.c
Normal file
43
C05/ex07/ft_find_next_prime.c
Normal file
@ -0,0 +1,43 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_find_next_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: adjoly <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/07/23 23:24:05 by adjoly #+# #+# */
|
||||
/* Updated: 2023/07/31 09:59:29 by adjoly ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = 2;
|
||||
if (nb == 2147483647)
|
||||
return (1);
|
||||
if (nb <= 0 || nb == 1)
|
||||
return (0);
|
||||
if (nb % 2 == 0)
|
||||
return (0);
|
||||
while (i < nb / 2)
|
||||
{
|
||||
if (nb % i == 0)
|
||||
return (0);
|
||||
i++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_find_next_prime(int nb)
|
||||
{
|
||||
int i;
|
||||
|
||||
i = nb;
|
||||
if (nb <= 2)
|
||||
return (2);
|
||||
while (ft_is_prime(i) != 1)
|
||||
i++;
|
||||
return (i);
|
||||
}
|
Reference in New Issue
Block a user