if a = number through number quest - if-statement

I was wondering if lua "roblox", if statment has a way to go through....
well let me just show you.
local a = some.object.value
if a == 5 or a == 6 or a == 7 ................. goes on for a long time.
looking for a short cut. kinda like
if a <= 2000 then "some code" end
but this needs to start at like 5. Instead of 1
if a == 5 "through" 2000 then BLA end
Also i know i can use a for loop to do the trick but i was just wondering if there was a way to do it without for loop?
I have googled lua wiki and looked around but i could not find anything. :( It is probably do to i don't know what this type of thing would be called.

Just formulate your condition in English:
If your number is in the interval 5-2000, number is greater than or equal 5 and number is smaller than or equal 2000.
Now translate this to Lua
number >= 5 and number <=2000
If you're dealing with integer numbers only you may of course also use number > 4 and number < 2001
See Lua Reference Manual 3.4.4: Relational Operators and
Lua Reference Manual 3.4.5: Logical Operators
If the number is in the interval both conditions will be true. true and true is true.
If any of the conditions is false either because your number is smaller 5 or greater 2000 it evaluates to false as both false and true and true and false evaluate to false.
In an if statement:
if number >= 5 and number <= 2000 then
-- do something
end

Related

How do I use the "IF statement" to select numbers from a range?

I have a set of measurements ranging from 1 to 30. Any number between 1 and 10 is "Good", any number between 11 and 24 is "Mild" while any number from 25 to 30 is "Bad". I hope to use the IF statement to do this classification but I have am yet to get it right. I have tried the following:
=IF((1<=A2<=10),"Good",IF((11<=A2<=24),"Mild",IF(A2>=25,"Bad")))
I would appreciate your kind suggestions on the most effective way to get this done.
In Excel there is no IF(x <= A <= y, ...) .
The commonly used alternative is IF(AND(x <= A, A <= y), ...).
Reference:
AND function
IF function
IF: nested formulas and avoiding pitfalls

What's the difference between LEN and <>"" when searching for values?

I initially learned that if I want to see if a cell has any contents to use if(A1<>"",.... But as I receive more and more assistance on SO, it seems most people use if(LEN(A1),... Is there a difference? Did I learn the wrong information? Should I ever opt for one over the other or just always use LEN from now on?
pretty much the same result. difference is:
LEN(A1) - checks if A1 has a length
A1<>"" - checks if A1 is not equal to "empty"
then there is a length of the formula itself (some prefer to save 1 extra character space):
A1<>"" has 6 characters compared to LEN(A1) 7 characters
the superiority of LEN comes when you need to check for character count like:
=IF(LEN(A1)=4, TRUE, FALSE)
eg. output TRUE only if A1 value has exactly 4 characters

While loop excluding multiples of specific number

I'm working on c++ and I need help writing a program that prints all numbers between 1 and 200 excluding multiples of 7. I understand how to print the number between 1 and 200, but I don't understand how to print them excluding multiples of 7.
Use the modulo operator % to determine if the number is a multiple of 7.
if number % 7 == 0, then it's a multiple of seven, and then you don't print it.
You can pre calculate the multiples of 7, storing them somewhere.
Then, for every value in the 'for' you print only if the number is not in where you stored the pre calculations.

Assistance needed with parsing a 32-bit flag

I'm working with a 32bit signed int, that should contain true/false flags for 24+ items, starting with an id of 1. So my first assumption was that each bit corresponds to its corresponding item item id. So to start off, I simply did
(flag_int >> item_id) & 1
And this worked - mostly. For example, starting with a flag int of 125831406, the expected result(left) compared to the actual result (right)
1 true true
2 true true
3 true true
4 false false
5 true true
6 true true
7 true true
8 false false
9 false false
10 false false
11 false true <<
.. ??? false
22 true false <<
23 ??? true
24 true true
25 true true
26 true true
The trouble is, not every flag ends up with known discrepancies, and when it does, it's not always on the same id, and the number of discrepancies always seems to vary between 1 and 2.
So this tells me there's some other value at play. The good news is that there's two possibilities that I know of. First, is the entry id - which in the example is 307, and second is an additional 0/1 flag, which in the example would be 1.
The bad news is, that I'm woefully deficient in bitwise operations when it comes to flags, and my attempts have been all for naught. So while I certainly don't expect the "right" answer, some ideas on what to try would be most appreciated.
The purpose of these flags was to determine whether or not certain parts of a character should be hidden, by the type of headwear they were wearing. Each ID identified a character type, and each item had two Flag IDs - one for each gender.
To avoid any extra variables - I elected to use the "Ears" flag - because unlike hair, or other parts, it should've been the easiest to spot, with the lowest chance of additional complexity. I mean, every character has ears, and only one style of ears - so it should've been pretty binary.
The discrepancies - despite how much time I spent making sure I was getting the most accurate results, were embarrassingly easy to explain - because it had nothing to do with the flags, and everything to do with the traits of these characters.
One character type of each gender would ignore their flag, because their ears were part of the base mesh, and couldn't be hidden.
two more character types- both genders, seemed to defy their flag, only because their ears weren't considered ears, so their display would be handled by a different flag.
So anyway, that was the problem. If I would've simply taken another look at the mesh of those character types, I would've saved myself a ton of time.

Prime backwards in order

An emirp (prime spelled backwards) is a pime number whose reversal is also prime. Ex. 17 & 71. I have to write a program that displays the first 100 emirps. It has to display 10 numbers per line and align the numbers properly:
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157.
I have no cue what I am doing and would love if anyone could dump this down for me.
It sounds like there are two general problems:
Finding the emirps.
Formatting the output as required.
Break down your tasks into smaller parts, and then you'll be able to see more clearly how to get the whole task done.
To find the emirps, first write some helper functions:
is_prime() to determine whether a number is prime or not
reverse_digits() to reverse the digits of any number
Combining these two functions, you can imagine a loop that finds all the numbers that are primes both forward and reversed. Your first task is complete when you can simply generate a list of those numbers, printing them to the console one per line.
Next, work out what format you want to use (it looks like a fixed format of some number of character spaces per number is what you need). You know that you have 100 numbers, 10 per line, so working out how to format the numbers should be straightforward.
Break the problem down into simpler sub-problems:
Firstly, you need to check whether a number is prime. This is such a common task that you can easily Google it - or try a naive implementation yourself, which may be better given that this is homework.
Secondly, you need to reverse the digits of a number. I'd suggest you figure out an algorithm for this on a piece of paper first, then implement it in code.
Put the two together - it shouldn't be that hard.
Format the results properly. Printing 10 numbers per line is something you should be able to figure out easily once the rest is done.
Once you have a simple version working you might be able to optimise it in some way.
A straight forward way of checking if a number is prime is by trying all known primes less than it and seeing if it divides evenly into that number.
Example: To find the first couple of primes
Start off with the number 2, it is prime because the only divisors are itself and 1, meaning the only way to multiple two numbers to get 2 is 2 x 1. Likewise for 3.
So that starts us off with two known primes 2 and 3. To check the next number we can check if 4 modulo 2 equals 0. This means when divide 2 into 4 there is no remainder, which means 2 is a factor of 4. Specifically we know 2 x 2 = 4. Thus 4 is not prime.
Moving on to the next number: 5. To check five we try 5 modulo 2 and 5 modulo 3, both of which equals one. So 5 is prime, add it to our list of known primes and then continue on checking the next number. This rather tedious process is great for a computer.
So on and so forth - check the next number by looping through all previous found primes and check if they divide evenly, if all previously found primes don't divide evenly, you have a new prime. Repeat.
You can speed this up by counting by 2's, since all even numbers are divisible by two. Also, another nice trick is you don't have to check any primes greater than the square root of the number, since anything larger would need a smaller prime factor. Cuts your loops in half.
So that is your algorithm to generate a large list of primes.
Collect a good chunk of them in an array, say the first 10,000 or so. And then loop through them, reverse the numbers and see if the result is in your array. If so you have a emirp, continue until you get the first 100 emirps
If the first 10,000 primes don't return 100 emirps. Move on to the next 10,000. Repeat.
For homework, I would use a fairly simplistic isPrime function, pseudo-code along the lines of:
def isPrime (num):
set testDiv1 to 2
while testDiv1 multiplied by testDiv1 is less than or equal to num:
testDiv2 = integer part of (num divided by testDiv1)
if testDiv1 multiplied by testDiv2 is equal to num:
return true
Add 1 to testDiv1
return false
This basically checks whether the number is evenly divisible by any number between 2 and the square root of the number, a primitive primality check. The reson you stop at the square root is because you would have already found a match below it if there was one above it.
For example 100 is 2 times 50, 4 times 25, 5 time 20 and 10 times 10. The next one after that would be 20 times 5 but you don't need to check 20 since it would have been found when you checked 5. Any positive number can be expressed as a product of two other positive numbers, one below the square root and one above (other than the exact square root case of course).
The next tricky bit is the reversal of digits. C has some nice features which will make this easier for you, the pseudo-code is basically:
def reverseDigits (num):
set newNum to zero
while num is not equal to zero:
multiply newnum by ten
add (num modulo ten) to newnum
set num to the integer part of (num divided by ten)
return newNum
In C, you can use int() for integer parts and % for the modulo operator (what's left over when you divide something by something else - like 47 % 10 is 7, 9 % 4 is 1, 1000 % 10 is 0 and so on).
The isEmirp will be a fairly simplistic:
def isEmirp (num):
if not isPrime (num):
return false
num2 = reverseDigits (num)
if not isPrime (num2):
return false
return true
Then at the top level, your code will look something like:
def mainProg:
create array of twenty emirps
set currEmirp to zero
set tryNum to two
while currEmirp is less than twenty
if isEmirp (tryNum):
put tryNum into emirps array at position currEmirp
add 1 to currEmirp
for currEmirp ranging from 0 to 9:
print formatted emirps array at position currEmirp
print new line
for currEmirp ranging from 10 to 19:
print formatted emirps array at position currEmirp
print new line
Right, you should be able to get some usable code out of that, I hope. If you have any questions of the translation, leave a comment and I'll provide pointers for you, rather than solving it or doing the actual work.
You'll learn a great deal more if you try yourself, even if you have a lot of trouble initially.