How do I display all perfect squares below a given number on scratch? - mit-scratch

I was able to list all the prime numbers up to an inputted number, however as I am inexperienced at coding on Scratch, I have had difficulty constructing a list of all the perfect squares below a number.
For example, if you input 17 the output should be 16,9,4.

The main way to detect if a number is a perfect square is to take the square root and round it and then multiply it by itself. If it is a perfect square the answer will be the number you started with.
Let's try with 4: The square root of 4 is 2 and when you round it you get 2 and when you multiply 2 times 2 you do get 4 so 4 is a perfect number.
Let's try with 5: The square root of 5 is 2.23606797749979 and when you round it you get 2 and when you multiply 2 times 2 you do NOT get 5.
There are two ways to write the loop: you can count up or you can count down. Either way, for each number you do the test and if the number is a perfect square you display it.
counting down:
counting up:

Related

Swift MPSCNNConvolution -- weights all set to 1, shouldn't the output look just like the input?

Trying to figure out how to use MPSCNNConvolution. I have a 4 x 3 image, and a 4 x 3 kernel. I'm setting all the weights to 1, and all the inputs to 1, and I sort of expected to get all 1's back. What I get instead is
12 9 6 3
8 6 4 2
4 3 2 1
The problem is that I don't know whether it's supposed to behave like this or not. I've been all over every shred of Apple doc I can find, every online article, every github repo, and I can't find anything that says what kind of output to expect when the layer is set up correctly.
The pattern holds for differently sized images. A 3 x 2 gives me
6 4 2
3 2 1
And a 2 x 2 gives me
4 2
2 1
I've pushed my "minimal" example to github. It's not small. Xcode 12.4 no longer supports Float16, so there's utility code for floating between Float16 and Float32, plus all the convoluted setup for convolution, and yet more code for trying to un-headache un-safe pointers.
My specific questions: is this output "just the normal behavior" for MPSCNNConvolution? Is there a name for this function/algorithm, something I can look up?
The documentation for MPSCNNConvolution is slightly confusing. To the uninitiated, it might seem that MPSCNNConvolution is a kind of container that holds convolution kernels. This is not the case. MPSCNNConvolution is itself a kernel. Specifically, it weights and sums all the input values under the kernel window. Just a straight sum, no averaging or maxing. What you're seeing is the result of the kernel starting at (0, 0) and sliding way off the right edge, and eventually way off the bottom edge.
Set your kernel offset and your clip rectangle on the input image, and MPSCNNConvolution will work the same way as MPSCNNPooling* kernels and all the others

Generate prime numbers recursively, Matlab

I am trying to generate prime numbers recursively using the previous primes. 2 is a prime by default and each time I keep adding 1 to the next number and divide by the previous primes, eg 2+1=3 so i check that 2 does not divide 3 so 3 is prime so i store 2 and 3 so far, next would be 3+1=4, i would use my current prime list 2 and 3, and see that 2 divides 4 so it does not go into the list then i continue with 4+1 and so forth all the way up to n. The key thing is dividing by the previous primes each time and if you reach a prime the list is updated. Please check my program to see what i am doing wrong.
this is my program so far but I am just getting 3 and 962, i want it update the list and refer back to it each time for the loop so i can use mod(2+numba,primlist) each time:
n=960;
primlist=[];
for numba=2:n
if mod(2+1,2)~=0
primlist=2+1;
end
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
primlist
You are initializing your primlist again and again. Do not do that. I am making as less modifications to your code to make it run correctly. The logic is essentially correct. You just need to initialize primlist outside.
n=960;
primlist=2;
for numba=1:n %Changed 2 to 1
if mod(2+numba,primlist)~=0
primlist=[primlist;2+numba];
end
end
primlist

Generate matrix of random number with constraints in matlab

I want to generate a matrix of random numbers (normrnd with mean == 0) that satisfy the following constraints using MATLAB (or any other language)
The sum of the absolute values in the matrix must equal X
The largest abs(single number) must equal Y
The difference between the number and its 8 neighbors (3 if in corner, 5 if on edge) must be less than Z
It would be relatively easy to satisfy one of the constraints, but I can't think of an algorithm that satisfies all of them...
Any ideas?
I am not sure whether to edit my post or to reply here, so I am editing... #MZimmerman6, you have a point. Though these constraints won't produce a unique solution, how would I obtain multiple solutions without using rand?
A very simply 3 x 3 where 5 is the max element value, 30 is the sum, and 2 is the difference
5 4 3
4 4 2
3 2 3
Rody, that actually may help...I need to think more :)
Luis ...Hmmm...why not? I can add up the absolute value of a normally distributed sample...right?
Here is an algorithm to get the 'random' numbers that you need.
Generate a valid number (for example in the middle)
Determine the feasible range for one of the numbers next to it
If there is no range, you go to step 1, otherwise generate a number and continue
Depending on your constraints it may take a while of course. You could add an other step to see if changing the existing numbers would help before going back to step 1.

matlab percentage change between cells

