99% of the pajeets won't solve this correctly

99% of the pajeets won't solve this correctly

write a program that prints the sum of integers in a given closed range [a,b] where b < a, both are positive integers.

Attached: index.png (211x239, 7K)

>homework

come on is not as hard as it looks like.

/thread

Jow Forums can't even solve fizzbuzz correctly, do you think they gonna solve a discrete math problem?

print(sum(range(b,a)))

>0

int total = 0, b = 5;

for (int a = 10; b < a; a--) {
total += a;
}

println('OP is faggot');

pajeet detected.

(define (opISfag a b) (apply + (range a (- b 1) -1)))

j=0
For(i=a,i

#include

int sum(int a, int b);

int main () {
int a,b;
scanf("%d,%d", a,b);
print("%d", sum(a,b));
return 0;
}

int sum(int a, int b) {
if (a==1)
return b*(b+1)/2

else
return (b+1-a)*(b+a)/2
}

>doesn't even compile
Put a little more effort into your weak trolling attempts.

Is it an inclusive range?

I said its closed range, yes inclusive btw its b > a not b < a sorry.

btw all fucking idiots using loops for things problem should leave the thread.

You can do it in constant time instead of O(length of range) time. This is the first question on Hackerrank’s ‘practice employer test’ or whatever.

So best I can figure you can do it the "practical" way or the showboating-algorithm-nerd way:

#!/usr/bin/env perl

use strict;
use warnings;
use feature 'say';

sub adder {
my $num1 = shift;
my $num2 = shift;
my $total;

for ( $num1 .. $num2 ) {
$total += $_;
}

return $total;
}

sub adder2 {
my $num1 = shift;
my $num2 = shift;
my $total = ( $num2 * ( $num2 + 1 ) ) / 2
- ( ( $num1 - 1 ) * ( ( $num1 - 1 ) + 1 ) ) / 2;

return $total;
}

my $num1 = 1;
my $num2 = 10;
say adder( $num1, $num2 );
say adder2( $num1, $num2 );


I don't see much of a practical difference here aside from the first one being more common-sense and a hell of a lot easier to remember and maintain. Am I missing something? The first one feels like it's "too simple" but why the hell would you use the second if the first is available?

adder runs in quadratic time and adder2 runs in linear time. Read about Big-O complexity classes

Thank you, I'll look into it.

You mean linear and constant time.

>666
Yes I do thank you.

Is that respectively?

var sumA = (a * (a+1))/2
var sumB = (b * (b+1))/2

return sumB - sumA

def niggers(a, b):
if a == b:
return a
else:
return a + niggers(a-1, b)

print niggers(100,70)


am I disabled?

I like these threads

>he has an irrational hatred for loops

int a = 5;
int b = 10;
int sum = 0;
while (a < b)
sum += (a + b);
printf ("%d\n", sum);

int a = some positive number;
int b = some positive number greater than a, I'm not checking that;
int bucket = 0;

while( a < b + 1 )
{
bucket += a;
a++;
}
System.out.println(bucket);

Yeah we might've done OP's homework. I just needed a quick warm up. Back to work for me now.

return (a * (a + 1) - b * (b - 1)) / 2

I can't believe it took this long. You people really are fucking brainlets.

//Valid input check
return (b + a)*(a - b + 1)/2;

There you go you moronic fucking highschooler, hope you get an A. saged

>I just needed a quick warm up.
warm up? seriously? you never coded a shit dude, how is possible that you used a loop for a high school problem.

((a+b)*abs(a-b))/2

If you even think about using a loop you are a low IQ brainlet.

In the greatest language ever created, MATLAB:
sum(a:b)

Wow I can't believe it, OP is being a faggot again.

Attached: make-my-dinner-jutsu.jpg (750x573, 40K)

enjoy blowing up your stack functard

congratz op
Meanwhile... I have a job, and you haven't. :)

Attached: 81051a39fbd5b5c774e78ecf60de256d9b7b5a66.jpg (1000x831, 413K)

Dear lord. You had better be joking.

can I have your stuff

>all these tards posting O(n) solutions when the O(1) solution is fucking trivial

>ITT: pathetic students

pleaseNoBulling :: (Int,Int) -> String
pleaseNoBulling (start,end)
| start < end = "Start < End"
| start < 0 || end < 0 = "Only positive integers allowed"
| otherwise = show $ foldl1 (\x y -> x + y) [start,start-1..end]
main = print $ pleaseNoBulling (555,444)

do you realize that remaining 1% would still be greater than your country's population.

>Meanwhile... I have a job,
not in firmware, we care about computational complexity , so you will stay as monkey all your life.

do you even know basic math?, you don't need any kind of iterations to solve this idiot.

shut the fuck up O(n)-tard.

I mean, this one is even easier than fizzbuzz.