I'm trying to find a random number between 1-100000 that divided with 7 & 13 & 15,
the loop should run until it find the right number,
I'm trying to do:
from random import randint
while True:
for x in range (1,100000):
if x % 7 == 0 and x % 13 ==0 and x % 15 == 0:
print x
You could do that with a list comprehension:
l=[x for x in range(1,100000) if x % 7 == 0 and x % 13 == 0 and x % 15 == 0]
Then, to select a random value from that list, change the top line to import random, and you can do:
random.choice(l)
As we know, 7,13 and 15 are co-primes, then, we should start from 7*13*15 = 1365. Then you can apply #numbermaniac solution.
import random
random.choice([i for i in xrange(1365,1000000) if i%1365 == 0])
another solution might be,
# select random number between your upper_limit/1365
# then multiplying it 1365
random.randint(1, 732)*1365
Related
from __future__ import print_function
i = 0
for i in range (0, 100)
print(i, end = " ")
i += 1
if i % 10 == 0
print(" ")
print()
I just start my python learning several days ago.
It is about the for_loop training.
As my codes tell, it should print out numbers from 0 to 98 increasing by 2 till 98. As 10 digits printed out, it should print another 10 digits in the next line and keep going on. But it do not work properly. However, it works from changing function to
range(0,100) `
range(0,100)
But do not work when I change the function to
range(0, 100, 2)`
range(0, 100, 2)
Hint: When you use range(0, 100, 2), i is always even and i += 1 would make it odd, hence never satisfying i % 10 == 0. Try using different variable for digit counter or modifying the operands for addition and modulus.
I am new to coding and I am finding this site really helpful. So I have been trying to solve this problem and I am getting erroneous results, so I would be really grateful if you could help me out here.
The Problem: Find the sum of all the multiples of 3 or 5 below 1000. (For example, if we list all the positive integers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9, which sum is 23.)
My code:
count = 0
count1 = 0
for x in range(1000):
if x % 5 == 0:
count = count + x
if x % 3 == 0:
count1 = count1 + x
print count1 + count
What am I doing wrong?
Thanks in advance!
You want an elif in your code so you don't count the same x twice but a simpler way is to use an or with a single count variable:
count = 0
for x in range(1000):
if x % 5 == 0 or x % 3 == 0:
count += x
Which can be done using sum:
print(sum(x for x in range(3, 1000) if not x % 5 or not x % 3))
For completeness, a working version using your own code:
count = 0
count1 = 0
for x in range(1000):
if x % 5 == 0:
count += x
elif x % 3 == 0:
count1 += x
print count1 + count
ifs are always evaluated; so, for instance, when x is 15 it is evenly divisible by 5 and 3 so you count 15 twice, an elif is only evaluated if the previous if/elif evaluates to False so using elif only one occurrence of x will be added to the total.
Below 10 there is no number being multiple of both 5 and 3. But below 1000 there are several numbers divided exactly by 3 and 5 also (15, 45 ...).
So you need:
count=0
for x in range(1000):
if x % 5 == 0 or x % 3 == 0:
count=count + x
print count
Why will this not stop when len(prime) increments to 9,
I also tried using the counter but that did not work either. In this case it runs until it has check for primes to 100 not until if finds the first 10 primes.
prime = [2]
odd_list = [x for x in range(100) if x % 2 != 0 and x > 2]
count = 0
while len(prime) < 10:
for z in odd_list:
for y in range(2, z):
if z % y == 0: # is not prime
break
else:
prime.append(z)
count += 1
print 'count = ', count
print 'length of prime =', len(prime)
print prime
You need to check number of primes in internal loop:
prime = [2]
odd_list = [x for x in range(100) if x % 2 != 0 and x > 2]
count = 0
for z in odd_list:
for y in range(2, z):
if z % y == 0: # is not prime
break
else:
prime.append(z)
count += 1
if len(prime) >= 10:
break
print 'count = ', count
print 'length of prime =', len(prime)
print prime
In each interation of external while loop you run whole for loop:
while len(prime) < 10:
for z in odd_list:
...
so after first while iteration all primes will already be found, and only then you will stop while loop.
I'm currently flabbergasted with a seemingly easy problem.
I've got a 4x4 board with sequentially ordered elements, like so:
1 2 3 4
5 6 7 8
9 10 11 12
13 14 15 16
And I want to write list comprehensions that, given a direction and element, a list will be returned with all elements in the given direction.
For example: given direction North for element 10 would give 6 and 2.
I've got 3 directions nailed, North, South and West but I can't figure out East.
My list comprehensions for the 3 I've got are, with Id being the element in question and List a list from 1 to 16:
North->
[ X | X <- List, X < Id, (Id-X) mod 4 == 0]
South ->
[ X | X <- List, X > Id, (Id-X) mod4 == 0]
West ->
[ X | X <- List, X < Id, X > ((Id-1) div 4)*4]
I can't find a valid equation for East. Any help?
Edit:
East 14 should give 15 and 16 for example. East 16 should give the empty list. I'm really stumped.
The key to this problem is using division and modulo to tell which numbers are on the same lines.
If two values have the same modulo result, then they are on the same column. If they are less than the given value, then they are north. If they are more than the given value, they are south.
In general, if two values have the same integer result of division, they should be on the same row. This would always be true for 0-based squares; however, this example uses a square that starts at 1. Instead of testing the division results of the given value and the numbers on the board, subtract 1 from the given value and the test value, then divide and compare results.
Psuedo code
given = 10
for x in board:
if int( (given - 1) / 4 ) equals int( (x - 1) / 4 ):
if x < given:
x is west
if x > given:
x is east
Here is a sample function written in Python
import math
def figure(board, num, dir):
dir = dir.lower()
sq = math.sqrt(len(board))
if sq != int(sq) or num not in board:
return "invalid sqaure"
if dir == "n":
return [x for x in board if x < num and x%sq == num%sq]
elif dir == "s":
return [x for x in board if x > num and x%sq == num%sq]
elif dir == "e":
return [x for x in board if x > num and int((x-1)/sq) == int((num-1)/sq)]
elif dir == "w":
return [x for x in board if x < num and int((x-1)/sq) == int((num-1)/sq)]
else:
return "invalid direction"
I think the Esat shold be:
ESAT->
[ X | X <- List, X >Id, X <= ⌈Id / 4)⌉ * 4 ]
def sequence(n):
while n != 1:
print n,
if n%2 == 0: # n is even
n = n/2
else: # n is odd
n = n*3+1
sequence(6)
6 3 10 5 16 8 4 2
Why the output doesn't include 1 here?Many many thanx!
try using <= or >= for instance, while n >= 1. That should do what you need :)
You have the while loop set on breaking if n == 1. Try possibly doing n > 0 or n >= 1.
while n != 1:
print n
Once n gets a value of 1, it won't enter the loop, thus not allowing 'n' to be printed.