I'm a newbie to Matlab and just stumped how to do a simple task that can be easily performed in excel. I'm simply trying to get the percent change between cells in a matrix. I would like to create a for loop for this task. The data is setup in the following format:
DAY1 DAY2 DAY3...DAY 100
SUBJECT RESULTS
I could only perform getting the percent change between two data points. How would I conduct it if across multiple days and multiple subjects? And please provide explanation
Thanks a bunch
FOR EXAMPLE, FOR DAY 1 SUBJECT1(RESULT=1), SUBJECT2(RESULT=4), SUBJECT3(RESULT=5), DAY 2 SUBJECT1(RESULT=2), SUBJECT2(RESULT=8), SUBJECT3(RESULT=10), DAY 3 SUBJECT1(RESULT=1), SUBJECT2(RESULT=4), SUBJECT3(RESULT=5).
I WANT THE PERCENT CHANGE SO OUTPUT WILL BE DAY 2 SUBJECT1(RESULT=100%), SUBJECT2(RESULT=100%), SUBJECT3(RESULT=100%). DAY3 SUBJECT1(RESULT=50%), SUBJECT2(RESULT=50%), SUBJECT3(RESULT=50%)
updated:
Hi thanks for responding guys. sorry for the confusion. zebediah49 is pretty close to what I'm looking for. My data is for example a 10 x 10 double. I merely wanted to get the percentage change from column to column. For example, if I want the percentage change from rows 1 through 10 on all columns (from columns 2:10). I would like the code to function for any matrix dimension (e.g., 1000 x 1000 double) zebediah49 could you explain the code you posted? thanks
updated2:
zebediah49,
(data(1:end,100)- data(1:end,99))./data(1:end,99)
output=[data(:,2:end)-data(:,1:end-1)]./data(:,1:end-1)*100;
Observing the code above, How would I go about modifying it so that column 100 is used as the index against all of the other columns(1-99)? If I change the code to the following:
(data(1:end,100)- data(1:end,:))./data(1:end,:)
matlab is unable because of exceeding matrix dimensions. How would I go about implementing that?
UPDATE 3
zebediah49,
Worked perfectly!!! Originally I created a new variable for the index and repmat the index to match the matrices which was not a good idea. It took forever to replicate when dealing with large numbers.
Thanks for you contribution once again.
Thanks Chris for your contribution too!!! I was looking more on how to address and manipulate arrays within a matrix.
It's matlab; you don't actually want a loop.
output=input(2:end,:)./input(1:end-1,:)*100;
will probably do roughly what you want. Since you didn't give anything about your matlab structure, you may have to change index order, etc. in order to make it work.
If it's not obvious, that line defines output as a matrix consisting of the input matrix, divided by the input matrix shifted right by one element. The ./ operator is important, because it means that you will divide each element by its corresponding one, as opposed to doing matrix division.
EDIT: further explanation was requested:
I assumed you wanted % change of the form 1->1->2->3->1 to be 100%, 200%, 150%, 33%.
The other form can be obtained by subtracting 100%.
input(2:end,:) will grab a sub-matrix, where the first row is cut off. (I put the time along the first dimension... if you want it the other way it would be input(:,2:end).
Matlab is 1-indexed, and lets you use the special value end to refer to the las element.
Thus, end-1 is the second-last.
The point here is that element (i) of this matrix is element (i+1) of the original.
input(1:end-1,:), like the above, will also grab a sub-matrix, except that that it's missing the last column.
I then divide element (i) by element (i+1). Because of how I picked out the sub-matrices, they now line up.
As a semi-graphical demonstration, using my above numbers:
input: [1 1 2 3 1]
input(2,end): [1 2 3 1]
input(1,end-1): [1 1 2 3]
When I do the division, it's first/first, second/second, etc.
input(2:end,:)./input(1:end-1,:):
[1 2 3 1 ]
./ [1 1 2 3 ]
---------------------
== [1.0 2.0 1.5 0.3]
The extra index set to (:) means that it will do that procedure across all of the other dimension.
EDIT2: Revised question: How do I exclude a row, and keep it as an index.
You say you tried something to the effect of (data(1:end,100)- data(1:end,:))./data(1:end,:). Matlab will not like this, because the element-by-element operators need them to be the same size. If you wanted it to only work on the 100th column, setting the second index to be 100 instead of : would do that.
I would, instead, suggest setting the first to be the index, and the rest to be data.
Thus, the data is processed by cutting off the first:
output=[data(2:end,2:end)-data(2:end,1:end-1)]./data(2:end,1:end-1)*100;
OR, (if you neglect the start, matlab assumes 1; neglect the end and it assumes end, making (:) shorthand for (1:end).
output=[data(2:,2:end)-data(2:,1:end-1)]./data(2:,1:end-1)*100;
However, you will probably still want the indices back, in which case you will need to append that subarray back:
output=[data(1,1:end-1) data(2:,2:end)-data(2:,1:end-1)]./data(2:,1:end-1)*100];
This is probably not how you should be doing it though-- keep data in one matrix, and time or whatever else in a separate array. That makes it much easier to do stuff like this to data, without having to worry about excluding time. It's especially nice when graphing.
Oh, and one more thing:
(data(:,2:end)-data(:,1:end-1))./data(:,1:end-1)*100;
is identically equivalent to
data(:,2:end)./data(:,1:end-1)*100-100;
Assuming zebediah49 guessed right in the comment above and you want
1 4 5
2 8 10
1 4 5
to turn into
1 1 1
-.5 -.5 -.5
then try this:
data = [1,4,5; 2,8,10; 1,4,5];
changes_absolute = diff(data);
changes_absolute./data(1:end-1,:)
ans =
1.0000 1.0000 1.0000
-0.5000 -0.5000 -0.5000
You don't need the intermediate variable, you can directly write diff(data)./data(1:end,:). I just thought the above might be easier to read. Getting from that result to percentage numbers is left as an exercise to the reader. :-)
Oh, and if you really want 50%, not -50%, just use abs around the final line.

how to find middle point of a sorted array in matlab?

i have an arry lets say
A=[2 3 4 5 6 7 8 9]
i want to get middle point
like B=[5]
how to do it?
Try to use end to automatically obtain the index of the last entry, and use ceil to round up the half length when the length is not even
B=A(ceil(end/2))
MATLAB's built-in median function will work. If you have an array with an odd number of elements it pulls the middle point. Otherwise if you have an even number of points, it averages the two points in the middle.