jarv / cmdchallenge

This repo is mirror of https://gitlab.com/jarv/cmdchallenge
https://about.cmdchallenge.com
MIT License
721 stars 71 forks source link

find_primes solution problem #157

Closed ademouerghi closed 7 years ago

ademouerghi commented 7 years ago

This how i solve find_primes but it doesn't work

input="random-numbers.txt" while IFS= read -r var do if [expr $var % 2-eq 1 ] then echo $var fi done < "$input"|wc -l

ecksun commented 7 years ago

I don't think this is correct though, you are just checking if the number is divisible by 2, not if it is a prime (try 9 for example, it is not a prime).

I could not actually try your command though, I'm guessing there is a formatting issue.

Some tips:

I would recommend you to use $() instead of \`for executing commands using bash, its more readable and allows you to nest expressions (for example$(du -hs $(head -n 1 logfile))). Moreover you don't need theinputvariable, you should simply be able to do< random-numbers.txt`

ademouerghi commented 7 years ago

and this one not working too

`input="random-numbers.txt" while IFS= read -r var do
prime=1 vari=$(($var/2))

for core in $(seq 2 $vari); do 
    if (($var%$core==0))
    then
        prime=0
    fi     
done
if (($prime==1)); 
then
    echo $var
fi

done < "$input"|wc -l

jarv commented 7 years ago

Closing this as I don't think there is a real issue here.