I'm creating a program that display n number of prime number (e.g. The program will display first 6 prime numbers if the user inputted 6). And I want the display 1 row contains 5 numbers. I did googled it around to find the solution but failed to find it.
I have tried using if else statement to do it but not working
for ( $i = 3; $i -gt 0; ++$i ) #starting from 3
{
$sqrt = [math]::Sqrt($i) #Optimize memory consumption
for ( $j = 2; $j -le $sqrt; ++$j ) #Checked if it is divisible by any natural number
{
if ( $i % $j -eq 0 ) #if is prime number
{
$prime = 1 #set to false
break #stop taking into account
}
}
if ( $prime -eq 0 ) #if not prime number
{
Write-Host ("$i") #display prime number
$count++ #set count to 1
$turn++ #set turn to 1
}
$prime = 0 #is prime number
if ( $count -eq $value ) #if until nth prime number
{
break #stop taking into account
}
if ( $turn -eq 5 )
{
Write-Output(" ")
$turn = 0
}
}
I'm expecting the result is like:
3
5
7
11
13
17 19 23 29 31
But it gave me the output like this:
3
5
7
11
13
17
19
23
29
31
Any suggestions / advice is welcomed. Thank you in advance.