How to display all primes of N starting at 1 from a given input?

Asked

Viewed 126 times

0

This is the algorithm that I have been able 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

    A natural prime number has only two distinct natural divisors: the number one and itself. Since any number is always divisible by 1 and by itself, a natural number n will be prime if there is no divisor between 2 and n-1. Use % operator (Rest of entire division).

  • I can’t use modules

  • I have to do it hard but I can’t do the check

1 answer

0

Currently on the date of 09/05/19 a user has asked the following question "How to account for the amount of prime numbers in a matrix?" and I worked out an algorithm for the same in c++ (which was requested on the tag) and explained to the same some rules of prime numbers, I’ll do the same to you, so come on?

Before developing any algorithm you need to analyze the situation, with this we need to know first:

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 composite numbers.

What characterizes a prime number ?

Aggregating the knowledge of the above question, the unique even prime number is 2, all others are odd. So with this we know of a general form that the primes are not all odd numbers 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
        }  
    }
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.