Is this a valid way to check if a number is odd? I have an interview tomorrow that i am trying to prep for.
Is this a valid way to check if a number is odd? I have an interview tomorrow that i am trying to prep for
Other urls found in this thread:
Not even not zero
AHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAHAH
Were you never taught modulo?
what if the user enters the number 200003?
Make sure to put a sleep, or whatever your language equivalent is, in or else the processor could overheat.
should work fine :)
it's a little slow though, you should just fill the array with every odd number at compile time; either through constexpr functions or just typing it by hand.
what the fuck
Use
number % 2 == 0
instead. This will evaluate to true if the number is odd, and it's constant time.
never gonna make it
My bad, replace 0 with 1
am tired
if (i % 2 = 0) {
odd = true
} else { odd = false};
this is better
Poe's law in action.
what if modulo isn't allowed
def is_even(i):
return i % 2 == 0;
I honestly can't tell if this is bait
oops meant to put two = signs in both instances
Why don't you just half the input number and if the result isn't an integer then the input must be odd?
You'd need a special case for 0 obviously but wouldn't that be much simpler and less taxing on the processor?
for integers just check if the least significant bit is 1:
number & 1 == 1
why
>using modulo
>ever
if LSB == 1, the number is odd, otherwise its even
>both instances
Then you can use the Bitwise operator & to check for odd.
(number & 1) != 0
will evaluate to true if the number is odd
what if bitwise operator isn't allowed either
for starters using assignment instead of comparator operator.
Second if (i & 1) {true} else {false} is redundant code 101, just return i&1;
>input(0)
>even
just import the isOdd library
return ((input & 0x1) == 0x1)
removing != 0 makes this code actually work
this thread is proof of why a isOdd package exists in npm
see >he doesn't use the ternary operator
Then,
function isOdd(x) {
for (let i=1; i
while (n > 0)
n -= 2;
return n == -1;
Zero is even, retard.
I have an interview too. I need to print the values of an array.
array = {"Please", "Help", "user"}
I have no idea how to do it. I thought of the following.
String1 = Array[1]
String2 = Array[2]
String3 = Array[3]
print(String1)
print(String2)
print(String3)
And if there's more I could
If array.length > 3
String1 = Array[4]
String2 = Array[5]
String3 = Array[6]
And so on.
jesus christ. Is this bait? Please let this be bait.
if( int(userInput / 2.0) * 2 == userInput )
I advise you to take your time and learn the fundamentals of programming.
>not gonna make it
this guy beat me to it
>what if bitwise operator isn't allowed either
The most efficient method not being allowed?
Leave the interview.
just print from the array
I refuse to believe OP is serious right now
100001 is literally impossible to count to so you're golden OP.
if (number != 1 && number !=3 && number !=5 && number !=7 && number !=9 && number !=11 && number != 13){
cout
no you need to use a tree not an array, for faster searchinnngh
I am a little confused by what you are asking.
for (int i = 0; i < array.length; i++ ) {
print(array[i]);
}
if ( number/2 < (number - 1) / 2 ) // then even
whoops i mean
if ( number/2 > (number - 1)/2 ) // then even
mixed up the == operator, and the if else brances, this is a b8
you sure about that bud?
const e = x => x & 1 == 1 ? false : true;
thnk like a discrete mathematician user.
If a number, n, is even. Then we can say that n = 2k
Odd numbers are always 1 greater or less than an even number, so then all odds can be expressed as n = 2k + 1.
So then if 2|n (2 divides n) then n is even. Therefore if !(2|n) then n is odd.
Thus:
Odd(n) = return n % 2 != 0
>mfw this is the question 95% of your applicants fail at.
this code is easily scalable. depending on the amount of ram available, you could check for an uncountable number of odd number possibilities
nice catch OP
odd = i % 2 == 0;
so many people willing to prove how smart they are, they can't even see the obvious bait.
>assuming anything about how the computer represents data
Whew lad
They're undergrads flexing their muscles.
nice bait
im genuinely curious how many different ways you can do this (not counting arbitrarily retarded ways) So far there's 3. 2 are kind of similar, otherwise 4.
Those are my favorite interviews, the ones where you can't use a completely valid operator because they don't like it.
If you're going to control my code that much, why don't you just write it instead?
just download the is-odd npm module bruh, programming is for losers anyway
int isodd(int x)
{
return (x / 2) * 2 == x ? 0 : 1;
}
>weekly downloads 1,425,671
>version 3.0.1
>pull requests 1
then make your own
const mod = (a, b) => a - b * Math.floor(a / b);
const isEven = x => mod(x, 2) == 0;
>uncountable number of odd number
Ok, this is getting out of hand.
oh fugg it's about odd oh well
Would the endianess of the computer affect it?
My Mac Quadra disagrees with you, user.
No, because that affects how it gets stored into memory, but if you're dealing with an x-bit integer, it's going to be treated the same way when it's in a register.
LSB is "least significant bit", meaning the bit that has the minimum effect on the value of the integer.
>In computing, the least significant bit (LSB) is the bit position in a binary integer giving the units value, that is, determining whether the number is even or odd
I like lua
for i =1, 100 do
If i%2==1 then print(i) end
end
>In computing, the least significant bit (LSB) is the bit position in a binary integer giving the units value, that is, determining whether the number is even or odd
This is the correct answer, and what the interviewers are looking for. If you can't get that right you don't deserve the job.
>interviewers are looking for bithacks and not readable maintainable code
>cpp
>windows
>doesn't know basic mathematical operations
>believes he could pass the interview
Or just
if (input % 2 == 0) {
even
}
else {
odd
}
for a more mathematical solution any CS101 student could recite at the drop of a feather.
yeah that was established, and relies on mathematical operations, not bit hacks
I don't know what's worse: OP's bait or the fact that Javascriptlets actually can't modulo.
what in the actual fuck am i looking at
sageru fuck off baitposting troll
bool is_odd(int num) {
return num & 0b1;
}
Could it be more efficient than counting to:
take input x, divide that by 2 to get y,
then get z = x - y
and finally x is even if z == y
def is_even(n):
if n == 0:
return True
elif n == 1:
return False
else:
return is_even(n-2)
This is what the npm module is for, you don't have to reinvent the wheel.
>cpp
>windows
This is why OP has an interview, and you have cold tendies on a dirty plate for dinner.
>assuming endianness of a machine
brainlet cuck btfo
By all means go have interview but you will not be hired. I am one of those people who thinks that you shouldnt need math degree to do programming, but jesus christ, OP, you need some math knowledge, at least high school level and maybe first year of uni, other stuff you can pick up when needed but you need the foundation, not knowing modulus I wonder if you even finished secondary school