I want to generate a list that contains random numbers from -100 to 100. The code I have so far just goes from -50 to 50.
for(int line=0;line<100;line++) //reads individual lines and compares them
{
ofs<<rand()%101 + (-50)<<endl;
}
I would really appreciate it if someone could point me in the right direction!
Change to %201 and +(-100). A little experimentation would have figured this out.
Note that you want values from -100 to +100, which is a range of 200 values, so you'd really want:
rand() % 200 // possible off-by-one error
This will give you 0..199. To get closer to the range you want, subtract 99:
(rand() % 200) - 99
This will give you the range -99..+100. This is closer to what you want.
You should be able to go back now and tweak to get the range you really want.
rand() %201 - 100 works just right
Related
I am in need of finding prime numbers in arithmetic progression
80218110*n+8021749, n=1 to 100,000
I was told that using Sage would be a good option, since my computer is old. I happen to be new to Sage and I haven't found it to solve my problem, I guess it shouldn't be difficult, does anyone have a good reference for printing primes in arithmetic progressions?
SageMath is based on Python, and Python provides a syntax which should be comfortable for mathematicians:
[80218110*n + 8021749 for n in range(100)]
range(100) is the ordered set 0, 1, 2, ..., 99, and so the previous line evaluates 80218110*n + 8021749 for these values of n. We can also test whether the entries are prime:
INPUT: [80218110*n+8021749 for n in range(100) if (80218110*n+8021749).is_prime()]
OUTPUT:
[8021749,
489330409,
569548519,
970639069,
1050857179,
1131075289,
1772820169,
2093692609,
2173910719,
3136528039,
3617836699,
4660672129,
4740890239,
5382635119,
6425470549,
7067215429,
7227651649,
7548524089]
You can of course make the argument to range larger, but maybe it's not a good idea to print the whole list.
INPUT: len([80218110*n+8021749 for n in range(100000) if (80218110*n+8021749).is_prime()])
OUTPUT: 15273
(len returns the length of the list.) Producing this list is pretty quick, at least on my computer:
INPUT: %time L = [80218110*n+8021749 for n in range(100000) if (80218110*n+8021749).is_prime()]
OUTPUT CPU times: user 94.6 ms, sys: 1.13 ms, total: 95.8 ms
Wall time: 95.5 ms
(ms is milliseconds.)
I need a ROUND calculation to always round up when it lands between 0 and 1 (but not when it's a value above these numbers), but can't seem to figure out how to make it work.
This is what I have currently:
=ROUND(100/DATA!H6)
try:
=IF((100/DATA!H6>0)*(100/DATA!H6<1), ROUNDUP(100/DATA!H6, ), 100/DATA!H6)
the code below gives me 0% for each 20th iteration, instead of the actual percentage I would like it to show.
n=100
for i in range(n):
if i% 20 ==0:
print str(i/n*100) + '%, Progress'
Results:
0%, Progress
0%, Progress
etc.....
I must be missing something really simple. Thank you.
change the division to i/(float)n*100 so that the resulting output will be formatted to decimal points by the python interpreter.
Division automatically rounds down to the nearest integer.
What happens in your code is:
i = 20
n = 100
i/n = 20/100, which becomes 0.
Then (i/n)*100 = 0*100 = 0.
You could solve this by first multiplying i by 100 and then dividing by n:
i*100/n
n=100
for i in range(n):
print(i);
if i% 20 ==0:
print str((float(i)/float(n))*100) + '%, Progress'
for python i/n is an (int)/(int) according to your variable declaration. so ti tries to give an int answer for i/n which is always 0.
In each iteration i/n*100 gets rounded down to the nearest integer (this is how the division operator works on integers in python2).
You could either use explicit casting to float() or execute from __future__ import division beforehand. This would prevent the division operator from rounding down automatically.
Here you can find a detailed description of a similar problem.
So, I have a query that returns a number of records. And on my results page I display a line that says: Results 1 to 10 out of 30+ results are shown below.
The 30+ number I get by doing this:
<!--- round to the nearest ten --->
<cfset totalfoundRounded = Round(myquery.recordcount/ 10) * 10>
This works great if my recordcount is for example 34 or less. Or 24 or less, basically as soon as it gets over 5, my code rounds up. So even if there are only 18 records, it will say 20+ records found.
I have been scratching my head on how to get this logic to work, so that it always rounds down to the nearest 10, even if it's 18, or 19.
I tried using int() but that didn't work, it only works on decimal places.
So, is there a function in coldfusion, or some technique I'm missing to get this to work? I have been unable to find anything searching, and math is not my strong point :(
Thank you very much for any suggestions!!
Try this?
<cfset totalfoundRounded = Int(myquery.recordcount/ 10) * 10>
What do you want it to say when it's less than 10? 0+? You can probably do number - (number % 10). This will always round down to the nearest multiple of 10.
What about this?
function Roundupdollars(amt) {
roundedAmt = amt * 100
roundedAmt = Round(roundedAmt)
roundedAmt = roundedAmt / 100
return roundedAmt;
}
I'd like to display only the numbers that lie in the thousands position for a value i.e. 193000 will be displayed as 193 and so on. How do I achieve this, maybe at the template level?
Divide by 1000?
as long as 193000 is an int, you can just divide it by 1000:
x = 193000.1
y = int(x)/1000
there's no filter already in Django, but you could add one yourself: http://docs.djangoproject.com/en/dev/howto/custom-template-tags/#howto-custom-template-tags