Divisible-portugol (VISUALG)

I have the following question and below what I have tried but it is not showing only divisible numbers which is my intention.

I'm doing the test by typing the number 8, the correct was pro program say " 8 is divisible by 8 and by 2"

Write an algorithm that receives a number from the keyboard and inform if it is divisible by 8, by 5, by 2 or if it is not divisible by any of these. Check for each of the numbers. Example: Number: 8 Divisible by 8

My Code

var 
numero,contador,divisivel: inteiro
inicio
 contador <- 1
 divisível<- 0
escreva("Digite um número: )
 leia(numero)
repita
 escreva(contador)
   se(numero % contador = 0) então
  divisível <- divisível + 1
fimse
  contador <- contador + 1
ate (contador > numero)
  escreva("Número: ",divisível)
fimalgoritimo
Author: YODA, 2017-06-01

1 answers

Since I don't know the exact syntax of portugol/visualg, I ask the kindness of correcting any slip

This is a problem whose answer needs to meet a decision question, and also needs to meet the formatting of an answer.

What is a matter of decision?

A decision question is a problem to which one must answer sim or não. Examples of decision problem:

Note that it is not always possible to get the affirmative or negative answer, but this is another conversation for another day.

Question Decision problem

The question asks if given number is divisible by 8, if it is divisible by 5 and by 2; ask this individually, so for 200 I need to answer yes three times.

Each of these questions is an instance of a larger problem: is X divisible by Y?

To answer the general question, the code is:

X % Y = 0

Then, for each of the questions, I replace the Y with the appropriate value:

X % 8 = 0
X % 5 = 0
X % 2 = 0

Formatting the response

The answer format is:

X é divisível por N( e por K)*

Where X is the number informed, N is the first divisor, and the part in parentheses repeats for each K also divisor of X.

To identify if divisors of our search universe have been detected (8, 5, and 2, in that order), I will use a variable called divisores_encontrados, which will save the value of how many divisors the algorithm has found up to that moment. This means that it starts with zero, and if any of the checks return that the number is divisor, increment the value of divisores_encontrados.

Note: consider everything after # as a comment

var entrada, divisores_encontrados, divisor_teste: inteiro
início 
    divisores_encontrados <- 0 # inicializando o valor; ainda não foi encontrado nada
    leia(entrada)

    divisor_teste <- 8
    se entrada % divisor_teste = 0 então 
        se divisores_encontrados = 0 # primeira vez que encontrou um divisor 
            escreva(entrada)
            escreva(" é divisível por ")
            escreva(divisor_teste)
        senão
            escreva(" e por ")
            escreva(divisor_teste)
        fimse # verificação do primeiro divisor encontrado
        divisores_encontrados <- divisores_encontrados + 1
    fimse # verificação da divisibilidade 

    divisor_teste <- 5
    se entrada % divisor_teste = 0 então 
        se divisores_encontrados = 0 # primeira vez que encontrou um divisor 
            escreva(entrada)
            escreva(" é divisível por ")
            escreva(divisor_teste)
        senão
            escreva(" e por ")
            escreva(divisor_teste)
        fimse # verificação do primeiro divisor encontrado
        divisores_encontrados <- divisores_encontrados + 1
    fimse # verificação da divisibilidade 

    divisor_teste <- 2
    se entrada % divisor_teste = 0 então 
        se divisores_encontrados = 0 # primeira vez que encontrou um divisor 
            escreva(entrada)
            escreva(" é divisível por ")
            escreva(divisor_teste)
        senão
            escreva(" e por ")
            escreva(divisor_teste)
        fimse # verificação do primeiro divisor encontrado
        divisores_encontrados <- divisores_encontrados + 1
    fimse # verificação da divisibilidade 

    se divisores_encontrados = 0 
        escreva(entrada)
        escreva(" não é divisível por 2, 5 e nem 8")
    fimse # caso sem divisibilidade por 2, 5, 8

    escreval("") # só para pular linha no fim ;-)
fimalgoritmo

Bonus: about the amount of divisors

Your question program calculates how many divisors a number has. It writes at each step of the loop which number is being tested, but without breaking line.

You may simply not write these intermediate numbers if they don't interest you.

To know how many divisors has a number, we also do not need to pass for all numbers smaller than the number reported; there are strategies of decomposition into prime numbers and, also, another one that allows you to count up to the square root of the number. I'll go into more detail in the latter.

Every number X % Y = 0 means that there exists F integer such that F * Y = X. For any number other than the perfect square root of X, finding Y means knowing about the existence of its sister factor F. If I assume that Y is less than F, so I just need to count how many distinct Ys exist in the range [1, raiz(X)) (I left the range open at the square root, then I explain why) and multiply by 2. In the case of the square root, if the number found is a perfect square, the pair of raiz(X) will be F = raiz(X), so this particular number can only enter once.

The case of the number 1 and of X are trivial divisors, so I won't even go through them in the loop

var divisores_encontrados, r, entrada, divisor_teste: inteiro
início
    leia(entrada)

    se entrada = 1 # único caso que existem menos de dois divisores
        divisores_encontrados = 1
    senão
        divisores_encontrados <- 2 # os dois triviais
         r <- arredonda_baixo(raiz(entrada))
         divisor_teste <- 2
         enquanto divisor_teste < r # não pode ser <= porque o intervalo é aberto na raiz
             se entrada % divisor_teste = 0
                 divisores_encontrados <- divisores_encontrados + 2 # lembrando que achamos divisor_teste e sabemos que ele tem um par F maior do que a raiz quadrada
             fimse
             divisor_teste <- divisor_teste + 1
          fimenquanto

          se entrada % r = 0
             divisores_encontrados <- divisores_encontrados + 1 # entrada é quadrado perfeito
        fimse
    fimse

    escreva(entrada)
    escreva(" possui ")
    escreva(divisores_encontrados)
    escreval(" divisores")
fimalgoritmo
 5
Author: Jefferson Quesado, 2017-06-01 03:22:21