\documentclass{article}

\usepackage{amsmath}
\usepackage{amssymb}
\usepackage[amsmath]{ntheorem}
\providecommand{\qed}{\hfill\ensuremath{\blacksquare}}

\usepackage{listings}
% a ``simple algorithmic'' language
\lstdefinelanguage{Algo}{
  morekeywords={if,then,else,while,do,for,all,return,end,case,repeat,until},
  sensitive=true,
  morecomment=[l]{//}
}

\lstset{language=Algo,mathescape=true,belowskip=0pt,resetmargins=true}

\usepackage{lipsum}


\newtheorem{question}{Answer to question}

\begin{document}

\textbf{Name}: Sergius Catilina.

\begin{question}[X]
  I propose the following algorithm:
  \begin{lstlisting}[mathescape=true]
    while not solved
      think $x$ minutes
    end
  \end{lstlisting}

  \bigskip
  The complexity analysis is a follows: \lipsum[1]
  We conclude: $O(n+m^2)$.
  
  \bigskip
  We will show correctness now. Proof: \lipsum[2] \qed
\end{question}

\begin{question}
  \lipsum[3]
\end{question}

\begin{question}
  \lipsum[4]
\end{question}

\begin{question}
  \lipsum[5]
\end{question}
\end{document}
