How to have more than 3 "If" arguments in Google spreadsheet? - if-statement

I am trying to say that if the number is 4 then it comes out as 25 in the answer box
4.01 - 5 = 20
5.01 - 6 = 15
6.01 - 7 = 10
7.01 - 8 = 5
However, it only let me insert 3 arguments into this string
=if(H6<4.01<5.01<6.01<7.01<8.01,"25","20","15","10","5","0")
This is too many and it won't work. Please help

I don't know what exactly are your if conditions, as it is not very clear from the question.
But in Google Spreadsheet you can try like this
=IF(H6<4.01, 25, IF(H6<5.01, 20, IF(H6<6.01, 15, IF(H6<7.01, 10, IF(H6<8.01, 5, 0)))))
You can nest IF conditions inside another IF condition.

Use IFS
Sample usage
IFS(A1>90, "A", A1>80, "B", A1>70, "C")
IFS({A1:A5} > 3, "bigger", {A1:A5} = 3, "equal")

It's likely an XY problem, since you don't really need IFs in this case. The main formula can be simplified to just =5*(9 - CEILING.PRECISE(A1)). The function CEILING.PRECISE can also be changed to CEILING, CEILING.MATH, ISO.CEILING or ROUNDUP in this case
If you want to limit the result to 25 for for inputs <= 4 and 0 for values > 8 then just clamp it with any appropriate way
=5*(9 - CEILING.PRECISE(MIN(MAX(A1, 4), 9)))
=5*(9 - MEDIAN(4, CEILING.PRECISE(A1), 9))
=5*(9 - CEILING.PRECISE(MEDIAN(4, A1, 9)))
=MEDIAN(0, 5*(9 - CEILING.PRECISE(A1)), 25)

Related

C++/Arduino 7digit doesn't show the 5 or 6

My problem is simple but I can't seem to fix it.
I'm trying to get the basic numbers on my 7 digit display (0-9). I'm doing this using Binary code for the output.
But somehow my display displays a 9 while it's actually the 5 that needs to be shown. Also if I want to display the 6 it displays a 8.
Can someone help me?
This are the binary codes I'm using.
0 = B00000011
1 = B10011111
2 = B01001011
3 = B00001101
4 = B10011001
5 = B01001001
6 = B01000001
7 = B00011111
8 = B00000001
9 = B00001001
Without access to the board you're using, it's difficult to pinpoint exactly what's going on, but I can make some educated guesses. Since you're learning, I'll be a bit longwinded (as is my nature anyway).
Here is a diagram of a 7 segment display, and its labels (side by side)
-- aa
| | f b
-- gg
| | e c
-- dd
From your code, it appears that the wiring from your values to the display is as follows within a byte (bit numbers on top, X for unused), where 0 is ON and 1 is OFF:
76543210
abcdefgX
Based on that, the values used in your table should be:
0 = B00000011
1 = B10011111
2 = B00100101
3 = B00001101
4 = B10011001
5 = B01001001
6 = B01000001
7 = B00011111
8 = B00000001
9 = B00001001
Unless I've made a mistake, which is quite possible, I think your implementation of "2" is incorrect. This is further likely because the "2" value should have 5 "ON" bits, and there are only four 0's in your implementation.
Since you thought the 2 was correct, this implies that the wire for the 6th bit ("b" in the 7-segment diagram above) is loose or miswired (or possibly shorted to a different wire) because it is apparently still "ON" when a "1" is on that bit. This would cause a 6 to show as an 8, and a 5 to show as a 9, as you described.

PQ - Function.InvokeAfter() - real delay

