Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Basic loops, please help a bro out :D
New Posts  All Forums:Forum Nav:

Okay so here is the situation. Im just trying to come up with some general structure for a few loops to solve what the factors of a number are.
In no specific language though because i just wanted to write them out in general before i translate them into lisp,
and for my specific problem im not allowed to use division or modulus or subtraction ( but in this code its okay if i use n-- for example because i can easily convert that)
so this is kinda what i have so far, its not complete because im stumped, i know im on the right track though.
Code:
``````// num will be whatever the function at the time uses
n = num
i = 1
while( i <= (num*.05)){
while( n >= 0){
if( n = 0)
//we know its a factor
else if( n >=0)
n-i;
else
//we know its not a factor
}
i++;
}```
```

i know its wrong thats what i need help with haha the gist of what i am trying to do is:

-okay for the factors you only need to check up though 1/2 of what the number your trying to find the factors for (for example say it was 8. obviously only check up to 4)
-and what i want to do is say like you start at i ( 1 ) as the first possible factor and if you subtract it from n and eventually get a zero then we know it is a factor.
- so for that one if we start at with i = 1 and subtract from 8, oh okay thats 7, which is >= 0 so do it again and again and finally it gets to zero, now we know its a factor
-now we increase i to 2 to see if 2 is a factor, sub it from 8 and eventually its 0 so we know its a factor.
-now 3. sub it 3 times and its less than 0 so we know it isnt a factor.
-now 4 sub it 2 times and we know its a factor

The more i just explained it the more i realized how wrong my code is but thats why because the execution of this is just stumping me.
I know now i maybe might need another while loop in there somewhere ( i attempted that originally but it was just blowing my mind hahah ) (i went ahead and tried another loop real quick)
So i would really apprichiate it if someone could help me out with this code

thanks a bunch!
Here ya go...in C and not the least bit optimized:
Code:
``````void print_factors(long input)
{
long i;
long j;

for (i = 0; i <= input; ++i)
for (j = input; j > 0; --j)
if (j * i == input)
printf("%ld\n", i);
}```
```
Should put you on the right track...
 Meh (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62
Logitech MX 518 My desk.
 Meh (14 items)
CPUMotherboardGraphicsRAM
i5 760 @ 3.8 Ghz GA-P55-USB3 Gigabyte GTX 460 16GB(4x4) Corsair Vengeance
Hard DriveOptical DriveCoolingOS
Samsung Spinpoint F3 1TB Asus Cheapo Corsair H70 Arch Linux
MonitorKeyboardPowerCase
LG 26LH20 HDTV Das Model S Ultimate Silent Corsair HX850 Lian Li(Lancool) PC-K62