Write a program that computes the sum of all natural numbers from 0 to n, where n is given by the user

Write a program that computes the sum of all natural numbers from 0 to n, where n is given by the user.

I'm waiting Jow Forums

Attached: 39994527A1CF4B028C5B8793EB0A2D6E.png (420x420, 7K)

Other urls found in this thread:

mathwords.com/a/arithmetic_series.htm
twitter.com/SFWRedditVideos

do your own homework, brainlet

Attached: you can't be serious.png (79x76, 16K)

Or else what? Is this bird gonna stab me?

Attached: Nevermore.jpg (412x430, 29K)

>maths

no thanks

>homework
>one for loop

OP started yesterday

int sum = 0;
for(natural numbers nat in range 0 to n)
sum+=nat;

nobody's gonna do your homework for you, newfriend.

>loop
am i being baited

return n*(n+1)/2;

/thread

Attached: 268zj7.jpg (645x729, 26K)

(\n -> read $ sum [1..n]) getContents >>= putStrLn

How would you do it?

n * (n+1) / 2 ?????

Or else that birb gonna stab OP

>06/12/18(Tue)09:56:30 No.66323586
puedo
for each(int i = 0; i zoop zoop; i < n)
{
add sum;
}
me gusto

1+2+3+4+5+6= (1+6)+(2+5)+(3+4)=7+7+7

You should be able to figure the rest out

your equation is ambiguous, could you reformat?

It's not you Pajeet

>mfw completely forgot this exists
sorry for suggesting a loop earlier i will go off myself
t. brainlet

package main

import (
"fmt"
"os"
"strconv"
)