Trying to calculate real delay between InvokeAfter-function's executions.
Function is supposed to fire five times a second
index delay now
0 0 18:47:33
1 0 18:47:33
2 0 18:47:33
3 0 18:47:33
4 0 18:47:33
5 1 18:47:34
6 1 18:47:34
7 1 18:47:34
8 1 18:47:34
9 1 18:47:34
10 2 18:47:35
11 2 18:47:35
12 2 18:47:35
13 2 18:47:35
14 2 18:47:35
...
But I get this
Column real_delay is a difference between this row and previous
CODE
let
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each Number.IntegerDivide([Column1], 5)),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(
()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))
),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
What's wrong with code? Or maybe with InvokeAfter-function???
5 times a second means you should be waiting (second / 5) = 0.2 fractional seconds each invocation.
If you run this code:
let
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each 0.2),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(
()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))
),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
you'll see the function was invoked about 5 times per second.
== SOLUTION ==
Here is my own solution. A surprising one, I think...
NEW CODE
let
threads=5,
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each if Number.Mod([Column1], threads)=0 and [Column1]>0 then 1 else 0),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
The original idea was the multithreading parsing. And since there were some limits for simultaneous connections, I had to adapt.
I thought there is a "null-zero-start" moment, after which function is invoked - the moment, when cell is calculated (all cells almost at the same time). And second parameter means a delay after this start point. But it appears to accumulate all the delays. Very strange behaviour, imho...
So I solved the problem, but still do not understand why =)

Powerball number generator

