Ad

Uses BigInteger operations instead of string, saves 0.002ms, down to 0.004ms (4us)

Code
Diff
  • import java.math.BigInteger;
    import java.util.stream.Collectors;
    
    public class MaxNumber
    {
    	// Kudos to java users idk how u program with this shit language
        public static BigInteger print(long number)
    	{
    		BigInteger x = BigInteger.ZERO;
            int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    		
    		while (number != 0)
    		{
    			digits[(int)(number % 10)]++;
    			number /= 10;
    		}
    		
    		for (int i = 9; i >= 0; --i)
    		{
    			while (digits[i] > 0)
    			{
    				x = x.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    				digits[i]--;
    			}
    		}
    		
    		return x;
        }
    }
    
    • import java.math.BigInteger;
    • import java.util.stream.Collectors;
    • public class MaxNumber
    • {
    • // Kudos to java users idk how u program with this shit language
    • public static BigInteger print(long number)
    • {
    • BigInteger x = BigInteger.ZERO;
    • int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    • while (number != 0)
    • {
    • digits[(int)(number % 10)]++;
    • number /= 10;
    • }
    • number = 0;
    • String szDecimal = "";
    • for (int i = 9; i >= 0; --i)
    • {
    • while (digits[i] > 0)
    • {
    • szDecimal += (char)((int)('0') + i);
    • number += i;
    • x = x.multiply(BigInteger.TEN).add(BigInteger.valueOf(i));
    • digits[i]--;
    • }
    • }
    • return new BigInteger(szDecimal);
    • return x;
    • }
    • }

Average execution time is 0.006ms (6us)

