How to display all prime numbers of N starting at 1 from a given entry?
This is the algorithm I managed to develop so far in portugol:
programa {
funcao inicio() {
inteiro n1, soma ,div,div2
escreva("digite um numero")
leia(n1)
para(inteiro i=1 ;i < n1 ; i++) {
para(inteiro p =1 ; p < i ;p ++) {
div= i/p
div2=i-(div*p)
se(div2 == 0) {
escreva(i,"\n")
}
}
}
}
}
1 answers
Currently on the date of 09/05/19 one user asked the following Question " How to account for the amount of prime numbers in an array?" and I worked out an algorithm for the same in c++ (which was asked in the tag) and I explained to the same some rules of the prime numbers , I will do the same with you, so let's go?
Before developing any algorithm you need to analyze the situation, with this we need to know first next:
What are prime numbers?
A prime number is a natural number greater than 1, whose only positive divisors are 1 and itself . all others are called compound numbers.
What characterizes a prime number ?
Adding the knowledge of the above question, the only even prime number is 2, all others are odd. so with this we know from a general form that the prime numbers are not all odd and by exception the only even number 2.
programa {
funcao inicio() {
var n: inteiro
escreva("digite um número")
leia(n)
para(inteiro i = 1 ; i < n ; i++) {
se((i > 1) e (i % p == 1))
entao
escreva("É NÚMERO PRIMO: ", i)
fimse
}
}
}