Musical representation of pi - c#

I'm creating a program to output the values of pi as sound. I've seen a few videos and some other representations, and want to try to make my own for fun, except I'm unsure how to proceed.
There are the notes A,B,C,D,E,F and G.
Unfortunately, there are 10 possible values for a digit, 1,2,3,4,5,6,7,8,9, or 0. I read something about a rule of fifths, but I am unsure how I would map the 10 numbers to sound. I'm not sure if this is the right place to ask this question.
How would you go about mapping a sound to numbers 0-9? I'd like to keep the spread as even as possible, so if every possible sound was on a line, 0-9 would be evenly distributed on the line, if that makes sense.
Currently, I'm trying Console.beep(note frequency, 1000) with these frequencies http://www.phy.mtu.edu/~suits/notefreqs.html, and ignoring values that aren't 1-7. Any thoughts?

You either need to assign two extra notes to the two spare numbers or represent the digits with something else, how about a rest or an accent for a note or some other effect?Or of course you could include semi tones, you would have to miss one out though as there are 11 in an octave, or 12 including the octave (up or down) of the first note.The nicest sounding thing would be assign each digit to a note on a scale like E minor pentatonic, or G major or a sweet bluesy one or something.
Example blues scale in C minor:
0 = C //This is the root note
1 = E flat 2 = F 3 = G
flat 4 = G 5 = B flat 6 = C //This note is one octave
higher than the root note 7 = E flat 8 = F 9 = G flat
G flat in this scale is the blues note.

What if you consider the digits to be degrees of a scale rather than absolute note mappings?
In the key of C, as an example:
c = 1 // tonic or root
d = 2
e = 3 // third (major in this case)
f = 4
g = 5 // perfect fifth
a = 6
b = 7
c = 8 //octave
d = 9
As for the handling of 0 you could treat this as a rest.

You can use 8 to go up an octave, 9 to go down an octave and 0 for rest (silence).
Just a suggestion.
This will give variety to your song.
Hop this helped you.

When all of the sharps and flats are included, the western musical scale has 12 semitones. To move from one semitone to the next, you multiply the frequency by the twelfth root of 2 (which is 1.05946).
It might be interesting to invent a 10 semitone scale, where the next semitone is found by multiplying by the tenth root of 2 (which is 1.0718). Then map the digits to these ten semitones, and off we go...

Related

I think I found another algorithm for number factorization [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
A few days ago i thought a new algorithm (maybe it is just new for me and it has already been developed ) of number factorization
My main idea is like that:
For example lets say we want to find factors of N = 148261;
so we need two numbers lets say ending with abc and def :
...abc * ...def = 148261 //(Equation.1)
(...+100*a+10*b+c) * (...+100*d+10*e+f) = 148261 //(Equation.2)
If we look this Equation2 with (mod 10) we find :
c*f = 1 mod(10)
Since c and f is digit (0<=..<=9) we do brute force on this range and find possible cases of (c,f) as :
(c,f) => (1,1) (3,7) (7,3) (9,9)
OK this was step 1 - we found last digits of the factors
Lets continue on step 2
For each possible cases of (c,f) lets try to find the previous digits
case (c,f) = (1,1):
if we look at the Equation2 with mod 100 and brute force b and e values we find that:
-- (b,e) => (1,5) (2,4)
...etc...
case (c,f) = (3,7):
....
If we continue this procedure recursively up to step 6 (since 148261 is 6 digits long)
we found possible factors:
173 * 857 = 148261
857 * 173 = 148261
It is recursive and its recursion degree is equals to the digits long of n which is log(10,n)
I have implemented this recursive algorithm with C#. It returns 1 * N and N * 1 if N is prime .
You can find source code (here) in my github repo.
It is long enough not to write full source here
I have written unit tests to see if this is working and seems all is right and my question is not about false positive or true negative responses of this algorithm because it works as described
Now it is interesting for me - my first question - is this algorithm has a known name or developed by someone else - i would like to know more about
And I have done some performance tests such that I can find all factors of n = int.MaxValue = 2147483647 in just a 0.2 second in my laptop I am quite impressed of the results
so i increased range and find this performance scale
2^20->00:00:00.0164829
2^25->00:00:00.0331482
2^30->00:00:00.3606766
2^35->00:00:00.7528593
2^40->00:00:08.8903395
2^45->00:02:13.2954906
As you can see there is much more increase of time after n=2^30 and it is not bcs of algorithm itself it is because i used uint64 and it hits its limits to calculate inner sums and products
after 2^30 i converted it to System.Numerics.BigInteger so this caused performance hits
and my second question is how to calculate O notation value of this algorithm Just want to compare this to existing algorithms
Then I looked up the inner values of digits and find interesting case : the possible cases of inner digits is increasing up to maxStep/2 then decreases for example n = int.MaxValue = 2147483647 the inner item counts become :
step-1 yield 5 items
step-2 yield 5 items
step-3 yield 40 items
step-4 yield 400 items
step-5 yield 4000 items
step-6 yield 21882 items
step-7 yield 6248 items
step-8 yield 1028 items
step-9 yield 112 items
step-10 yield 22 items
and my third question is how do we calculate (maybe a prebuild formula?) these inner counts for given n

Calculate the total amount (number) of combinations of a set with the specified combination length? [duplicate]

This question already has answers here:
Calculating all possible sub-sequences of a given length (C#)
(4 answers)
Closed 5 years ago.
Having a set of elements, which in this case is an Array of 3 characters/elements {A, B, C}:
char[] charSet = "ABC".ToCharArray();
I would like to write a generic usage function to help determine which would be the total amount of combinations that can be generated OF THE SPECIFIED LENGTH and determining too the amount of possible combinations with and without repetition. To avoid possible mistakes: this question is not about combo/perm generation, just calculation.
A simple uncompleted example to understand me:
public static long CalculateCombinations(int setLength, int comboLength, bool allowRepetition)
{
return result;
}
( where setLength is the amount of elements in the set, comboLength is the desired length of each combination, and allowRepetition a deterministic flag to help calculate the amount of combinations when and when not elements repetition is allowed in each combination. )
Then, if I have the same character set specified above, and I want to calculate the amount of possible combinations with repetition, the algorithm should return a value of 9, which would be the equivalent amount to this serie of combinations:
1: AA
2: AB
3: AC
4: BA
5: BB
6: BC
7: CA
8: CB
9: CC
The same algorithm should return me a value of 6 if I dont want repetition, which would be the equivalent amount to this serie of combinations:
1: AB
2: AC
3: BA
4: BC
5: CA
6: CB
Basically I'm trying to reproduce what this online service can do: http://textmechanic.com/text-tools/combination-permutation-tools/combination-generator/ however I tried to investigate and implement different 'nCr' formulas around the WWW (like http://www.vcskicks.com/code-snippet/combination.php ) and StackOverflow threads (like https://stackoverflow.com/a/26312275/1248295 ), but i don't get it how to calculate it when the combination length factor and repetition is involved in the calculation. Maybe this could be too basic than what it appears to me, but maths are not my forte.
My question: how can I write an algorithm that can calculate what I explained?. Would be very grateful if someone could link a formula and its implementation in C# or VB.NET.
Let's try it with three characters, A, B and C (n = 3) and combo length of k = 2, as your example states.
With repetition
We start with two empty spaces.
The first empty space can be filled in 3 possible ways.
For each of three possible ways, the second space can be filled in another three possible ways.
This gives you a total of 3 × 3 possibilities.
In general, there are n ^ k possibilities.
Without repetition
We start with two empty spaces.
The first empty space can be filled in 3 possible ways.
The second empty space can be filled in 2 possible ways, because you don't want to repeat yourself.
This gives you 3 × 2 possibilities in your case.
Let's go with another example. Say, you have five letters (ABCDE) and combo length of four _ _ _ _.
We put any of five letters on the first empty space. This is five possibilities: A, B, C, D, E.
Now for each possibility after the last step, no matter which letter we've chosen, now we have 4 letters left to choose from. If in the previous step we've chosen A, the corpus is now BCDE -- this is four possibilities. For B, we choose from ACDE -- this is again for possibilities. In total, since there were 5 ways to do previous step, and there are 4 ways to go after any of the previous choices, in total this is 20 possibilities: (AB, AC, AD, AE), (BA, BC, BD, BE), (CA, CB, CD, CE), (DA, DB, DC, DE), (EA, EB, EC, ED).
Let's keep going. After picking two letters, we're left with 3. With the same logic as before, for each of the previous 20 possibilities we have another 3 possibilities. This is 60 in total.
And one more space left. We have two letters which we haven't chosen before. From any of the previous 60 possibilities, we now have two possibilities. That's 120 in total.
So we've arrived at this by multiplying 5 × 4 × 3 × 2. Why start from 5? Because we initially had 5 letters: ABCDE. Why have four numbers in our multiplication? Because there were 4 empty spaces: _ _ _ _.
In general, you keep multiplying a decremented value starting from n, and do this k times: n × (n - 1) × ... × (n - k + 1).
The last value is (n - k + 1) because you are multiplying k values in total. From n to (n - k + 1) there are k values in total (inclusive).
We can test this with our n = 5 and k = 4 example. We said that the formula was 5 × 4 × 3 × 2. Now look at the general formula: indeed, we start from n = 5 and keep multiplying until we reach the number 5 - 4 + 1 = 2.
In your function's signature, n is setLength, k is comboLength. The implementation should be trivial with the above formulas, so I'm leaving this to the reader.
These are called permutations with and without repetition.

Teaching a ANN how to add

Preface: I'm currently learning about ANNs because I have ~18.5k images in ~83 classes. They will be used to train a ANN to recognize approximately equal images in realtime. I followed the image example in the book, but it doesn't work for me. So I'm going back to the beginning as I've likely missed something.
I took the Encog XOR example and extended it to teach it how to add numbers less than 100. So far, the results are mixed, even for exact input after training.
Inputs (normalized from 100): 0+0, 1+2, 3+4, 5+6, 7+8, 1+1, 2+2, 7.5+7.5, 7+7, 50+50, 20+20.
Outputs are the numbers added, then normalized to 100.
After training 100,000 times, some sample output from input data:
0+0=1E-18 (great!)
1+2=6.95
3+4=7.99 (so close!)
5+6=9.33
7+8=11.03
1+1=6.70
2+2=7.16
7.5+7.5=10.94
7+7=10.48
50+50=99.99 (woo!)
20+20=41.27 (close enough)
From cherry-picked unseen data:
2+4=7.75
6+8=10.65
4+6=9.02
4+8=9.91
25+75=99.99 (!!)
21+21=87.41 (?)
I've messed with layers, neuron numbers, and [Resilient|Back]Propagation, but I'm not entirely sure if it's getting better or worse. With the above data, the layers are 2, 6, 1.
I have no frame of reference for judging this. Is this normal? Do I have not enough input? Is my data not complete or random enough, or too weighted?
You are not the first one to ask this. It seems logical to teach an ANN to add. We teach them to function as logic gates, why not addition/multiplication operators. I can't answer this completely, because I have not researched it myself to see how well an ANN performs in this situation.
If you are just teaching addition or multiplication, you might have best results with a linear output and no hidden layer. For example, to learn to add, the two weights would need to be 1.0 and the bias weight would have to go to zero:
linear( (input1 * w1) + (input2 * w2) + bias) =
becomes
linear( (input1 * 1.0) + (input2 * 1.0) + (0.0) ) =
Training a sigmoid or tanh might be more problematic. The weights/bias and hidden layer would basically have to undo the sigmoid to truely get back to an addition like above.
I think part of the problem is that the neural network is recognizing patterns, not really learning math.
ANN can learn arbitrary function, including all arithmetics. For example, it was proved that addition of N numbers can be computed by polynomial-size network of depth 2. One way to teach NN arithmetics is to use binary representation (i.e. not normalized input from 100, but a set of input neurons each representing one binary digit, and same representation for output). This way you will be able to implement addition and other arithmetics. See this paper for further discussion and description of ANN topologies used in learning arithmetics.
PS. If you want to work with image recognition, its not good idea to start practicing with your original dataset. Try some well-studied dataset like MNIST, where it is known what results can be expected from correctly implemented algorithms. After mastering classical examples, you can move to work with your own data.
I am in the middle of a demo that makes the computer to learn how to multiply and I share my progress on this: as Jeff suggested I used the Linear approach and in particular ADALINE. At this moment my program "knows" how to multiply by 5. This is the output I am getting:
1 x 5 ~= 5.17716232607829
2 x 5 ~= 10.147218373698
3 x 5 ~= 15.1172744213176
4 x 5 ~= 20.0873304689373
5 x 5 ~= 25.057386516557
6 x 5 ~= 30.0274425641767
7 x 5 ~= 34.9974986117963
8 x 5 ~= 39.967554659416
9 x 5 ~= 44.9376107070357
10 x 5 ~= 49.9076667546553
Let me know if you are interested in this demo. I'd be happy to share.

Vertical Curve Formula

I'm making just a basic application that just writes pixels along a curve in C#.
I came across this website with a formula that looks promising. I believe this website is also talking about the same thing here.
What I don't really understand is how to implement it. I tried looking at the JavaScript code on the first link but I can't really tell what data I need to supply. The things involving the PVC, PVI, or PVT are the things I don't understand.
The example situation I'm going to set up is just both of the grades (vertical incline/decline) is just 5 and -5. Let's say point 1 is at 0, 0 and point 2 is 100, 100.
Can someone explain some of the obscure variables in the formula and how would I use the formula to draw the curve?
Generally, to draw a curve in 2D you vary one parameter, and then collect x,y point pairs, and plot the pairs. In your case it will work to just vary the horizontal distance (x), and then collect the corresponding y-values, and then you can plot these.
As for the formula, it is very unclear. Basically it's just a parabola with a bunch of (poorly defined) jargon around it. To graph this, you want to vary x from 0 to L (this isn't obvious, btw, I had to work out the math, i.e., how to vary x so that the slopes would be as they suggest in the figure, anyway, it's 0 to L, and they should have said so).
I don't have C# running now, but hopefully you can translate this Python code:
from matplotlib.pyplot import plot, show
from numpy import arange
G1 = .1 # an initial slope (grade) of 10% (note that one never uses percentages directly in calculations, it's always %/100)
G2 = -.02 # a final slope (grade) of 2%
c = 0 # elevation (value of curve when x=0, that is, y at PVC
L = 10. # the length of the curve in whatever unit you want to use (ft, m, mi, etc), but this basically sets your unit system
N = 1000 # I'm going to calculate and plot 100 points to illustrate this curve
x = arange(0, L, float(L)/N) # an array of N x values from 0 to (almost) L
# calculate the curve
a = (G2-G1)/(2*L)
b = G1
y = a*x*x + b*x + c # this is shorthand for a loop y[0]=a*x[0]*x[0] + b*...
plot(x, y)
show()
print (y[1]-y[0])/(x[1]-x[0]), (y[-1]-y[-2])/(x[-1]-x[-2])
The final line prints the initial and final slopes as a check (in Python neg indexing counts from the back of the array), and this match what I specified for G1 and G2. The plot looks like:
As for your requests: "The example situation I'm going to set up is just both of the grades (vertical incline/decline) is just 5 and -5. Let's say point 1 is at 0, 0 and point 2 is 100, 100.", in a parabola you basically get three free parameters (corresponding to a, b, and c), and here, I think, you over-specified it.
What are PVC, PVT, and PVI? PVC: the starting point, so Y_PVC is the height of the starting point. PVT: the ending point. PVI: if you draw a line from PVC at the initial slope G1 (ie the tangent to the curve on the left), and similarly from PVT, the point where they intersect is called PVI (though why someone would ever care about this point is beyond me).

Struggling to make algorithm to generate board for a puzzle game

I'm looking to make a number puzzle game. For the sake of the question, let's say the board is a grid consisting of 4 x 4 squares. (In the actual puzzle game, this number will be 1..15)
A number may only occur once in each column and once in each row, a little like Sudoku, but without "squares".
Valid:
[1, 2, 3, 4
2, 3, 4, 1
3, 4, 1, 2
4, 1, 2, 3]
I can't seem to come up with an algorithm that will consistently generate valid, random n x n boards.
I'm writing this in C#.
Start by reading my series on graph colouring algorithms:
http://blogs.msdn.com/b/ericlippert/archive/tags/graph+colouring/
It is going to seem like this has nothing to do with your problem, but by the time you're done, you'll see that it has everything to do with your problem.
OK, now that you've read that, you know that you can use a graph colouring algorithm to describe a Sudoku-like puzzle and then solve a specific instance of the puzzle. But clearly you can use the same algorithm to generate puzzles.
Start by defining your graph regions that are fully connected.
Then modify the algorithm so that it tries to find two solutions.
Now create a blank graph and set one of the regions at random to a random colour. Try to solve the graph. Were there two solutions? Then add another random colour. Try it again. Were there no solutions? Then back up a step and add a different random colour.
Keep doing that -- adding random colours, backtracking when you get no solutions, and continuing until you get a puzzle that has a unique solution. And you're done; you've got a random puzzle generator.
It seems you could use this valid example as input to an algorithm that randomly swapped two rows a random number of times, then swapped two random columns a random number of times.
There aren't too many combinations you need to try. You can always rearrange a valid board so the top row is 1,2,3,4 (by remapping the symbols), and the left column is 1,2,3,4 (by rearranging rows 2 thru 4). On each row there are only 6 permutations of the remaining 3 symbols, so you can loop over those to find which of the 216 possible boards are valid. You may as well store the valid ones.
Then pick a valid board randomly, randomly rearrange the rows, and randomly reassign the symbols.
I don't speak C#, but the following algorithm ought to be easily translated.
Associate a set consisting of the numbers 1..N with each row and column:
for i = 1 to N
row_set[i] = column_set[i] = Set(1 .. N)
Then make a single pass through the matrix, choosing an entry for each position randomly from the set elements valid at that row and column. Remove the number chosen from the respective row and column sets.
for r = 1 to N
for c = 1 to N
k = RandomChoice( Intersection( column_set[c], row_set[r] ))
puzzle_board[r, c] = k
column_set[c] = column_set[c] - k
row_set[r] = row_set[r] - k
next c
next r
Looks like you want to generate uniformly distributed Latin Squares.
This pdf has a description of a method by Jacobson and Matthews (which was published elsewhere, a reference of which can be found here: http://designtheory.org/library/encyc/latinsq/z/)
Or you could potentially pre-generate a "lot" of them (before you ship :-)), store that in a file and randomly pick one.
Hope that helps.
The easiest way I can think of would be to create a partial game and solve it. If it's not solvable, or if it's wrong, make another. ;-)
Sudoku without squares sounds a bit like Sudoku. :)
http://www.codeproject.com/KB/game/sudoku.aspx
There is an explanation of the board generator code they use there.
Check out http://www.chiark.greenend.org.uk/~sgtatham/puzzles/ - he's got several puzzles that have precisely this constraint (among others).
A further solution would be this. Suppose you have a number of solutions. For each of them, you can generate a new solution by simply permuting the identifiers (1..15). These new solutions are of course logically the same, but to a player they will appear different.
The permutation might be done by treating each identifier in the initial solution as an index into an array, and then shuffling that array.
Use your first valid example:
1 2 3 4
2 3 4 1
3 4 1 2
4 1 2 3
Then, create randomly 2 permutations of {1, 2, 3, 4}.
Use the first to permute rows and then the second to permute columns.
You can find several ways to create permutations in Knuth's The Art of Computer Programming (TAOCP), Volume 4 Fascicle 2, Generating All Tuples and Permutations (2005), v+128pp. ISBN 0-201-85393-0.
If you can't find a copy in a library, a preprint (of the part that discusses permutations) is available at his site: fasc2b.ps.gz
EDIT - CORRECTION
The above solution is similar to 500-Intenral Server Error's one. But I think both won't find all valid arrangements.
For example they'll find:
1 3 2 4
3 1 4 2
2 4 1 3
4 2 3 1
but not this one:
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
One more step is needed: After rearranging rows and columns (either using my or 500's way), create one more permutation (lets call it s3) and use it to permute all the numbers in the array.
s3 = randomPermutation(1 ... n)
for i=1 to n
for j=1 to n
array[i,j] = s3( array[i,j] )

Categories

Resources