Code
Diff
  • import java.math.BigInteger;
    import java.util.stream.Collectors;
    
    public class MaxNumber
    {
    	// Kudos to java users idk how u program with this shit language
        public static BigInteger print(long number)
    	{
            int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    		
    		while (number != 0)
    		{
    			digits[(int)(number % 10)]++;
    			number /= 10;
    		}
    		number = 0;
    		String szDecimal = "";
    		for (int i = 9; i >= 0; --i)
    		{
    			while (digits[i] > 0)
    			{
    				szDecimal += (char)((int)('0') + i);
    				number += i;
    				digits[i]--;
    			}
    		}
    		
    		return new BigInteger(szDecimal);
        }
    }
    
    • import java.math.BigInteger;
    • import java.util.stream.Collectors;
    • public class MaxNumber {
    • public static BigInteger print(long number) {
    • return new BigInteger(Long.toString(number)
    • .chars()
    • .mapToObj(c -> String.valueOf((char) c))
    • .sorted((a, b) -> b.compareTo(a))
    • .collect(Collectors.joining()));
    • public class MaxNumber
    • {
    • // Kudos to java users idk how u program with this shit language
    • public static BigInteger print(long number)
    • {
    • int digits[] = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
    • while (number != 0)
    • {
    • digits[(int)(number % 10)]++;
    • number /= 10;
    • }
    • number = 0;
    • String szDecimal = "";
    • for (int i = 9; i >= 0; --i)
    • {
    • while (digits[i] > 0)
    • {
    • szDecimal += (char)((int)('0') + i);
    • number += i;
    • digits[i]--;
    • }
    • }
    • return new BigInteger(szDecimal);
    • }
    • }

M̸̥̦̼̮̃è̷͔̬̰̹͠s̴̭̀̇͒̐s̷̟̤̦͈͕̃̅̎e̷̹̐̆̍̃d̴͎̻̜̿͆Ṷ̵̯̮͙͘͜p̴̝̎F̶͔͋́ụ̵̡̯̉̌̀͜ṋ̸̃c̴̠̟͚̦͔͒͘t̷̘̑͌̓̌̕i̸̛̱͌̎͠ȯ̴̢͇̪̲͔̾̐̚n̶̗͕̅̊̌ <- Pointer to a function int (*)(int)

F̶̝͈̪̰͑͐̍̄͑͜u̴̧̳͉̞̬͠͝n̴̮͛c̷̪̻̆t̷̼̉ī̵̱̲̳̖̤ö̴́͑́͒͜͝ņ̴̹̯̝̀̿̉́Ī̶̪͉̭͋̏͠m̵̯͈̼̲̩̆̒͂p̷͚̎͋̊l̶̨͙̮̯̒̀͋̕ͅe̸̲̬̫̍̽͝͝m̵̗̖̮̿e̷͛̓͋͜͝n̷̨̹̰̬̊t̸̥̖́a̷̡͚͆̍̿̋̌t̶̮͔͑̌̓i̷͉̖̱̖̾͂ö̵̡̠̺́̊͊n̵͎͙͔̣͗͘ <- A constant string literal

x̷͓͖̙̕ <- parameter integer x

<-- Explanation -->

The function odd_even treats the memory address of the string literal as a function pointer, and calls it with parameter x.

The string literal is the binary of this ASM function:

mov eax, edi; Moves the first parameter into EAX

not eax; Applies NOT operator (compliment) to EAX

and eax, 1; Applies AND operator to EAX and constant value 1

ret; Returns the value in EAX

Essentially this returns 1 if even, 0 if odd

Code
Diff
  • #include <stdbool.h>
    #include <stdio.h>
    
    typedef int (*M̸̥̦̼̮̃è̷͔̬̰̹͠s̴̭̀̇͒̐s̷̟̤̦͈͕̃̅̎e̷̹̐̆̍̃d̴͎̻̜̿͆Ṷ̵̯̮͙͘͜p̴̝̎F̶͔͋́ụ̵̡̯̉̌̀͜ṋ̸̃c̴̠̟͚̦͔͒͘t̷̘̑͌̓̌̕i̸̛̱͌̎͠ȯ̴̢͇̪̲͔̾̐̚n̶̗͕̅̊̌)(int);
    
    const char F̶̝͈̪̰͑͐̍̄͑͜u̴̧̳͉̞̬͠͝n̴̮͛c̷̪̻̆t̷̼̉ī̵̱̲̳̖̤ö̴́͑́͒͜͝ņ̴̹̯̝̀̿̉́Ī̶̪͉̭͋̏͠m̵̯͈̼̲̩̆̒͂p̷͚̎͋̊l̶̨͙̮̯̒̀͋̕ͅe̸̲̬̫̍̽͝͝m̵̗̖̮̿e̷͛̓͋͜͝n̷̨̹̰̬̊t̸̥̖́a̷̡͚͆̍̿̋̌t̶̮͔͑̌̓i̷͉̖̱̖̾͂ö̵̡̠̺́̊͊n̵͎͙͔̣͗͘[] = "\x89\xF8\xF7\xD0\x83\xE0\x01\xC3";
    
    bool odd_even(int x̷͓͖̙̕)
    {
        return ((M̸̥̦̼̮̃è̷͔̬̰̹͠s̴̭̀̇͒̐s̷̟̤̦͈͕̃̅̎e̷̹̐̆̍̃d̴͎̻̜̿͆Ṷ̵̯̮͙͘͜p̴̝̎F̶͔͋́ụ̵̡̯̉̌̀͜ṋ̸̃c̴̠̟͚̦͔͒͘t̷̘̑͌̓̌̕i̸̛̱͌̎͠ȯ̴̢͇̪̲͔̾̐̚n̶̗͕̅̊̌)&F̶̝͈̪̰͑͐̍̄͑͜u̴̧̳͉̞̬͠͝n̴̮͛c̷̪̻̆t̷̼̉ī̵̱̲̳̖̤ö̴́͑́͒͜͝ņ̴̹̯̝̀̿̉́Ī̶̪͉̭͋̏͠m̵̯͈̼̲̩̆̒͂p̷͚̎͋̊l̶̨͙̮̯̒̀͋̕ͅe̸̲̬̫̍̽͝͝m̵̗̖̮̿e̷͛̓͋͜͝n̷̨̹̰̬̊t̸̥̖́a̷̡͚͆̍̿̋̌t̶̮͔͑̌̓i̷͉̖̱̖̾͂ö̵̡̠̺́̊͊n̵͎͙͔̣͗͘[0])(x̷͓͖̙̕);
    }
    
    • #include <stdbool.h>
    • #include <stdio.h>
    • bool odd_even(int n)
    • typedef int (*M̸̥̦̼̮̃è̷͔̬̰̹͠s̴̭̀̇͒̐s̷̟̤̦͈͕̃̅̎e̷̹̐̆̍̃d̴͎̻̜̿͆Ṷ̵̯̮͙͘͜p̴̝̎F̶͔͋́ụ̵̡̯̉̌̀͜ṋ̸̃c̴̠̟͚̦͔͒͘t̷̘̑͌̓̌̕i̸̛̱͌̎͠ȯ̴̢͇̪̲͔̾̐̚n̶̗͕̅̊̌)(int);
    • const char F̶̝͈̪̰͑͐̍̄͑͜u̴̧̳͉̞̬͠͝n̴̮͛c̷̪̻̆t̷̼̉ī̵̱̲̳̖̤ö̴́͑́͒͜͝ņ̴̹̯̝̀̿̉́Ī̶̪͉̭͋̏͠m̵̯͈̼̲̩̆̒͂p̷͚̎͋̊l̶̨͙̮̯̒̀͋̕ͅe̸̲̬̫̍̽͝͝m̵̗̖̮̿e̷͛̓͋͜͝n̷̨̹̰̬̊t̸̥̖́a̷̡͚͆̍̿̋̌t̶̮͔͑̌̓i̷͉̖̱̖̾͂ö̵̡̠̺́̊͊n̵͎͙͔̣͗͘[] = "\x89\xF8\xF7\xD0\x83\xE0\x01\xC3";
    • bool odd_even(int x̷͓͖̙̕)
    • {
    • return n < 0 || n > 1 ? odd_even(n + 2 * (n < 0 ? 1 : -1)) : !n;
    • }
    • return ((M̸̥̦̼̮̃è̷͔̬̰̹͠s̴̭̀̇͒̐s̷̟̤̦͈͕̃̅̎e̷̹̐̆̍̃d̴͎̻̜̿͆Ṷ̵̯̮͙͘͜p̴̝̎F̶͔͋́ụ̵̡̯̉̌̀͜ṋ̸̃c̴̠̟͚̦͔͒͘t̷̘̑͌̓̌̕i̸̛̱͌̎͠ȯ̴̢͇̪̲͔̾̐̚n̶̗͕̅̊̌)&F̶̝͈̪̰͑͐̍̄͑͜u̴̧̳͉̞̬͠͝n̴̮͛c̷̪̻̆t̷̼̉ī̵̱̲̳̖̤ö̴́͑́͒͜͝ņ̴̹̯̝̀̿̉́Ī̶̪͉̭͋̏͠m̵̯͈̼̲̩̆̒͂p̷͚̎͋̊l̶̨͙̮̯̒̀͋̕ͅe̸̲̬̫̍̽͝͝m̵̗̖̮̿e̷͛̓͋͜͝n̷̨̹̰̬̊t̸̥̖́a̷̡͚͆̍̿̋̌t̶̮͔͑̌̓i̷͉̖̱̖̾͂ö̵̡̠̺́̊͊n̵͎͙͔̣͗͘[0])(x̷͓͖̙̕);
    • }

Uses a macro, no function calling, compiler will do it completely inline, no extra memory usage.

Code
Diff
  • #ifndef div2
    #define div2(x) (x>>1)
    #endif
    • typedef unsigned long long ull;
    • ull div2(ull a) { return a >> 1; }
    • #ifndef div2
    • #define div2(x) (x>>1)
    • #endif

Simple calculator with add, subtract, multiply, divide and modulus operators.

Updates: Removed use of stringstream. Previous implementation used stringstream as a wrapper for std::to_string() function, new implementation removes all stringstream use and replaces with constant strings and std::to_string calls directly.

Code
Diff
  • #include <string>
    
    std::string calculator(int a, char op, int b)
    {
        switch (op)
        {
            case '+': { return std::to_string(a + b); }
            case '-': { return std::to_string(a - b); }
            case '*': { return std::to_string(a * b); }
            case '/': { if (!b) return "Invalid Input!"; else return std::to_string(a / b); }
            case '%': { return std::to_string(a % b); }
            default: return "Invalid Input!";
        }
    }
    
    • #include <ostream>
    • using namespace std;
    • #include <string>
    • string calculator(int a, char op, int b) {
    • stringstream s;
    • switch (op) {
    • case '+': {s<<a+b; return s.str();}
    • case '-': {s<<a-b; return s.str();}
    • case '*': {s<<a*b; return s.str();}
    • case '/': {b != 0 ? s<<a/b : s<<"Invalid Input!"; return s.str();}
    • case '%': {s<<a%b; return s.str();}
    • std::string calculator(int a, char op, int b)
    • {
    • switch (op)
    • {
    • case '+': { return std::to_string(a + b); }
    • case '-': { return std::to_string(a - b); }
    • case '*': { return std::to_string(a * b); }
    • case '/': { if (!b) return "Invalid Input!"; else return std::to_string(a / b); }
    • case '%': { return std::to_string(a % b); }
    • default: return "Invalid Input!";
    • }
    • }

just gotta make sure

Code
Diff
  • def return_hundred(n = 100):
        for i in range(1):
            if True:
                if True:
                    if True:
                        if True:
                            if True:
                                if True:
                                    if n == 100:
                                        if n != 99:
                                            if n != 101:
                                                if n > 99:
                                                    if n < 101:
                                                        if n == 100:
                                                            return n;
        
    • def return_hundred(n = 100):
    • print("")
    • return {i: i + 1 for i in range(n)}[sorted({i: i + 1 for i in range(n)})[-1]]
    • for i in range(1):
    • if True:
    • if True:
    • if True:
    • if True:
    • if True:
    • if True:
    • if n == 100:
    • if n != 99:
    • if n != 101:
    • if n > 99:
    • if n < 101:
    • if n == 100:
    • return n;