func main() {
defer func() {
if rec := recover(); rec != nil {
fmt.Fprintf(os.Stderr, "error: %v\n", rec)
os.Exit(1)
}
}()

if len(os.Args) != 2 {
panic(fmt.Errorf("%s accepts exactly one argument", os.Args[0]))
}
n, err := strconv.Atoi(os.Args[1])
if err != nil || n >= (1

Okay, well do you mean (n*(n+1)) / 2

or do you mean n* ((n+1)/2)?

and don't call me a pajeet

While threading your own post is the works of a newfag, I do concur thread should have ended here.

Literally does not make a difference retard

In ruby: (0..n).sum
I was hoping to get less chars than n*(n+1)/2 but it's still 9 chars

* and / have the same precedence, so you read them from left to right, just like + and -. Would you say that this: 1 + 3 - 2 is also ambiguous?

learn how to math, brainlets

Attached: Untitled.png (577x137, 4K)

10/10

Attached: b.png (1597x1600, 532K)

kek

>natural numbers from 0 to n
>from 0
>0

Also f(n)=n(n+1)/2. Now fuck off.

Can someone explain this one to me? I got into development because I don't want to use math.

He is just a retarded faggot. they are same. Try calculating it if you can't believe it.

>not calling a pre announcement meeting to appoint stakeholders and define kpis (>90% accuracy) before deciding on the big data framework to assemble training sets for the naturalSum ML JS microservice

lol, if you don't know how to make money at least write a fucking unit test beforehand plebs

>I got into development
>I don't want to use math
How else are you gonna understand recursion ? How about complexity of algorithms?

DUP 1+ 2 */

Some definitions of natural numbers include 0.

what is natural number

(let [n (read)]
(if (or (not (int? n))
(< n 0))
(display "n has to be a non-negative integer\n")
(display (/ (* n (+ n 1)) 2))
(newline)))

>he needs to understand how it works instead of how to use it
wat a brainlet

Attached: 1490427613542.jpg (258x245, 12K)

const homework = length => eval(Array.from({ length }).map((_, i) => i + 1).join(" + "))

let rec sum n x =
match n with
| n -> sum (n-1) (x+n)
| 0 -> x
| _ -> failwith "u dun fuked up"

What do I win?

def natural_mumbers(n):
if n == 0:
return 0
return n + natural_mumbers(n-1)

Attached: c52241fa8b9cf46128e9f073b6c0965c.jpg (726x1089, 119K)

indent 4 more spaces on line 3

Attached: 1516642283304.png (657x527, 33K)

Bump

Attached: 1480203292113.png (1322x1696, 1.39M)

Sage

Attached: cat-soup.jpg (143x200, 6K)

butts

(defun homework (n)
(reduce #'+ (loop for i upto n collect i)))

can't do your homework kid?

#include

int natty(int n) {
if (n == 1)
return n;
else
return n + natty(n - 1);
}
int main(int argc, char *argv[]) {
if (argc == 2) {
return natty(atoi(argv[1]));
} else {
return -1;
}
}

ITT: persons that not know that the sum is n+1 choose 2.

Hmmm, (n choose k) is n! / (k!(n-k)!)

OP, if you have a recursive function for the faculty, you can easily write your program.

>Not understaning what you're doing
ITT it's better to be a Pajeet rather than knowing your shit.

=SUM(A1)

So is that a shitty meme or is it just some dumbfuck that's been struggling with his CS homework this year?

(define (sum n)
(if (= n 1)
1
(+ n (sum (- n 1)))))

Why it gives 15 for (sum 5)??

take a calculus class

>tfw did a degree in CS and forgot how to do this already

your_total = 1 + n

stop n;

Easy:

sum(1:n)

Oh yeah, I program in Matlab ;)

>understanding what you're doing is same like understanding every process of actions
>it's like, you can't drive a car if you don't know how combustion engine works
haha, look at this faggot

Are you really so brainlet that reading docs about i.e. some data structure like list or dictionary or linkedList or whatever ; you won't be able to use it because you don't know yet how it works?

Jeez, what a fucking retard. You probably use this as excuse to procrastinate.

>He can't do math

Attached: laughingwhores.jpg (1023x681, 46K)

Honestly code for that is gonna be pretty complex. Your PC will need to be at least 20 gigabots to compile it.

t. Computer man

>Go

Recognize:
(n) * (n+1) * (1/2)

So because multiplication is commutative, the order of the terms does not matter, Raj.

import sys
print(sum(range(int(sys.argv[1]) + 1)))

Epic troll is epic

Include me in the screencap.

int n;
if (int.TryParse(Console.ReadLine()), out n)
{
int sum = 0;
for (int i = 0; i < ++n; i++) {
sum += i;
}
Console.WriteLine(sum);
}

derp

int n;
if (int.TryParse(Console.ReadLine(), out n))
{
int sum = 0;
for (int i = 0; i < ++n; i++) {
sum += i;
}
Console.WriteLine(sum);
}

#include

int main(){
printf("It seems like it's perfect time for gulag");
}

>he implies to feel better about himself
I had 2 years of engineering level math at university. Not your burger level math.

Despite that, you don't need to understand how everything works to be productive programmer.
Not saying that you can do everything without math and deeper knowledge.

Your original post still holds level of retardation.
>I can't drive car because I don't know how gears are turning!

#include
main()
{
unsigned int n=0, i=0, b=0;
while (1)
{
scanf("%d", &b);
for (n=0; i

>implying i'm a burger
>implying I made the car post

Attached: you.jpg (1200x943, 124K)

>implying I am a burger
You do sound like one.

Brainlet here, is there are general algorithm to convert arithmetical loops into series?

>engineering level math
you do realize this is not an accomplishment right
not even actual undergrad math major math is that impressive

>I can't drive car because I don't know how gears are turning!
jesus man are you a fucking girl

n = console.Readline()
Print(0.5*n*(n+1))
Literally year 11 maths (junior year for OP)

>0.5*
why are you even doing floating point arithmetic on a closed formula for a series of integers

>implies I said it as accomplishment

>calling names because he knows that it was stupid to say that you don't need to understand how algorithms work to write code

(n*(n+1)) >> 1

mathwords.com/a/arithmetic_series.htm

Bad habit

thanks user

TRICK QUESTION
0 is not a natural number

based gaussposter