I try to use this countdown. But I need to reverse it when reaches zero, e.g. when it reaches zero, it should count-up.
Is this possible?
Thanks.
you have not specified a language, so here is some java.
the algorithm still applies.
// the modifier to apply
int modifier = 1;
// min and max values
int maxValue = 10;
int minValue = -10;
// the value of the count
int count = 0;
// loop forever
while ( true ) {
// it higher then max or below min
if ( count >= maxValue || count <= minValue ) {
// invert the modifier
modifier = ( modifier * -1 );
}
// add modifier to count
count += modifier;
// use it
doSomethingWith( count );
}
This will,
start at 0
count up to 10
count down to -10
count up to 0
repeat
Related
I'm looking for a tip for a AS3 script, have no idea how to start there
Button, if clicked the function is executed, which outputs a predefined value as the cross sum of a number string.
Example:
Cross sum should be 10
By clicking on the button, the function generates the number 55 or 82 or 37 or 523, ie numbers with the cross sum 10
An alternative way using % (modulo) instead of a string. You could write that into one line like this:
while (sum != 0) { qsum += sum % 10; sum /= 10; }
The trick is that modulo will give us only the last digit of the longer number, then we divide by 10 to trim off that last number (from longer) and we re-read a newer ending digit of the long number.
Example:
Long num = 1234, so each trim gives, 4 then 3 then 2 then 1 and we'll sum them up each time.
usage:
myInt = cross_sum(50); //makes myInt hold answer result of function (where ExpectedValue input is 50).
and the supporting function...
function cross_sum( ExpectedValue: int ) : int
{
var rand :int = Math.floor(Math.random() * 100000000000)
var sum :int = Math.abs( rand );
var qsum :int = 0;
while (sum != 0)
{
qsum += sum % 10; //get last digit of sum...
sum /= 10; //trim down sum by 1 digit...
}
if ( qsum == ExpectedValue ) { return rand; } //# stop here and give back "rand" as answer result.
else { cross_sum( expectedValue ); } //# else if wrong, try again...
}
Got it now.....
the function calculates a number, with the crosssum 50
function berechnen() {
var rand = Math.floor(Math.random() * 100000000000)
var sum = String(rand)
var qsum = 0;
for (var i = 0; i < sum.length; i++) {
qsum += Number(sum.charAt(i));
}
if (qsum == 50) {
summe.text = String(sum);
} else {
berechnen()
}
}
I have a question, more on the theoretical side. I want to make a recursive function that counts all (not only prime) different divisors of a given natural number.
For example with f(0)=0 (per Def.), f(3) = 2, f(6) = 4, f(16) = 5 etc.
Theoretically, how could I do that?
Thanks.
If I understand correctly, you only want to COUNT them, not to collect them, right?
A second assumption is that you don't want to count only independent divisors (i.e. you want to count "2", "3" but not "6").
If this is the case, the algorithm shown in Sean's answer can be simplified significantly:
You don't need the array divisorList but only a counter,
You as soon as you find a divisor, you can reduce the max limit of the loop by the result of dividing the root number by the divisor (e.g. if your root number is 900 and 2 is the first divisor, you can set the limit of the loop to 450; then, when checking 3 you will reduce the limit to 150 and so on).
EDIT:
After thinking a little bit more, here is the correct algorithm:
Assume that the number is "N". Then, you already start with a count of 2 (i.e. 1 and N),
You then check if N divides by 2; if it does, you need to add 2 to the count (i.e. 2 and N/2),
You then change the limit of the loop to N/2,
Test if dividing by 3 yields an integer; if it does, you add 2 to the count (i.e. 3 and N/3) and reduce the limit to N/3,
Test 4...
Test 5...
...
In Pseudo-code:
var Limit = N ;
Count = 2 ;
for (I = 2 ; I < Limit ; I++) {
if (N/I is integer) {
Count = Count + 3 ;
Limit = N / I ;
} ;
} ;
Note: I don't know which language you are programming, so you need to verify if your language allows you to change the limit of the loop. If it does not, you can include an EXIT-LOOP condition (e.g. if I >= Limit then exit loop).
Hope this resolves your problem.
public static ArrayList<int> recursiveDivisors(int num)
{
ArrayList<int> divisorList = new ArrayList<int>();
for (int i = 1; i <= num; i++)
{
if (num % i == 0)
divisorList.add(i)
}
return divisorList;
}
Something like this?
Returns all divisors in a divisor array list
EDIT: Not recursive
if I record a series of frequencies beeps into a buffer, for example:
15kHz for 50ms, 17k for 50 ms and goes on, is there any way to "go" along the time plain and to decode this freqs(with goertzel or something)?
Hey, this is an update, I've added a code that shows how I find the first delimiter in the sound buffer that I check. If I record 5 seconds of a buffer(I record into a stream buffer and not a file) The first snippet takes something like 30 seconds to analyze the index where the start delimiter starts at. I thinks it is very newbie...must find a better solution. thanks
(every delimiter is 0.2 seconds duration) and it's like that - Start delimiter = 12KHz, 1's = 13k, 0's = 14k, End delimiter = 15k
double max_power = 0;
int max_power_index = 0;
double DelimiterSamplesCount = SampleRate * DelimiterTime;
float[] samples32array = samples32.ToArray();
//Searching For Delimiter
for (int i = 0; i < (samples32array.Length); i++) //Delimiter Samples Length = SampleRate*DelimiterTimeLength,( i.e: 44100*0.2=8820 samples)
{
if ((i + (int)DelimiterSamplesCount - 1) > samples32array.Length) break;
double power = Goertzel.GoertzelFilter(samples32array, StartDelimiterFreq, i, i + (int)DelimiterSamplesCount - 1);
if(power > max_power)
{
max_power = power;
max_power_index = i;
}
}
My Goertzel is like that:
public static double GoertzelFilter(float[] samples, double freq, int start, int end)
{
double sPrev = 0.0;
double sPrev2 = 0.0;
int i;
double normalizedfreq = freq / 44100;
double coeff = 2 * Math.Cos(2 * Math.PI * normalizedfreq);
for (i = start; i < end; i++)
{
double s = samples[i] + coeff * sPrev - sPrev2;
sPrev2 = sPrev;
sPrev = s;
}
double power = sPrev2 * sPrev2 + sPrev * sPrev - coeff * sPrev * sPrev2;
return power;
}
If you know the set of frequencies and the durations, then a set of sliding Goertzel filters is a good start to building a simple demodulator. Comparing and scanning for for a peak difference between these filters is a better decision criteria than just checking for a certain magnitude output.
I have a function filledFunction() that returns a float filled:
float filledFunction(){
if (FreqMeasure.available()) {
sum = sum + FreqMeasure.read();
count = count + 1;
if (count > 30) {
frequency = FreqMeasure.countToFrequency(sum / count);
a = frequency * x;
b = exp (a);
c = w * b;
d = frequency * z;
e = exp (d);
f = y * e;
float filled = c + f;
sum = 0;
count = 0;
return filled;
}
}
}
When I call this function with
while (1){
fillLevel = filledFunction();
int tofill = 500 - fillLevel;
Serial.print("fillLevel: ");
Serial.println(fillLevel);
Serial.print("tofill: ");
Serial.println(tofill);
The serial monitor should output two numbers that add up to 500 named fillLevel and tofill. Instead I get a repeating sequence of similar values:
http://i.imgur.com/Y9Wu8P2.png
The First two values are correct (410.93 + 89 = 500), but the following 60ish values are unknown to me, and do not belong there.
I am using an arduino nano
The filledFunction() function only returns a value if FreqMeasure.available() returns true AND count > 30. As stated in the answers to this question the C89, C99 and C11 standards all say that the default return value of a function is undefined (that is if the function completes without executing a return statement). Which really means that anything could happen, such as outputting arbitrary numbers.
In addition, the output that you're seeing starts off 'correct' with one of the numbers subtracted from 500, even when they have weird values such as 11699.00 and -11199 (which equals 500 - 11699.00). However, lower down in the output this seems to break down and the reason is that on Arduino Nano an int can only hold numbers up to 32767 and therefore the result of the subtraction is too big and 'overflows' to be a large negative number.
Fixing the filledFunction() function to explicitly return a value even if FreqMeasure.available() is false or count <= 30 and ensuring that it can't return a number greater than 500 will likely solve these issues.
I created a function that sums the digits of a given positive integer:
def digit_sum(n):
tot = 0
for i in str(n):
tot += int(i)
return tot
but I know that using mod10, mod 100 etc you can find the digits of a given number. So I thought there is an alternative way to construct the function without having the back and forth integer convert. Any ideas?
Shown here in Javascript: http://jsfiddle.net/bhQLa/
Break the digits out individually using
// Loop through the digits without using string
var base = 1;
while (base * 10 <= num) base *= 10;
while (base >= 1) {
var digit = (num - (num % base)) / base;
digits.push(digit);
num -= digit * base;
base /= 10;
}
// ---
Then sum them up. First increment the base to a maximum. Then decrement the base after grabbing the digit by (num - (num % base)) / base. Don't forget to decrement your working number, then the base.