Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <unistd.h>
- #include <stdio.h>
- void ft_putstr(char *str)
- {
- int i;
- i = 0;
- while (str[i])
- {
- write(1,&str[i],1);
- i++;
- }
- }
- int ft_atoi(char *str)
- {
- int i;
- int is_neg;
- int res;
- i = 0;
- is_neg = 0;
- res = 0;
- if (!str)
- return (0);
- while (str[i] == '\t' || str[i] == '\n' || str[i] == '\r' || str[i] == '\v' || str[i] == '\f')
- i++;
- if (str[i] == '+' || str[i] == '-')
- {
- if (str[i] == '-')
- is_neg = 1;
- i++;
- }
- while (str[i] >= '0' && str[i] <= '9')
- {
- res = (res * 10) + (str[i] - '0');
- i++;
- }
- return (is_neg ? -res : res);
- }
- void ft_putnbr(int nb)
- {
- char c;
- if (nb == -2147483648)
- ft_putstr("-2147483648");
- if (nb < 0)
- {
- nb = -nb;
- write(1, "-", 1);
- }
- if (nb < 10)
- {
- c = nb + '0';
- write(1, &c, 1);
- }
- else
- {
- ft_putnbr(nb / 10);
- ft_putnbr(nb % 10);
- }
- }
- int is_prime(int nb)
- {
- int i;
- i = 2;
- if (nb < 0)
- return (0);
- while (i <= (nb / 2))
- {
- if (!(nb % i))
- return (0);
- else
- i++;
- }
- return (1);
- }
- int main(int ac, char *av[])
- {
- int sum;
- int nb;
- sum = 0;
- nb = ft_atoi(av[1]);
- if (ac == 2 && nb > 0)
- {
- while (nb > 0)
- {
- if (is_prime(nb))
- sum += nb;
- nb--;
- }
- ft_putnbr(sum);
- }
- else
- write (1,"0",1);
- write(1, "\n", 1);
- return (0);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement