mathematic of computers - numbers

I can not figure out the sequence of numbers between hexadecimal
288 and 2AO i really need help.

288 + 1 = 289
289 + 1 = 28A
...
28F + 1 = 290
290 + 1 = 291
...
29F + 1 = 2A0
You might want to know that even Windows calc.exe provides a HEX mode and that Google itself can do it :)

Read this for info on base-16 numeral system
Decimal:
$ seq 0x288 0x2A0
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
Hex:
# printf "%x\n" `seq 0x288 0x2A0`
288
289
28a
28b
28c
28d
28e
28f
290
291
292
293
294
295
296
297
298
299
29a
29b
29c
29d
29e
29f
2a0

Let's start with something simpler. What's the sequence of numbers between 32 and 45, in the 10 base, as you're used to?
After 32, there's 33, 34, 35... 39. And then, since the digits in base-10 are between 0 and 9, you advance to 40. The rightmost digit wraps back to 0, and the digit on its left becomes one bigger, thus giving you 40. From there you continue - 41,42,43,44,45.
Now, in other bases, its simply a matter of a different amount of digits. Let's take the same question (32->45), but in base 6. Base 6 has six digits - 0,1,2,3,4,5. So you go from 32 to 33, 34, 35, and here, just like you jumped from 39 to 40, you stop. There is no 36 in base 6 - you go from 5 to 0, and then you increment the left digit - hence 40. From there it's 41,42,43,44,45.
Now, with bases which are less than 10 (like base 6 above), it's easy - there are less digits. But what about base 11? base 64? or in your case, base 16? How would you represent the eleventh digit?
Here, the convention is simple. The digits turn into letters. These are the digits for base 16, the hexadecimal base:
0 1 2 3 4 5 6 7 8 9 A B C D E F
So the eleventh digit is A. The sixteenth digit is F. Let's go back to my first example but do it in the hexadecimal base. You start with 32. Go to 33, 34... 39, and then you proceed within the 30s with 3A, 3B, 3C, 3D, 3E, 3F, and here you wrap back to 0 - and jump to 40. Here is the complete sequence:
32,33,34,35,36,37,38,39,3A,3B,3C,3D,3E,3F,40,41,42,43,44,45
From here you should be able to solve 288-2A0 by yourself.
Good luck!

This C program will output the values:
#include <stdio.h>
int main() {
int i;
for(i=0x288; i<=0x2A0; i++)
printf("%X ", i);
printf("\n");
return 0;
}
Output: 288 289 28A 28B 28C 28D 28E 28F 290 291 292 293 294 295 296 297 298 299 29A 29B 29C 29D 29E 29F 2A0
Is this what you want?

Related

Data type changes when saving a binary image

I converted a grayscale image to a binary image as shown in the script below:
D = '/folder-path/';
S = dir(fullfile(D,'*.jpg'));
for k = 1:numel(S)
F = fullfile(D,S(k).name);
I = imread(F);
I2 = im2bw(I);
imwrite(I2,F);
end
The issue is when I try to read any of the images that were converted to binary and saved to the hard drive, the returned type is uint8!
I thought the image would contain two values like 0 and 255 for instance at least, but when running unique(I) on one image I got the following:
75×1 uint8 column vector
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
35
36
37
38
217
218
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
Why do you think this is happening? How can I read the saved images as binary and not uint8?
Thanks.
Do not write your binary image to a jpeg file, it is compressed and you certainly loose the exact values in the process.
In addition, erasing the source file really looks like a bad practice.
A solution would be to save your binary image in a png file with the same name. For instance:
imwrite(I2, [D s(k).name(1:end-3) 'png']);
In this case the png contains only zeros and ones. To be able to see your binary image in a viewer, better to have 0s and 255s:
imwrite(I2*255, [D s(k).name(1:end-3) 'png']);

Triangle Sum through down with NON-PRIME numbers with Swift 4

I know this question is asked before for different programming languages, and I tried to implement this with Swift 4 but once I submit my answer, I've been told that my answer was wrong so here is the task;
You will have a TRIANGLE input from a file and you need to find the maximum sum of the numbers according to given rules below;
You will start from the top and move downwards to an adjacent number as in below.
You are only allowed to walk downwards and diagonally.
You can only walk over NON PRIME NUMBERS.
According to above rules the maximum sum of the numbers from top to bottom in below example is 24.
var sampleString = """
1
8 4
2 6 9
8 5 9 3
As you can see this has several paths that fits the rule of NOT PRIME NUMBERS; 1>8>6>9, 1>4>6>9, 1>4>9>9
1 + 8 + 6 + 9 = 24. As you see 1, 8, 6, 9 are all NOT PRIME NUMBERS and walking over these yields the maximum sum.
assignment string:
var assignmentString = """
215
193 124
117 237 442
218 935 347 235
320 804 522 417 345
229 601 723 835 133 124
248 202 277 433 207 263 257
359 464 504 528 516 716 871 182
461 441 426 656 863 560 380 171 923
381 348 573 533 447 632 387 176 975 449
223 711 445 645 245 543 931 532 937 541 444
330 131 333 928 377 733 017 778 839 168 197 197
131 171 522 137 217 224 291 413 528 520 227 229 928
223 626 034 683 839 53 627 310 713 999 629 817 410 121
924 622 911 233 325 139 721 218 253 223 107 233 230 124 233"""
my code:
func maxSumForTriangle(triangleString: String) {
var temporaryIndex = 0
var earlierIndex = 0
var greatSum = 0
var temporaryMaxInLine = 0
let values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
print(values)
print(values.count)
for line in values {
if line.count == 1 {
greatSum += line[0]
earlierIndex = line.count - 1
} else {
for number in line.enumerated() {
if number.offset == earlierIndex || number.offset == earlierIndex + 1 {
//Check the number if its prime or not with the isPrime function we defined
if !isPrime(number.element) {
if number.element > temporaryMaxInLine {
temporaryMaxInLine = number.element
temporaryIndex = number.offset
}
}
}
}
earlierIndex = temporaryIndex
greatSum += temporaryMaxInLine
temporaryMaxInLine = 0
}
}
print(greatSum)
}
Which results in 7619 but then I realized where my problem is; I don't check for every possible path, I just check for the highest non prime number at each line and continue summing with it.
So I need to find a different approach for this problem so that my function can check every possible scenarios and return with the highest sum
I could not figure it out yet, should I implement a different function where it calls itself again so that it can check for all the possible paths?
Sorry for long question but I also wanted to show my old implementation.
This is a typical problem which can be solved with “dynamic programming”: The idea
is to compute the maximal possible sum for every starting
point in the pyramid.
And that becomes simple if we start at the bottom row and work upwards:
We only have to add to each entry the larger of its two lower neighbors.
At the end, the top entry is the desired maximal sum.
Taking the additional condition about non-primes into account, this
can be implemented as
var values = triangleString.components(separatedBy: .newlines).map {
$0.components(separatedBy: .whitespaces).compactMap(Int.init)
}
for row in values.indices.reversed() {
for col in values[row].indices {
if isPrime(values[row][col]) {
values[row][col] = Int.min
} else if row + 1 < values.endIndex {
values[row][col] += max(values[row+1][col], values[row+1][col+1])
}
}
}
print(values[0][0])

error reading a text file in octave

I have a text file named xMat.txt which has 200 space separated elements in one line and some 767 lines.
This is how xMat.txt looks.
386.0 386.0 388.0 394.0 402.0 413.0 ... .0 800.0 799.0 796
801.0 799.0 799.0 802.0 802.0 80 ... 399.0 397.0 394.0 391
.
.
.
When I try to read the file in octave using X = dlmread('xMat.txt',' ') I get a matrix of size 767 X 610. I am expecting a matrix of size 767 X 200 since there are 200 elements in one row. How can I solve this problem?
Edit - This is my file
Your uploaded file https://bpaste.net/raw/96cf21aa21b8 has incosistent number of columns per row.
$ awk "{print NF}" tmp | sort | uniq -c
2 200
754 201
1 206
1 217
1 223
1 234
1 237
1 238
1 269
1 273
1 390
1 420
1 610
So the most rows have 201 columns but one has 420 columns and one even has 610 columns. This is the reason you get a 767x610 matrix from dlmread.
Lets look which lines have more than 201 columns:
$ awk "{if (NF>201) print NR, NF}" tmp
68 217
580 206
613 390
615 234
657 273
676 610
679 237
720 269
722 238
743 223
762 420
The first coloumn shows the line number, the second number of columns.
So your line with 610 columns is line number 676. I aslo printed line 676:
so you see it really contains data, no multiple spaces which are filles with zeros.

Problems with matrix indexing using for loop and if condition

I to do some indexing, something like what follows:
for c=1:size(params,1)
for d=1:size(data,2)
if params(c,8)==1
value1(c,d)=data(params(c,11),d);
elseif params(c,8)==2
value2(c,d)=data(params(c,11),d);
elseif params(c,8)==3
value3(c,d)=data(params(c,11),d);
end
end
end
The problems with this is that if we have params(:,8)=1,3,1,3,2,3,1... then value1 will contain all zeros in rows 2, 4, 5, 6, etc. These are the rows that do not have 1 in column 8 in params. Similarly, value2 will contains all zeros in rows 1, 2, 3, 4, 6, 7... and value3 will contain all zeros in row 1, 3, 5, 7, .... Could anyone tell me how to index so I don't have 'gaps' of zeros in between rows? Thanks!
Edit; below is a sample dataset:
data (1080x15 double)
168 432 45 86
170 437 54 82
163 423 52 83
178 434 50 84
177 444 42 87
177 444 58 85
175 447 48 77
184 451 59 86
168 455 52 104
174 437 62 88
175 443 55 85
179 456 51 92
168 450 73 82
175 454 60 68
params (72x12 double - we are interested in only column 8 and 11 ) so I'm showing only column 8-11 for the sake of space:
1 10 15 1
3 12 16 16
2 10 15 32
3 12 16 47
1 8 14 63
2 10 15 77
2 8 14 92
3 10 15 106
1 12 16 121
3 8 14 137
2 10 15 151
The expected output for value1, value2, and value3 should be 24x15. This is because there are 15 columns in data and value 1, 2, 3 occur 24 times each in column 8 in params.
You can use bsxfun to avoid for-loop (note that it is actually not vertorizing):
value1 = bsxfun(#times,data(params(:,11),:),(params(:,8)==1));
value2 = bsxfun(#times,data(params(:,11),:),(params(:,8)==2));
value3 = bsxfun(#times,data(params(:,11),:),(params(:,8)==3));
But it still gives you the results with zero rows. So you can remove zero-rows by:
value1(all(value1==0,2),:)=[];
value2(all(value2==0,2),:)=[];
value3(all(value3==0,2),:)=[];
You can also use above commands to remove zero-rows in your results without using bsxfun. It is not always good to loose the transparency.

MATLAB accessing conditional values and performing operation in single column

Just started MATLAB 2 days ago and I can't figure out a non-loop method (since I read they were slow/inefficient and MATLAB has better alternatives) to perform a simple task.
I have a matrix of 5 columns and 270 rows. What I want to do is:
if the value of an element in column 5 of matrix goodM is below 90, I want to take that element and and subtract it from 90.
So far I tried:
test = goodM(:,5) <= 90;
goodM(test) = 999;
It changes all goodM values within column 1 not 5 into 999, in addition this method doesn't allow me to perform operations on the elements below 90 in column 5. Any elegant solution to doing this?
edit:: goodM(:,5)(test) = 999; doesn't seem to work either so I have no idea to specify the target column.
I am assuming you are looking to operate on elements that have values below 90 as your text in the question reads, rather than 'below or equal to' as represented by '<=' as used in your code. So try this -
ind = find(goodM(:,5) < 90) %// Find indices in column 5 that have values less than 90
goodM(ind,5) = 90 - goodM(ind,5) %// Operate on those elements using indices obtained from previous step
Try this code:
b=90-a(a(:,5)<90,5);
For example:
a =
265 104 479 13 176
26 110 447 208 144
379 163 179 366 464
301 48 274 391 26
429 374 174 184 297
495 375 312 373 82
465 272 399 447 420
205 170 373 122 84
1 417 63 65 252
271 277 412 113 500
then,
b=90-a(a(:,5)<90,5);
b =
64
8
6