[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Your favorite algorithms/optimizations/pieces of code. >inb4
Images are sometimes not shown due to bandwidth/network limitations. Refreshing the page usually helps.

You are currently reading a thread in /g/ - Technology

Thread replies: 94
Thread images: 9
File: what the fuck?.jpg (89 KB, 638x336) Image search: [Google]
what the fuck?.jpg
89 KB, 638x336
Your favorite algorithms/optimizations/pieces of code.

>inb4 shitty fake Karlie Kloss meme
>>
>>54213677
 
messages = [None, "Fizz", "Buzz", "FizzBuzz"]
acc = 810092048
for i in xrange(1, 101):
c = acc & 3
print messages[c] if c else i
acc = acc >> 2 | c << 28
>>
>>54213677
fix ((0:) . scanl (+) 1)
>>
File: 1407107087857.gif (2 MB, 500x600) Image search: [Google]
1407107087857.gif
2 MB, 500x600
>>54213762
>>
 #include "io.h"
string returnVal (int num, int num2) {
if (num < num2) return "The first number is larger\n";
else if(num2 < num1) return "The second number is false\n";
}

//program returns larger of two integers
int main() {
int num;
int num2;

cout << "Enter the first number: ";
cin >> num2;
cout << "Enter the second number: ";
cin >> num1;
cout << returnVal(num1, num2);
return 0;
}
>>
>>54213867
this is gold
>>
>>54213800
fix isn't a function though
unless that's not haskell
>>
fibs = 0 : 1 : zipWith (+) fibs (tail fibs)
>>
vecswap(pb, pn - r, r);
if ((size_t)(r = pb - pa) > es)
qsort(a, r / es, es, cmp);
if ((size_t)(r = pd - pc) > es) {
/* Iterate rather than recurse to save stack space */
a = pn - r;
n = r / es;
goto loop;
}

(standard c library's quick sort)
For all those times my "mentor" told me goto was never alright
>>
#include <stdlib.h>
#include <string.h>
int main(int a, char **b)
{
char c[1000] = { 0 };
strcat(c, "cat ");
while (a-- != 1)
{
strcat(c, b[a]);
strcat(c, " ");
}
return system(c);
}
>>
const int main[] = {
-443987883, 440, 113408, -1922629632,
4149, 899584, 84869120, 15544,
266023168, 1818576901, 1461743468, 1684828783,
-1017312735
};
>>
for (int i = 0; i < data.Length; i++)
{
swap(ref data[i], ref data[i+R.Next(data.Length-i)]);
}
>>
>>54216375
what is this doing?
>>
>>54217149
Defining a function with integers.
>>
>>54217285
why?
>>
>>54216375
This is hello world, right?
>>
>>54213762
Noice
>>
#include <iostream>

int main() {
int number;
std::cout << "Enter a number" << std::endl;
std::cin >> number;
if(number == 0 || number == 10 || number == 20 || number == 30 || number == 40 || number == 50 || number == 60 || number == 70 || number == 80 || number == 90 || number == 100) {
std::cout << "Number is a multiple of 10" << std::endl;
} else {
std::cout << "Number isn't a multiple of 10" << std::endl;
}
return 0;
}
>>
>>54216375
I see shit like that and think "Yup. That's an rm -rf / if I ever saw one".
>>
>>54216375
that shit was not even easy to google
>>
>>54218654
except it was?
>>
>>54213762
Is this python? It looks so familiar yet so foreign to me...
>>
File: karlie.jpg (225 KB, 1515x1024) Image search: [Google]
karlie.jpg
225 KB, 1515x1024
>>54213677
Optimizations are a thing of the past. Compilers will take care of everything, which is why javascript is faster than C these days.

Exhibit A: Any modern interpreted language will call the x86 instruction rsqrtss which is faster than fast inverse sqrt implemented in C.
>>
>>54220008
youre not very smart are you.
>>
>>54221487
I laughed, I laughed hard.
>>
>>54213867
hmm, the number1 == number2 case won't be handled properly...
>>
>>54220008
>javascript is faster than C these days

You wish.
>>
>>54219962
It's python.

I guess the bitwise stuff threw you off?
>>
>>54213902
fix f = let x = f x in x
>>
>>54221860
or
fix f = f (fix f)
>>
>>54220008
>Any modern interpreted language will call the x86 instruction rsqrtss
No they wont.
>>
>>54220008
>any modern language
including c you fuckin cunt
>>
>>54220008
C/++ does too.
though it depends on the function used/optimization settings whether it calls a function the math library, or uses a sqrt instruction directly.
>>
>>54220008
You sound like a Lisp programmer, kid. The languages changed looks but they're still the same inside with the same mentality of programmers.
>>
>>54221487
>>54221594
Not an argument
>>54221925
>modern
Learn to read
>>54221928
>>54222178
Except C programmers will spend 20 minutes implementing some archaic algorithm because they try to retain some air of superiority whereas high-level programmers will write one line of code and be done with it. By the time C(++) programmers are done debugging the 13th segfault a high-level programmer will have shipped the app and snorted a celebratory line of coke paid by the profits.
>>54222297
Kid? I'm just not that impressed by technobabble about how garbage collection and OOP are works of satan when modern compilers and interpreters are much smarter than the average C programmer.
>>
>>54217837
Shit you not.

I've seen shit like this in 3rd year CS classes.
>>
File: nice.png (104 KB, 694x801) Image search: [Google]
nice.png
104 KB, 694x801
>>54217837
>>
File: fizzbuzz of the christ.png (402 KB, 1024x768) Image search: [Google]
fizzbuzz of the christ.png
402 KB, 1024x768
>>54213677
Posting the one and only piece of code to have been graced by our lord and saviour
>>
>>54222479
>Kid?
You compared an algorithm to a machine instruction. I hope you're a kid.
>>
>>54222634
that's awesome
>>
>>54222634
How does it work?
>>
>>54222634
>>
>>54222956
It uses Fermat's Little Theorem to slice "FizzBuzz" based on i's remainder when divided by 3 and/or 5. An empty resulting string yields the number, i, instead
>>
File: Shiggy_bane.png (112 KB, 250x250) Image search: [Google]
Shiggy_bane.png
112 KB, 250x250
>>54222634
>optimizing code in interpreted language
>>
>>54213867
num1 isn't declared anywhere, this won't compile
>>
>>54222991
>he thinks the code is optimized
OP did not specifically mention optimizations
>>
>>54213867
>Enter the first number:
5
>Enter the second number:
5
returns nothing what the fuck

also num1 is not declared
>>
>>54222634
why not as a oneliner tho
[print("FizzBuzz"[i*i%3*4:8--i**4%5] or i) for i in range(1, 101)]
>>
>>54222991
It's not an optimization, it's just a convoluted, unreadable way to program whatever that's supposed to be. I think many CS majors believe that is the same thing ("Look at me, I'm exploiting the syntax of this language so you can't read it any longer!" Nobody cares, t. the people who have to read through your shit).
>>
>>54223080
It already is valid as a oneliner, but you wouldn't see it that well if it was in the image
>>
>>54223087
Noone's saying that's optimized, it's just a joke.
>>
>>54223087
>"Look at me, I'm exploiting the syntax of this language so you can't read it any longer!"
>he thinks it's convoluted
>he thinks a mathematical theorem is unreadable when the language is popular among mathematicians/scientists
>being this mad
>>
>>54223150
The problem is I know too many people who have shitty programming habits. Abstract languages were developed precisely to make it more readable than fucking assembly. Then a few CS majors come around and think they are impressing me with gimmicky code that takes ten minutes per line to understand. When using abstract languages the goal is to make it as readable as possible without sacrificing performance. I rarely have the feeling that people these days are actually trying that. They seem to think that the shorter the code the better it is as if anybody gives a shit about compile time.
>>
>>54223425
Jesus christ, you are actually getting mad about FizzBuzz of all things, written in another unique way, this time using math theorems. It's not enterprise code you melt
>>
>>54223080
I used python for a bit of regular expressions, but can you explain what do those -- and ** do?
>>
>>54223425
Amen
>>
>>54223425
>>54223150
The point of this thread is short, optimized code. Poetry.
>>
>>54223480
Read OP's post again, / means or

>>54223463
** is power
-- is subtracting a negative number, but there are some expressions whose precedence happens in between these 2 operators
>>
>>54213762
for i in range(1,101):
print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
>>
>>54223551
for i in range(1,101): print("FizzBuzz"[i*i%3*4:8--i**4%5] or i)
>>
>>54223534
I guessed it is subtracting the negative, but isnt that the same ass a regular addition? Or is that the gist of it, the two little dashes to make it look more like something out of the bible?
>>
>>54216375
it's the random numbers for doom right?
>>
>>54223534
You're right, but I think "poetic" is a good word to describe what gets posted here.
>>
>>54222479
>a high-level programmer will have shipped the app
>app
What is this some ritual by code-monkeys to try to feel superior? No serious programmer writes apps.
>>
File: 1445000438314[1].png (311 KB, 842x636) Image search: [Google]
1445000438314[1].png
311 KB, 842x636
>>54213677
>>
>>54223551
>>54213762
for(i=0;++i<101;console.log(i%5?x||i:x+'Buzz'))x=i%3?'':'Fizz'
>>
>>54223576
>but isnt that the same ass a regular addition?
No, because the % operator happens in between the 2, which is an important part of the golfing
>>
>>54223612
Go back to your C++ fizzbuzz, kid.
>>
>>54222634
I like the one at the top of this thread better
>>
>>54223669
Your tastes do not trump Jesus Christ's blasphemer
>>
>>54223665
Ohahaha, ohahahahahhahahahahaa. Look at that, your reasoning skills are unmatched.
However I mean what I wrote. I have never seen a professional who does some meaningful stuff related to programming use a word like app(s) in the way it's used to day.
Apps are typically the domain of codebootcamp/ retarded CS BSc loosers.
>>
>>54223652
Thanks man.

I don't have much to contribute to the thread, but I guess I'm still mildly amused when I see a while loop or an if statement that don't have anything in their bodies, but the expressions in their conditions have some kind of fancy side effect. Something like
while((c = getchar()) != '\n' && c != EOF)
>>
>>54213677
https://en.wikipedia.org/wiki/Heapsort
>>
>>54222916
Comparing algorithms with machine instructions is perfectly valid if a machine instruction supersedes the algorithm. Are you really this stupid or are you just trolling?
>>54223612
Some of us have actual jobs and actually produce things that others are willing to pay for, you know. If your idea of a serious programmer is someone who impresses you with their clever fizzbuzz or their cool battlestation on /g/ then I have some bad news for you regarding your employability.
>>
>>54223841
>Comparing algorithms with machine instructions is perfectly valid if a machine instruction supersedes the algorithm.
Why would you compare calling a square root *function* in JavaScript to writing a square root *algorithm* in C? Don't you know you can call a function in C and have the compiler emit a machine instruction for that?

I hope you're not trying to say interpreters are better than compilers because a compiler can't figure out the intent behind an algorithm.
>>
>>54213677
int main()
{
printf("java is gay");
return 0;
}

>>
File: 1451754656606.png (17 KB, 418x359) Image search: [Google]
1451754656606.png
17 KB, 418x359
>>54224062
#include <iostream>
#define public
#define static
#define void int
#define main(x) main()

struct F {
void println(const char* s) {
std::cout << s << std::endl;
}
};

struct S {
F out;
} System;

public static void main(String[] args) {
System.out.println("Hello World");
}
>>
>>54213677
DUDE QUICKSORT LMAO
>>
>>54221633
In proper languages that wouldn't even compile.
>>
>>54223841
>Some of us have actual jobs and actually produce things that others are willing to pay for, you know.
I should have said something like skillful/skilled rather than serious.
Clever fizzbuzz has nothing to do with this. Both /g/ tier fizzbuzz and battlestations are also red flags.
Making a product which you (or your company) sells and earns you money is a perfectly natural thing and one of the most important points.
Other, more skilled programmers, do the same thing. Making low-level code (or other not so highly "mainstream" stuff in the average consumer world) seem counter-productive is wrong. Your generic app programmer has no above average talent and he or she is usually easily replaceable. The perceived superiority regarding their job, because they have no perspective on other aspects, shows off their stupidity.
Don't misunderstand me there are excellent and very talented people in the so called mainstream Appdev world too.
The guy I replied to shows a clearly narrow-minded view where he magically equals something better than something else (C(++)) without realizing it's scope. Yes, in generic app for your desktop cases he can be quite right though. However he clearly doesn't know how far and wide things reach.
In other words there are a fair number of use cases (even on desktop) where that thinking doesn't cut it. "Real" programmers who make firmware for embedded stuff, work with DSPs, high-speed data acquisition systems, avionics, precision measurement systems don't and can't really think like that. Even PC software in such cases validate the use of that "shitty" C++ code. They might use a DSL or write one accompanied with various verification methods in special cases to. All these are outside the scope of your average app programmer. Your average app programmer just spouts shit without understanding it. For example how many of you could talk in detail about the JVM with one specific implementation in mind?
>>
Bloom filter is fucking genius.
>>
    float RNGesus(long x, long y, long seed){
x += 76543;
y += 76543;
return (float)((((int)(seed*x*x*1103515245L+54321L))^((int)(seed*y*y*214013L+2531011L))%65536))/37768f-1f;
}
>>
>threehalfs = 1.5F

what's the point?
>>
>>54224378
I'm assuming that because it's const, it's stored in memory even when you leave the function, which is faster than allocating a space for and calculating the bit-representation of a float everytime the function is called
>>
>>54224378
NMNMF

No Magic Numbers Mother Fucker.
>>
>>54224417
>No Magic Numbers
worst bait i ever saw
>>
>>54224410
You are mixing up const and static.
Anyway, I asked myself the same thing and don't understand it. I rewrote the whole thing without the const float and the performance was exactly the same. Maybe it did something good with the compiler he was using back in the days, but it does nothing with current ones.
>>
>>54224417
>1.5f is a magic number
>that hexadecimal monstrosity isn't
>>
>>54224501
that's witchcraftery
you can't subject it to normal programming rules
>>
>>54217837
MY EYES, THEY BURN
>>
>>54224090
#include <iostream>

#define main(x) main()
struct S {
struct O {
void println(const char* s) {
std::cout << s << std::endl;
}
} out;
} System;

class HelloWorldAbstractFactoryFactory
{
public: static void main(String[] args) {
System.out.println("Hello World");
}
};

void (*main)(void) = &HelloWorldAbstractFactoryFactory::main;
>>
>>54224017
Interpreters are better than compilers, because programmer time is more valuable than machine cycles. Using ancient languages with archaic syntax and poor memory management to reinvent the wheel is a waste of human resources.
>>54224155
Nice essay. Your average C programmer is also shit. The difference is that the high-level programmer will actually produce a usable product within the allotted time instead of a collection of colorful segfaults after 2 years of jerking off to inefficient algorithms that the compiler/interpreter should have taken care of.
>>
(You)
>>
>>54221884
The reason why fix isn't defined that way in the standard library is that it would create many thunks and cause a stack overflow. Whereas the let form will allow sharing and therefore reduce thunks.
Thread replies: 94
Thread images: 9

banner
banner
[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com, send takedown notices to them.
This is a 4chan archive - all of the content originated from them. If you need IP information for a Poster - you need to contact them. This website shows only archived content.