Task: List all even numbers up until 100

Task: List all even numbers up until 100.

Solution 1:
for(let i = 0; i

Attached: 1 uFTKDgI0kT878E97K14V1A.png (1600x900, 92K)

Other urls found in this thread:

warosu.org/g/thread/S67617367#p67617367
docs.python.org/3.0/whatsnew/3.0.html
twitter.com/NSFWRedditImage

None of them because mod is a slow as fuck operation. Use first with bitwise check:
for(let i = 0; i

Why is there always this guy? Who is this guy?

Are you misunderstanding the point of the thread deliberately or unintentionally? The task is trivial

Can't you just increase by 2 and get it over with?

for(let i = 0; i

Attached: Warframe0001.jpg (1920x1080, 1.05M)

I don't care how you fucking encapsulate your function

Attached: 1536671399019.jpg (920x666, 169K)

warosu.org/g/thread/S67617367#p67617367

Attached: 23.png (1164x732, 247K)

for i in [i for i in range(101) if not i & 1]: print (i)

for(let i = 0; i

Okay so here's the question i was merely implying to ask by giving three solution examples, since I'm genuinely wondering what the professional standard is and "should" be: what level of abstraction should be held when you do arbitrary tasks? Does it vary? How do you know what abstraction to aim for?

Asking primarily those who are _professional_ programmers (in a real company, not your wordpress theme production company) and/or employers.

wtf why are you looping twice
just do
[print(i) for i in range(101) if not i & 1]

Functional retards can't conceive of increasing a loop counter by 2.
Now ponder what that says about FP as a whole.

Bitwise check is faster than addition

based +2

bitwise check what? you still have to increment the number

maybe but instead you're doing 50 needless subtractions for the loop condition

I guess that's true. I concede

Not to mention you do 100 bitwise checks. +2 results in only 50 repetitions.

Earlier, an user had a sheet of whiteboard questions, of which one was to output 'even' and 'odd' on for each number, odd or even respectively, from 0 to 100, without using if/else or case switches.

You can just print 'even' once, and then print 'odd' 'even' 50 times. Get lazy.

kind of cheating but technically it isn't using if/else
[print(i & 1 and 'odd' or 'even') for i in range(101)]
your way is better though

echo 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100

range has a step parameter wtf are you doing

print(range(0,101,2))

bitwise check is not faster than just incrementing by 2 and printing it for this.

Here's disassembly of 0x0000000000400526 : push %rbp
0x0000000000400527 : mov %rsp,%rbp
0x000000000040052a : sub $0x10,%rsp
0x000000000040052e : movl $0x0,-0x4(%rbp)
0x0000000000400535 : jmp 0x40054f
0x0000000000400537 : mov -0x4(%rbp),%eax
0x000000000040053a : mov %eax,%esi
0x000000000040053c : mov $0x400644,%edi
0x0000000000400541 : mov $0x0,%eax
0x0000000000400546 : callq 0x400400
0x000000000040054b : addl $0x2,-0x4(%rbp)
0x000000000040054f : cmpl $0x64,-0x4(%rbp)
0x0000000000400553 : jle 0x400537
0x0000000000400555 : nop
0x0000000000400556 : leaveq
0x0000000000400557 : retq


and for :
0x0000000000400558 : push %rbp
0x0000000000400559 : mov %rsp,%rbp
0x000000000040055c : sub $0x10,%rsp
0x0000000000400560 : movl $0x0,-0x4(%rbp)
0x0000000000400567 : jmp 0x40058b
0x0000000000400569 : mov -0x4(%rbp),%eax
0x000000000040056c : and $0x1,%eax
0x000000000040056f : test %eax,%eax
0x0000000000400571 : jne 0x400587
0x0000000000400573 : mov -0x4(%rbp),%eax
0x0000000000400576 : mov %eax,%esi
0x0000000000400578 : mov $0x400644,%edi
0x000000000040057d : mov $0x0,%eax
0x0000000000400582 : callq 0x400400
0x0000000000400587 : addl $0x1,-0x4(%rbp)
0x000000000040058b : cmpl $0x64,-0x4(%rbp)
0x000000000040058f : jle 0x400569
0x0000000000400591 : nop
0x0000000000400592 : leaveq
0x0000000000400593 : retq

Your solution seems overly compicated for printing every other number. Is this troll?

for i in range(0, 102, 2):
print(i)

not quite
>>> print(range(0,101,2))
range(0, 101, 2)

It's python2

docs.python.org/3.0/whatsnew/3.0.html

[x | x

>using python 2 in 2018

works on my machine
in interpreter and as a script
x:~ user$ cat even.py
print(range(0,101,2))
x:~ user$ python even.py
[0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30, 32, 34, 36, 38, 40, 42, 44, 46, 48, 50, 52, 54, 56, 58, 60, 62, 64, 66, 68, 70, 72, 74, 76, 78, 80, 82, 84, 86, 88, 90, 92, 94, 96, 98, 100]


oh good thing I don't use faggot languages
enjoy your thought-policed basedlang

Of course it varies on the context. You can't make a blanket statement on this.

Exactly what I thought.

>t. brainlet

this can be optimized further
for(let i = 0; i

for(let i = 0; i

Wow. You do the same amount of prints you huge retard.

fewer loop iterations and thus fewer comparisons needed

Yep, really smart. Huge optimizations
Yet you somehow didn't think of using a single console.log call though
Halved amount of CMP instructions is clearly much more important than halved amount of prints

Attached: 1498330671493.jpg (204x306, 7K)

None of them take advantage of the trivial fact that you only have to loop over even numbers.
None of them take advantage of mapping the natural numbers to the nth numbers by multiplying them by n.
Worst of all, none of them separate the list building from the printing.

It's senseless to discuss your superficial differences when there are more fundamental problems.

>none of them separate the list building from the printing.
imagine doing the same thing for the numbers from 0 to a million. or a billion. why go for O(n) when you can do it with O(1) memory?

So, answering 's question:

Solution 1. But really, none would be preferable, as it's tricky to see what future usage you'd get out of any of these functions. As such, while it'd be useful to sequester the code into functions so that you can test things, you'd almost never actually use code from this kind of problem in production. So, whichever solution does its job and lets you ignore it in the future is best.

Another solution might be to group the even numbers into an array/vector/linked list, rather than outright print them. With that solution, you'd potentially have a small reason to maintain/document the function.

>Worst of all, none of them separate the list building from the printing.

fixed my solution. Roughly 2x speedup.

print([i for i in range(0,101,2)])

imagine using a language with generators or, even better, lazy evaluation

10 a%=0
20 a% = a% +2
30 print a%
40 if a% < 100 then goto 20
50 endif

>imagine using a language with bloat
not thanks

>i for i in
made me chuckle

in all seriousness, if you need to turn a range into a list manually, just wrap it in list().

Now imagine you could do that in O(1) memory while retaining that idea.
printing = mapM_ (putStrLn . show) numbers

numbers = [ 2 * k | k

I whipped out a C program for that:
#include
#include
#include

void splitty_split(int low, int high)
{
if (high == low)
printf("%d\n", 2 * high);
else {
int med = (low + high) / 2;
int pid = fork();
if (pid == 0)
splitty_split(low, med);
else
splitty_split(med + 1, high);
}
}

int main()
{
splitty_split(1, 50);
return 0;
}

I think it might work but I didn't test.

That’s probably slower user

Sam "Carmack" Hyde, he always makes crappy hacks and gets away with it

But now if I want to print the first even number, this breaks.

import sys

evens = [0]
odds = [1]

sys.setrecursionlimit(10000)

def is_even(n):
if n in evens:
return True
elif is_odd(n):
return False
else:
evens.append(n)
return True

def is_odd(n):
if n in odds:
return True
elif is_even(n-1):
odds.append(n)
return True
else:
return False

for n in range(0,101):
if is_even(n):
print(n)

Attached: 441.jpg (1154x1500, 1.16M)

>for i=2 to 100 step 2
>print i
>next i

>oh look guiz I used bitshift so it's automatically faster

Attached: 1519264861280.png (645x729, 62K)

(map print [0 100 2])

>this fucking thread
>people discussing hyper-micro-optimizations of complete horseshit like print odd and even numbers

Coming up with retarded ways to do thing is fun.
see

That really depends on the task at hand. In this case solution 3 is the way to go, because the higher level of abstraction doesn't complicate things. Usually it's the matter of "how much more work is it going to make this more generic and how much harder will it be to understand" vs "how likely it is that I'll ever make use of the abstraction" and there's no easy answer.
But instead of the comment you should have named the parameters properly.

Also,
>all these fizzbuzzers arguing about irrelevant details, most of which would be optimized away by the compiler/interpreter in any language where speed actually matters anything

hehe

Attached: 1535254527827.jpg (1200x1600, 374K)

for i 0 to 50
print 2*i

open editor:
msgbox"2 4 6 8 10 12 14 16 18 20 22 24 26 28 30 32 34 36 38 40 42 44 46 48 50 52 54 56 58 60 62 64 66 68 70 72 74 76 78 80 82 84 86 88 90 92 94 96 98 100"
save as .vbs

Attached: aayyyy.png (410x146, 3K)

Thank goodness someone else posted this.

Attached: ye.png (388x172, 5K)

Oh, also, I think that just printing them all isn't in the spirit of the challenge, so I did this, get rekt:
using System;

public class PointlessPatterns
{
public void printEvens(int first, int max)
{
string outputBuffer = "";
if (first%2==1)
{
first++;
}
while(first

Attached: 2016-05-16 20.22.14.jpg (300x169, 20K)

this, see for a better implementation that isn't shit.

Attached: IMG_0396.gif (320x240, 245K)

I just came up with a more general solution, lol. I kinda like this one, might actually be useful for something somewhere. Input is first=0, interval = 2, max=100 for this:
using System;

public class PointlessPatterns
{
public void printModableByInterval(int first, int interval, int max)
{
string outputBuffer = "";
while (first%interval!=0 && first < max)
{
first++;
}
while(first

sensible naming version, wish I saw the best solution back at the first post rather than getting there incrementally. It's like pressing the submit button on Jow Forums give you a split second IQ spike to realize why what you just posted is stupid because you can improve it:
using System;

public class PointlessPatterns
{
public void printModableByInterval(int rangeStart, int rangeEnd, int interval)
{
int current = rangeStart;//for clarity, the compiler will almost certainly optimize this away
string outputBuffer = "";
while (current%interval!=0 && current < rangeEnd)
{
current++;
}
while(current

Attached: IMG_0629.gif (250x250, 192K)

le oop meme
0.step(100,2)

Why am I so fucking dumb Jow Forums?
using System;

public class PointlessPatterns
{
public void printNumInRangeModableByInterval(int rangeStart, int rangeEnd, int interval)
{
int current = rangeStart;//for clarity, the compiler will almost certainly optimize this away
string outputBuffer = "";
while (current%interval!=0 && current < rangeEnd)
{
current++;
}
while(current

Attached: 2016-04-30 07.18.03.jpg (514x270, 31K)

in c++ this is just

const auto sum = ( Integers() | Take(100) | Filter(isEven) | Fold(sum) ).eval()

Attached: .jpg (1960x2922, 1.12M)

doesn't print

mapM_ print [0, 2 .. 100]

>Worst of all, none of them separate the list building from the printing.

This. Building a list and calling the print function once has to be faster, even for a shit algorithm.

for (let i = 2; i

(->>
(range 101)
(filter even? ))

besides being false in the general case (the cost of resizing the list may outweigh the multiple print calls), the point is to separate the logic for building the list from the task of printing it, so you can easily reason about either task.
Of course, it only makes sense when both parts require multiple lines, like it's retarded to split
main = print [2,4..100]

I cannot figure out how to output integers and not "True". Pretty new to this

aa = []
bb = []

def main():

def limit(numb, minimum= 1, maximum= 999):
return max(min(numb, maximum), minimum)

numb = int(eval(input("what range?: " , )))
evens = numb % 2 == 0

for i in range(numb):
if evens == True:
aa.append(evens)
print("The even numbers are: " , aa)

main()

wat

Attached: duck.jpg (600x399, 147K)

Here I cleaned it up...still can't get numbers and not 'True' though

aa = []

def main():

def limit(numb, minimum= 1, maximum= 999):
return max(min(numb, maximum), minimum)

numb = int(eval(input("what range?: " )))
evens = numb % 2 == 0

for i in range(numb):
if evens == True:
aa.append(evens)
print("The even numbers are: " , aa)

main()

>Task: List all even numbers up until 100.
Such a bullshit meaningless whiteboard task.

i don't read runes, what does that mean?

so solution shows a window so it's superior to any other

JUST

why the Fold(sum) ? what does it do?

Your evens variable is only testing the first "numb"
You need to make evens a function to test the numbers individually, and then you need to test each number when you iterate. I think like this:
numb = int(eval(input("what range?: " )))
evens = lambda n: n % 2 == 0

for i in range(numb):
if evens(i) == True:
aa.append(i)
print("The even numbers are: " , aa)

int print_even(int n){
std::cout

>//inside main

I thought he wanted the sum
tried writing a post like 3 times to either simply explain what fold does or just use the word applicative functor but i gave up
its simple if you google 'left fold' though

i know what fold does, i was just surprised to see it there and thought it was some sort of c++ fuckery like always

No shit an additional instruction would take longer. Though this can be further optimized.
From to :
test $0x1, %eax
jnz

Python
for i in range(0,101):
if i % 2 == 0:
print(i)

[print(x) for x in range(100) if x % 2 == 0]

Feels good being the only non brainlet in this thread

>evaluates x%2 for every value from 0 to 99 rather than starting at 2 and incrementing by 2
>produces the wrong result because he starts at 0 and ends at 98
>non brainlet
I have bad news for you, dude

There wouldnt be any purpose in that, you may as well make a generator with a step of 2.

Clearly the point to the question is to show you know how to use the modulo operator and do a basic check

Feels good, man.

You've still produced an incorrect output.

until
ənˈtJl/
preposition & conjunction
preposition: until; conjunction: until

up to (the point in time or the event mentioned).
"the kidnappers have given us until October 11th to deliver the documents"
synonyms: till, up to, up till, up until, as late as, up to the time of/that, until such time as, pending; More
through
"I was working away until midnight"
before, prior to, previous to, up to, up until, till, up till, earlier than, in advance of, ante-, pre-
"this did not happen until 1998"
antonyms: beyond, after

Origin
Middle English: from Old Norse und ‘as far as’ + till1 (the sense thus duplicated).
Translate until to
Use over time for: until

All of you autists went straight for optimization and language arguments. None of you took a step back and asked yourself ”who is the consumer of this code?”. Do you NEED to optimize and minimize verbosity? Is this a single person project? A critical module of a large team effort? Do you NEED to be minimal or overly vebose?

This is why I am a CTO at a Top500 and you are not.

Pro tip: learn to listen and see the whole picture of coders, teams, organizations, clients, consumers, maintainers and so on.

cringe and bluepilled

based and redpilled

"Until" is not clear. English is ambiguous. But all of OP's codes stopped at 100, not 98, so the intended meaning is clear.

Writing to console is slow as fuck. You should build a list, then output that.

int i = 0;
printf(i);
while( i < 51){
i++;
i++;
printf(i);
}


Objectively only right answer

Were basing the question off the question not whatever irrelevant examples are provided, clearly if the question were to ask for all evens including 100 it'd be phrased

1) Task: List all even numbers up to 100.

as opposed to

2) Task: List all even numbers up until 100.

Theres a reason why the specific word was chosen, if 1) was intended it costed less effort and letters to type "to" rather than "until", therefore clearly the intended purpose was to generate all evens excluding 100.

Attached: wrouang.jpg (1280x720, 54K)