0

Can anyone help please? I need to know how to make the sum of value S(n) = 0+1+2+3+4...+(n-1)+n (example: 0+1+2+3 = 6 , 0+1+2+3+4+5+6+7 = 28 and so on) on Latex

I am new in this language.

\batchmode
\documentclass{article}
\pagestyle{empty}
\usepackage{ifthen}

\newcounter{sommea}
\newcounter{sommeb}
\newcounter{compt}

\newcommand{\somme}[2]{
   \setcounter{sommea}{#1}
   \setcounter{sommeb}{#2}
   \setcounter{compt}{0}
   \sommen{#1}
}

\newcommand{\sommen}{1}{
  \ifthenelse{#1 = \thecompt}{\sommeadd{#1}}{}
}
yo'
  • 51,322
rudstep
  • 111
  • I did it on LISP, PYTHON and C. Now I need to do it on LATEX. Thank you for your answer. – rudstep Sep 18 '14 at 14:44
  • @David Carlisle it is definitely a duplicate. At first I recommended rudstep to ask the question in the TeX forum, not on stackoverflow, which he immediately did. Then I decided to provide an answer with my very limited TeX knowledge (printed below with a meaningful edit from Marc). – Ronald Oct 06 '14 at 14:14

2 Answers2

2

The forloop way .... just specify the desired number of integers to be counted:

\documentclass{article}
\pagestyle{empty}
\usepackage{forloop}

\newcounter{sumcounter}%
\newcounter{loopcounter}
\newcommand{\gausssum}[1]{%
\setcounter{sumcounter}{0}%
\forloop{loopcounter}{1}{\value{loopcounter} < \numexpr #1 +1}{%
  \addtocounter{sumcounter}{\value{loopcounter}}%
}%

}

\newcounter{democounter}

\begin{document}
\forloop{democounter}{1}{\value{democounter} < 21}{%
\gausssum{\number\value{democounter}}%
\(\sum\limits_{k=1}^{\number\value{democounter}} k = \thesumcounter\)%

}%

\end{document}

enter image description here

  • I just realized, that I posted my message just before the question was closed ;-) –  Oct 06 '14 at 14:19
0

Well, try this:

\usepackage{ifthen}

\newcounter{s}
\newcounter{ergebnis}
\newcommand{\Summe}[1]{%
\setcounter{s}{#1}%
\setcounter{ergebnis}{0}%
\whiledo{\value{s} > 0}{%
\addtocounter{ergebnis}{\value{s}}%
\addtocounter{s}{-1}%
}\theergebnis}

And \Summe{3} in your document will print 6. (It would have been possible to multiply and divide, making it a lot faster)

Ronald
  • 291