I know this sounds like a very specific question, but I am making a key generator for a program, and one part of the key has seven digits that need to be a multiple of seven when added together. How do you achieve this?
Generate a random 6-digit number then choose the number for the 7th digit that makes the algorithm work.
See any example of how to use std::rand for generating the random digits.
Once you have generated the 7 numbers for the key. You could than try a %7.
If the result is 0, than it mean that your number generated is a multiple of 7.
If the result is not 0, than you just have to regenerate another key or add 1 to the last number until modulo 7 (%7) equal 0.
I'm working on c++ and I need help writing a program that prints all numbers between 1 and 200 excluding multiples of 7. I understand how to print the number between 1 and 200, but I don't understand how to print them excluding multiples of 7.
Use the modulo operator % to determine if the number is a multiple of 7.
if number % 7 == 0, then it's a multiple of seven, and then you don't print it.
You can pre calculate the multiples of 7, storing them somewhere.
Then, for every value in the 'for' you print only if the number is not in where you stored the pre calculations.
I need to fill up a 2D array of size n with random numbers, however I need about n(n-1)/2 * .2 or .4 or .8 of the generated random numbers to be -1 and I need them spread out. Is there any possible way to do this? I thought about while filling up the array every so often make it set that element as -1 and use a count to keep track however there is no way to be sure those -1 are spread apart across the array, is this possible?
The easiest way that I can think of to do this would be:
fill out the start of your array with the required amount of -1s
fill out the rest of the array with random numbers
shuffle the array, possibly using std::random_shuffle or something similar.
After doing this, you can be sure that you have the correct amount of -1s, and that they will be randomly distributed.
If setting up the array is a once-off operation (i.e., you're not continuously changing elements and expecting the proportion of -1 value to remain constant(1)), you can first just not populate the array with -1 values at all, something like (pseudo-code):
for index in 0 .. array.size():
value = -1
while value == -1:
value = yourRandomFunctionIncludingNegOne()
array[index] = value
Then, you simply change a given number of them to -1 after population:
for index in 0 .. array.size() / 5: # 20%
index = random(array.size())
do:
index = random(array.size())
while array[index] == -1
array[index] = -1
(1) You can still use this method if the array is constantly changing, you just need to keep track of the negative-one and other-than-negative-one counts and adjust on the fly. It's a little more complicated but still doable.
I am aware that next_permutations() in the STL library gives you ALL the possible permutations. However, how can I get specific permutations with a fixed position.
An Example:
Suppose I have the bit format XXXXX(where X is 1 or 0). Can I do something where I keep the second index 1, say (now being X1XXX), and want to find all other permutations for that specific case? Basically, I want the final result as the permutations of ALL other positions when the second index is 1.
Something like (2nd element is always fixed as 1):
01000
01001
...
11000
..
An emirp (prime spelled backwards) is a pime number whose reversal is also prime. Ex. 17 & 71. I have to write a program that displays the first 100 emirps. It has to display 10 numbers per line and align the numbers properly:
2 3 5 7 11 13 17 31 37 71
73 79 97 101 107 113 131 149 151 157.
I have no cue what I am doing and would love if anyone could dump this down for me.
It sounds like there are two general problems:
Finding the emirps.
Formatting the output as required.
Break down your tasks into smaller parts, and then you'll be able to see more clearly how to get the whole task done.
To find the emirps, first write some helper functions:
is_prime() to determine whether a number is prime or not
reverse_digits() to reverse the digits of any number
Combining these two functions, you can imagine a loop that finds all the numbers that are primes both forward and reversed. Your first task is complete when you can simply generate a list of those numbers, printing them to the console one per line.
Next, work out what format you want to use (it looks like a fixed format of some number of character spaces per number is what you need). You know that you have 100 numbers, 10 per line, so working out how to format the numbers should be straightforward.
Break the problem down into simpler sub-problems:
Firstly, you need to check whether a number is prime. This is such a common task that you can easily Google it - or try a naive implementation yourself, which may be better given that this is homework.
Secondly, you need to reverse the digits of a number. I'd suggest you figure out an algorithm for this on a piece of paper first, then implement it in code.
Put the two together - it shouldn't be that hard.
Format the results properly. Printing 10 numbers per line is something you should be able to figure out easily once the rest is done.
Once you have a simple version working you might be able to optimise it in some way.
A straight forward way of checking if a number is prime is by trying all known primes less than it and seeing if it divides evenly into that number.
Example: To find the first couple of primes
Start off with the number 2, it is prime because the only divisors are itself and 1, meaning the only way to multiple two numbers to get 2 is 2 x 1. Likewise for 3.
So that starts us off with two known primes 2 and 3. To check the next number we can check if 4 modulo 2 equals 0. This means when divide 2 into 4 there is no remainder, which means 2 is a factor of 4. Specifically we know 2 x 2 = 4. Thus 4 is not prime.
Moving on to the next number: 5. To check five we try 5 modulo 2 and 5 modulo 3, both of which equals one. So 5 is prime, add it to our list of known primes and then continue on checking the next number. This rather tedious process is great for a computer.
So on and so forth - check the next number by looping through all previous found primes and check if they divide evenly, if all previously found primes don't divide evenly, you have a new prime. Repeat.
You can speed this up by counting by 2's, since all even numbers are divisible by two. Also, another nice trick is you don't have to check any primes greater than the square root of the number, since anything larger would need a smaller prime factor. Cuts your loops in half.
So that is your algorithm to generate a large list of primes.
Collect a good chunk of them in an array, say the first 10,000 or so. And then loop through them, reverse the numbers and see if the result is in your array. If so you have a emirp, continue until you get the first 100 emirps
If the first 10,000 primes don't return 100 emirps. Move on to the next 10,000. Repeat.
For homework, I would use a fairly simplistic isPrime function, pseudo-code along the lines of:
def isPrime (num):
set testDiv1 to 2
while testDiv1 multiplied by testDiv1 is less than or equal to num:
testDiv2 = integer part of (num divided by testDiv1)
if testDiv1 multiplied by testDiv2 is equal to num:
return true
Add 1 to testDiv1
return false
This basically checks whether the number is evenly divisible by any number between 2 and the square root of the number, a primitive primality check. The reson you stop at the square root is because you would have already found a match below it if there was one above it.
For example 100 is 2 times 50, 4 times 25, 5 time 20 and 10 times 10. The next one after that would be 20 times 5 but you don't need to check 20 since it would have been found when you checked 5. Any positive number can be expressed as a product of two other positive numbers, one below the square root and one above (other than the exact square root case of course).
The next tricky bit is the reversal of digits. C has some nice features which will make this easier for you, the pseudo-code is basically:
def reverseDigits (num):
set newNum to zero
while num is not equal to zero:
multiply newnum by ten
add (num modulo ten) to newnum
set num to the integer part of (num divided by ten)
return newNum
In C, you can use int() for integer parts and % for the modulo operator (what's left over when you divide something by something else - like 47 % 10 is 7, 9 % 4 is 1, 1000 % 10 is 0 and so on).
The isEmirp will be a fairly simplistic:
def isEmirp (num):
if not isPrime (num):
return false
num2 = reverseDigits (num)
if not isPrime (num2):
return false
return true
Then at the top level, your code will look something like:
def mainProg:
create array of twenty emirps
set currEmirp to zero
set tryNum to two
while currEmirp is less than twenty
if isEmirp (tryNum):
put tryNum into emirps array at position currEmirp
add 1 to currEmirp
for currEmirp ranging from 0 to 9:
print formatted emirps array at position currEmirp
print new line
for currEmirp ranging from 10 to 19:
print formatted emirps array at position currEmirp
print new line
Right, you should be able to get some usable code out of that, I hope. If you have any questions of the translation, leave a comment and I'll provide pointers for you, rather than solving it or doing the actual work.
You'll learn a great deal more if you try yourself, even if you have a lot of trouble initially.