To win the Powerball lottery (an extremely unlikely event so don't waste your time) you have to pick six numbers correctly. The first five numbers are drawn from a drum containing 53 balls and the sixth is drawn from a drum containing 42 balls. The chances of doing this are 1 in 120,526,770.
The output needs to be in the form:
Official (but fruitless) Powerball number generator
How many sets of numbers? 3
Your numbers: 3 12 14 26 47 Powerball: 2
Your numbers: 1 4 31 34 51 Powerball: 17
Your numbers: 10 12 49 50 53 Powerball: 35
import random
#Powerball
print "Offical Powerball number generaor"
x = int(raw_input("How many sets of numbers? "))
z = range(1,42)
z1 = random.choice(z)
def list1():
l1=[]
n=1
while n<=5:
y = range(1,53)
y1 = random.choice(y)
l1.append(y1)
n +=1
print sorted(l1)
i=1
while i<=x:
# print "Your numbers: " + list1() + "Powerball: "+ str(z1)
print list1()
raw_input("Press<enter>")
My code's output goes on a infinite loop. I have to kill it. And the message is:
None
[2, 7, 22, 33, 42]
None
[15, 19, 19, 26, 48]
None
[1, 5, 7, 26, 41]
None
[7, 42, 42, 42, 51]
None
..... etc ....
while i<=x: - you never increment i, so it is stuck in your last loop...
To avoid such things and remove the noise of i+=1 lines in your code I suggest using for loops for i in range(x) and for n in range(5).
Better yet, the following expression can replace list1:
[random.choice(range(1,53)) for x in xrange(5)]
At least, that does the same as your code. But what you probably really want (to avoid the same ball being chosen twice) is:
random.sample( range(1,53), 5 )

Calculating Hamming Sequence in C++ (a sequence of numbers that has only 2, 3, and 5 as dividers) [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Generating a sequence using prime numbers 2, 3, and 5 only, and then displaying an nth term (C++)
I've been brainstorming over this forever, and I just can't figure this out. I need to solve the following problem:
Generate the following sequence and display the nth term in the
sequence
2,3,4,5,6,8,9,10,12,15, etc..... Sequence only has Prime numbers
2,3,5
I need to use basic C++, such as while, for, if, etc. Nothing fancy. I can't use arrays simply because I don't know much about them yet, and I want to understand the code for the solution.
I'm not asking for a complete solution, but I am asking for guidance to get through this... please.
My problem is that I can't figure out how to check if the number if the number in the sequence is divisible by any other prime numbers other than 2, 3, and 5.
Also let's say I'm checking the number like this:
for(int i=2; i<n; i++){
if(i%2==0){
cout<<i<<", ";
}else if(i%3==0){
cout<<i<<", ";
}else if(i%5==0){
cout<<i<<", ";
}
It doesn't work simply due to the fact that it'll produce numbers such as 14, which can be divided by prime number 7. So I need to figure out how to ensure that that sequence is only divisible by 2, 3, and 5..... I've found lots of material online with solutions for the problem, but the solutions they have are far too advance, and I can't use them (also most of them are in other languages... not C++). I'm sure there's a simpler way.
The problem with your code is that you just check one of the prime factors, not all of them.
Take your example of 14. Your code only checks if 2,3 or 5 is a factor of 14, which is not exactly what you need. Indeed, you find that 2 is a factor of 14, but the other factor is 7, as you said. What you are missing is to further check if 7 has as only factors 2,3 and 5 (which is not the case). What you need to do is to eliminate all the factors 2,3 and 5 and see what is remaining.
Let's take two examples: 60 and 42
For 60
Start with factors 2
60 % 2 = 0, so now check 60 / 2 = 30.
30 % 2 = 0, so now check 30 / 2 = 15.
15 % 2 = 1, so no more factors of 2.
Go on with factors 3
15 % 3 = 0, so now check 15 / 3 = 5.
5 % 3 = 2, so no more factors of 3.
Finish with factors 5
5 % 5 = 0, so now check 5 / 5 = 1
1 % 5 = 1, so no more factors of 5.
We end up with 1, so this number is part of the sequence.
For 42
Again, start with factors 2
42 % 2 = 0, so now check 42 / 2 = 21.
21 % 2 = 1, so no more factors of 2.
Go on with factors 3
21 % 3 = 0, so now check 21 / 3 = 7.
7 % 3 = 1, so no more factors of 3.
Finish with factors 5
7 % 5 = 2, so no more factors of 5.
We end up with 7 (something different from 1), so this number is NOT part of the sequence.
So in your implementation, you should probably nest 3 while loops in your for loop to reflect this reasoning.
Store the next i value in temporary variable and then divide it by 2 as long as you can (eg. as long as i%2 == 0). Then divide by 3 as long as you can. Then by 5. And then check, what is left.
What about this?
bool try_hamming(int n)
{
while(n%2 == 0)
{
n = n/2;
}
while(n%3 == 0)
{
n = n/3;
}
while(n%5 == 0)
{
n = n/5;
}
return n==1;
}
This should return true when n is a hamming nummer and false other wise. So the main function could look something like this
#include<iostream>
using namespace std;
main()
{
for(int i=2;i<100;++i)
{
if(try_hamming(i) )
cout<< i <<",";
}
cout<<end;
}
this schould print out all Hamming numbers less then 100

Maple - converting 1D lists into 2D arrays

This is a question in Maple.
I understand in terms of java that I want a count and an increment but my logic doesn't convert as simply to maple code.
I have an very long list of numbers LIST (196) that I wish to turn into a 14x14 Array but using the convert(LIST,Array) only gives me a 1 dimensional array.
In Maple code, this will give me my first column.
j:=1;
for i from 1 to 14 do
B[i,j]:=Longlistvalue[i];
end do;
It's clear that my second column comes from t=2 and s from 15 to 24 but I'm struggling to put this into a loop.
Surely there is either a loop I can use for this or a maple command that puts the first 14 into the first row (or column) then the next 14 into the next row/column etc?
My most recent attempt gets me
B:=Array(1..14,1..14):
n:=1;
m:=14;
for j from 1 to 14 do
for i from n to m do
B[i,j]:=Longlistvalue[i];
end do;
n:=n+14;
m:=m+14;
end do;
But not it states that my array is out of range (because the s in B[i,j] must be less than 15).
Is there a way to get around this by means of a more efficient loop?
The Array (or Matrix) constructor can be used to do this directly, using an operator to assign the entries.
You can lay the list entries down into the Array either by column or by row. Adjust the example to fit your case where m=14 and n=14.
m,n := 3,4:
L:=[seq(i,i=1..m*n)]; # you got your list in another way
L := [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
Array(1..m,1..n,(i,j)->L[(j-1)*m+i]);
[1 4 7 10]
[ ]
[2 5 8 11]
[ ]
[3 6 9 12]
Array(1..m,1..n,(i,j)->L[(i-1)*n+j]);
[1 2 3 4]
[ ]
[5 6 7 8]
[ ]
[9 10 11 12]
You could also use nested loops.
Longlistvalue:=[seq(i,i=1..14^2)]: # your values will differ, of course
B:=Array(1..14,1..14):
n:=14;
m:=14;
for j from 1 to m do
for i from 1 to n do
B[i,j]:=Longlistvalue[(j-1)*m+i];
end do;
end do:
# so we can see the contents, displayed in full
interface(rtablesize=infinity):
B;