Determine something imposible for me...

I have a program inputing n number (exm.1000, 10000), and i have to get that number and figure out in a nested for loop only (No Arrays) the factor (or divisors) and after that if the factor of n number is even output = open
if its odd output = closed.

int a, b,

while (n <= SENTINEL)

for (a =1; a<= n number; a ++)

for (b=1; b<=n number; b++)// the is wrong but i cant figure out how to getthe

factors from the first for loop in the second

and the determine if they are closed or open.

I do wana say that i am having a hard time with logic behind it also,

Any advise is greatly appreciated.

Thanks for the time.

You are hard to understand. Do you want to find the number of factors or the factors themselves?

Either way,
1
2
for(int i = 0; i*i <= n; i  )
   if(n % i == 0) cout << i << " is a factor of " << n << 'n';
Little confused on what you need help with but maybe this will help.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
using namespace std;

int main(){

	int number = 0;

	cout << "Enter Number to be tested" << endl;

	cin >> number;
	cin.ignore();

	// i <= number /2 because no point testing past half of that number
	for(int i = 2; i <= number / 2; i  ) 
		if(number%i == 0) // if number is dividable by i than print out that it is
			cout << i << " is a factor of the number" << endl;

	if(number%2 == 0) // test if it is even because number modulate by 2 is zero if even
		cout << number << " is even" << endl;
	else
		cout << number << " is odd" << endl;
	
	cin.get();
	return 0;
}
Last edited on
first thanks for taking your time and sorry for the algorithm.

i want to find out the factors for each n number from the inner loop and from these factors

i need to figure out the factors that are even and odds.

Which determines what i am looking for.

evens = closed//this is what i need to output
odds = open // same here

Please, i am having a very hard time with this program and any help is appreciated

Again thanks for your time and help

Hi everyone thanks for the time and help

just wanted to add that

n number input from end user represents lockers

and the sample down is what i got this far

Thanks for the time everyone
Last edited on
// Purpose: This program is a game that requires an input for a n lockers.
// After the input the first sudent will open the lockers.
// Then the second student will close all the even-numberd lockers.
// Then the third student will chek every third locker
// if it's open it will close if it's closed it will open it.
// So every (n)-th student will check (n)-th locker and close
// it if it's open and open if its closed.
// When this program is over, it should print the total lockers
// that are open and the number of the lockers that are open.
//
// Input: The number of lockers.
//
// Output: The total lockers that are open.
// The number of the lockers that are open.
//
//********************************************************************************

// include statement(s).
#include <iostream>
#include <cmath>
#include <iomanip>

// using namespace statement.
using namespace std;
// Declare named constants, if necessary.
const int SENTINEL = -999;
int main()
{
int numLockers; //number of lockers
int a; //first for loop use
int b; //secon for loop use
int total; //tTotal lockers open

cout <<"Enter the number of the lockers: ";
cin >> numLockers;
cout <<"The number you entered is: " << numLockers << endl;

//while (n != SENTINEL)

for(a=1; a<=numLockers; a )
//cout << setw(3) << a << endl;

for (b=2; b<=a/2; b)// from this point on i cant seem to go any further

if(??)
output?


return 0;
}
So you want to count how many numbers between 1 and n, have an odd number of factors.
Change your approach. When a number will have an odd quantity of factors?

Hint: if p is a factor of n then q = n/p is also a factor of n.

Edit: [code]"Please use code tags"[/code]
Last edited on
Thanks for posting in this topic.

Can you please elaborate on what aproach i should take in regards to the one i posted.

I'm sory but i do not understand the hint you gave me.

for (a=1; a=number; a )//here i see all the numbers i get that.

1
2
3
for (?;?;?)
      
                if(??)
//on the second for how do i get each number and factorise it and determine if is even or odd.

i do not know how.

Please i need a jump start...
Last edited on
Suppose that you find a divisor of n. Then n/p = q equivalent to n = p*q
But then q is also a divisor of n.
So, when you find a divisor you actually find 2 divisors (p and q)
Example: 2 is a divisor of 6, and 6/2=3 is also a divisor of 6

So you could be tempted to say: all numbers have an even amount of divisors.
But that will be wrong, as it is easy to provide a counterexample 4 has as divisors 1, 2, 4 only 3 divisors (and odd number)
Now, what make 4 so especial?
A locker is closed if the locker number has an even number of factors.

so is stated in the problem. In the above example there are 3 total factors so its odd

odd = open locker

now if it was 4 total divisors then it's even 8 has four divisors 1, 2, 4, 8,

even = closed locker

How to get the divisors i do not know?

Thanks for your patience and time.


Last edited on
Topic archived. No new replies allowed.