init
This commit is contained in:
BIN
Correction/d/a.out
Executable file
BIN
Correction/d/a.out
Executable file
Binary file not shown.
23
Correction/d/ex00/ft_iterative_factorial.c
Normal file
23
Correction/d/ex00/ft_iterative_factorial.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 13:05:38 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/20 13:05:39 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_factorial(int nb)
|
||||
{
|
||||
int n;
|
||||
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
n = 1;
|
||||
while (nb)
|
||||
n = n * nb--;
|
||||
return (n);
|
||||
}
|
20
Correction/d/ex01/ft_recursive_factorial.c
Normal file
20
Correction/d/ex01/ft_recursive_factorial.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 13:17:09 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/20 13:17:10 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_factorial(int nb)
|
||||
{
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
else if (nb > 1)
|
||||
return (nb * ft_recursive_factorial(nb - 1));
|
||||
return (1);
|
||||
}
|
23
Correction/d/ex02/ft_iterative_power.c
Normal file
23
Correction/d/ex02/ft_iterative_power.c
Normal file
@ -0,0 +1,23 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 13:25:45 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/20 13:25:46 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_power(int nb, int power)
|
||||
{
|
||||
int stock;
|
||||
|
||||
stock = 1;
|
||||
if (power < 0)
|
||||
return (0);
|
||||
while (power--)
|
||||
stock = stock * nb;
|
||||
return (stock);
|
||||
}
|
20
Correction/d/ex03/ft_recursive_power.c
Normal file
20
Correction/d/ex03/ft_recursive_power.c
Normal file
@ -0,0 +1,20 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_recursive_power.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 16:44:43 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/20 16:44:44 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_recursive_power(int nb, int p)
|
||||
{
|
||||
if (p > 0)
|
||||
return (nb * ft_recursive_power(nb, p - 1));
|
||||
if (p == 0)
|
||||
return (1);
|
||||
return (0);
|
||||
}
|
22
Correction/d/ex04/ft_fibonacci.c
Normal file
22
Correction/d/ex04/ft_fibonacci.c
Normal file
@ -0,0 +1,22 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_fibonacci.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 17:16:27 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/20 17:16:27 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_fibonacci(int index)
|
||||
{
|
||||
if (!(index))
|
||||
return (0);
|
||||
if (index < 0)
|
||||
return (-1);
|
||||
if (index == 1)
|
||||
return (1);
|
||||
return (ft_fibonacci(index - 1) + ft_fibonacci(index - 2));
|
||||
}
|
42
Correction/d/ex05/ft_sqrt.c
Normal file
42
Correction/d/ex05/ft_sqrt.c
Normal file
@ -0,0 +1,42 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_sqrt.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/20 19:03:04 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/24 15:50:32 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_sqrt_bis(long int nb, int max, int min)
|
||||
{
|
||||
long int middle;
|
||||
|
||||
middle = (max - min) / 2 + min;
|
||||
if (middle * middle == nb)
|
||||
return (middle);
|
||||
else if (min + 1 == max)
|
||||
return (0);
|
||||
else if (middle * middle > nb)
|
||||
return (ft_sqrt_bis(nb, middle, min));
|
||||
else
|
||||
return (ft_sqrt_bis(nb, max, middle));
|
||||
}
|
||||
|
||||
int ft_sqrt(int nb)
|
||||
{
|
||||
if (nb < 0)
|
||||
return (0);
|
||||
return (ft_sqrt_bis(nb, nb, 0));
|
||||
}
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
int main(int ac, char **av)
|
||||
{
|
||||
if (ac != 2)
|
||||
return (0);
|
||||
printf("%d", ft_sqrt(atoi(av[1])));
|
||||
}
|
27
Correction/d/ex06/ft_is_prime.c
Normal file
27
Correction/d/ex06/ft_is_prime.c
Normal file
@ -0,0 +1,27 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_is_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/21 10:46:05 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/21 10:46:06 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int ind;
|
||||
|
||||
if (nb <= 1)
|
||||
return (0);
|
||||
ind = 2;
|
||||
while (ind < nb / 2)
|
||||
{
|
||||
if (!(nb % ind))
|
||||
return (0);
|
||||
ind++;
|
||||
}
|
||||
return (1);
|
||||
}
|
41
Correction/d/ex07/ft_find_next_prime.c
Normal file
41
Correction/d/ex07/ft_find_next_prime.c
Normal file
@ -0,0 +1,41 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_find_next_prime.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/21 10:46:05 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/21 14:42:31 by lflandri ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_is_prime(int nb)
|
||||
{
|
||||
int ind;
|
||||
|
||||
if (nb <= 1)
|
||||
return (0);
|
||||
ind = 2;
|
||||
while (ind < nb / 2)
|
||||
{
|
||||
if (!(nb % ind))
|
||||
return (0);
|
||||
ind++;
|
||||
}
|
||||
return (1);
|
||||
}
|
||||
|
||||
int ft_find_next_prime(int nb)
|
||||
{
|
||||
if (nb <= 2)
|
||||
return (2);
|
||||
if (!(nb % 2))
|
||||
nb++;
|
||||
while (1)
|
||||
{
|
||||
if (ft_is_prime(nb))
|
||||
return (nb);
|
||||
nb += 2;
|
||||
}
|
||||
}
|
102
Correction/d/ex08/ft_ten_queens_puzzle.c
Normal file
102
Correction/d/ex08/ft_ten_queens_puzzle.c
Normal file
@ -0,0 +1,102 @@
|
||||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_ten_queens_puzzle.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: lflandri <marvin@42.fr> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2022/07/21 14:43:07 by lflandri #+# #+# */
|
||||
/* Updated: 2022/07/24 16:26:44 by cchauvet ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
#include <unistd.h>
|
||||
|
||||
int ft_recursive(int tab[10][10], char *solution, int rang);
|
||||
|
||||
int insert_d(int tab[10][10], char *solution, int rang, int ind)
|
||||
{
|
||||
int hauteur;
|
||||
int rang_tempo;
|
||||
|
||||
rang_tempo = rang + 1;
|
||||
hauteur = 1;
|
||||
tab[rang][ind] = 2;
|
||||
while (rang_tempo != 10)
|
||||
{
|
||||
tab[rang_tempo][ind] = 1;
|
||||
if (ind - hauteur >= 0)
|
||||
tab[rang_tempo][ind - hauteur] = 1;
|
||||
if (ind + hauteur < 10)
|
||||
tab[rang_tempo][ind + hauteur] = 1;
|
||||
rang_tempo++;
|
||||
hauteur++;
|
||||
}
|
||||
return (ft_recursive(tab, solution, rang + 1));
|
||||
}
|
||||
|
||||
int cptab(int tab[10][10], char *solution, int rang, int ind)
|
||||
{
|
||||
int x;
|
||||
int y;
|
||||
int tab_copy[10][10];
|
||||
|
||||
x = 0;
|
||||
while (x != 10)
|
||||
{
|
||||
y = -1;
|
||||
while (++y != 10)
|
||||
tab_copy[x][y] = tab[x][y];
|
||||
x++;
|
||||
}
|
||||
return (insert_d(tab_copy, solution, rang, ind));
|
||||
}
|
||||
|
||||
int ft_recursive(int tab[10][10], char *solution, int rang)
|
||||
{
|
||||
int ind;
|
||||
int count;
|
||||
|
||||
count = 0;
|
||||
ind = 0;
|
||||
if (rang == 10)
|
||||
{
|
||||
write(1, solution, 11);
|
||||
count++;
|
||||
}
|
||||
else
|
||||
{
|
||||
while (ind != 10)
|
||||
{
|
||||
if (!(tab[rang][ind]))
|
||||
{
|
||||
*(solution + rang) = ind + 48;
|
||||
count += cptab(tab, solution, rang, ind);
|
||||
}
|
||||
ind++;
|
||||
}
|
||||
}
|
||||
return (count);
|
||||
}
|
||||
|
||||
int ft_ten_queens_puzzle(void)
|
||||
{
|
||||
int tab[10][10];
|
||||
char solution[11];
|
||||
int x;
|
||||
int y;
|
||||
|
||||
x = 0;
|
||||
while (x != 10)
|
||||
{
|
||||
y = -1;
|
||||
while (++y != 10)
|
||||
tab[x][y] = 0;
|
||||
x++;
|
||||
}
|
||||
solution[10] = '\n';
|
||||
return (ft_recursive(tab, solution, 0));
|
||||
}
|
||||
|
||||
int main()
|
||||
|
Reference in New Issue
Block a user