c05
This commit is contained in:
parent
13ea061fcc
commit
0705b343ff
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* ft_iterative_factorial.c :+: :+: :+: */
|
||||
/* +:+ +:+ +:+ */
|
||||
/* By: tosuman </var/spool/mail/tosuman> +#+ +:+ +#+ */
|
||||
/* +#+#+#+#+#+ +#+ */
|
||||
/* Created: 2023/03/30 16:54:02 by tosuman #+# #+# */
|
||||
/* Updated: 2023/03/30 16:54:03 by tosuman ### ########.fr */
|
||||
/* */
|
||||
/* ************************************************************************** */
|
||||
|
||||
int ft_iterative_factorial(int nb)
|
||||
{
|
||||
int fact;
|
||||
|
@ -10,7 +22,7 @@ int ft_iterative_factorial(int nb)
|
|||
return (fact);
|
||||
}
|
||||
|
||||
#define START
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
@ -27,3 +39,4 @@ int main(void)
|
|||
printf("15: %d\n", ft_iterative_factorial(15));
|
||||
return (0);
|
||||
}
|
||||
*/ ////
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
|
@ -5,7 +17,7 @@ int ft_recursive_factorial(int nb)
|
|||
return (nb * ft_recursive_factorial(nb - 1));
|
||||
}
|
||||
|
||||
#define START
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
@ -22,3 +34,4 @@ int main(void)
|
|||
printf("15: %d\n", ft_recursive_factorial(15));
|
||||
return (0);
|
||||
}
|
||||
*/ ////
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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;
|
||||
|
@ -10,7 +22,7 @@ int ft_iterative_power(int nb, int power)
|
|||
return (res);
|
||||
}
|
||||
|
||||
#define START
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
@ -80,3 +92,4 @@ int main(void)
|
|||
|
||||
return (0);
|
||||
}
|
||||
*/ ////
|
||||
|
|
|
@ -1,3 +1,15 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
|
@ -5,7 +17,7 @@ int ft_recursive_power(int nb, int power)
|
|||
return (nb * ft_recursive_power(nb, power - 1));
|
||||
}
|
||||
|
||||
#define START
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
|
||||
int main(void)
|
||||
|
@ -75,3 +87,4 @@ int main(void)
|
|||
|
||||
return (0);
|
||||
}
|
||||
*/ ////
|
||||
|
|
|
@ -1,4 +1,22 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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)
|
||||
{
|
||||
|
@ -25,24 +43,74 @@ int ft_int_sqrt(int nb)
|
|||
if (sqrt == old_sqrt)
|
||||
break ;
|
||||
}
|
||||
return (0);
|
||||
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);
|
||||
}
|
||||
|
||||
#define START
|
||||
/* ////
|
||||
#include <stdio.h>
|
||||
#include <limits.h>
|
||||
|
||||
int main(void)
|
||||
{
|
||||
int i;
|
||||
int prime_count;
|
||||
|
||||
i = 0;
|
||||
while (i < 200)
|
||||
prime_count = 0;
|
||||
i = -10;
|
||||
while (++i <= INT_MAX / 1000)
|
||||
{
|
||||
if (ft_is_prime(i))
|
||||
printf("Prime: %d", 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;
|
||||
*/ ////
|
||||
|
|
|
@ -0,0 +1,126 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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,29 +1,93 @@
|
|||
/* ************************************************************************** */
|
||||
/* */
|
||||
/* ::: :::::::: */
|
||||
/* 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
|
||||
|
||||
int backtrack(cols[SIZE], int col, int sols)
|
||||
void ft_putchar(char c)
|
||||
{
|
||||
if (col >= SIZE)
|
||||
return sols + 1;
|
||||
write(1, &c, 1);
|
||||
}
|
||||
|
||||
int ft_ten_queens_puzzle()
|
||||
// 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 = 0;
|
||||
while (i < SIZE)
|
||||
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