Jan 4, 2013 at 10:15pm UTC
what number can be divided by 7 without a remainder but when divided by 2 3 4 5 or 6 leaves 1?
I've found the riddle answer for the above question to be 301
but I can't figure out how to write a c++ program to find me the answer using %. Some hints on how to go about this would be highly appreciated,
Thank You.
Jan 4, 2013 at 10:17pm UTC
How did you figure it out without a program? Just translate that thought pattern into something a computer understands
Last edited on Jan 4, 2013 at 10:23pm UTC
Jan 4, 2013 at 10:21pm UTC
Try Something like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
#include iostream
int main(){
int NumberEntered = 0;
while (NumberEntered % 7 != 0 && NumberEntered % 2 != 1 && NumberEntered % 3 != 1
&& NumberEntered % 4 != 1 && NumberEntered % 5 != 1 && NumberEntered % 6 != 1)
NumberEntered++;
//You could also do && NumberEntered != 301 if you do not want 301 as an answer
std::cout >> NumberEntered;
return 0;
}
Check if the number is valid, if not start again with the next number.
Last edited on Jan 4, 2013 at 10:25pm UTC
Jan 5, 2013 at 5:06am UTC
I basically did LCM of 2,3,4,5,6 which is 60...
then I added 1 to make it 61 and tested it using % and it wasn't funny divisible by 7.
so I kept adding the LCM 61 121 181 241 301
the first one that left no remainder was 301.
I tried something similar to your code Pickle but it seems very tedious because I need to go through every single number until I get to 301.
this is what I currently have:
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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
void main()
{
int a[7],n,i,j,c,max,min;
unsigned long prod;
printf("Enter the no. of entries: " );
scanf("%d" ,&n);
printf("Enter the entries:" );
for (i=0;i<n;i++)
{
if (c>0)
a[i]=c;
else
{
printf("Invalid Entry" );
return ;
}
}
max=a[0];
for (i=0;i<n;i++)
if (a[i]>=max)
max=a[i];
min=a[0];
for (i=0;i<n;i++)
if (a[i]<min)
min=a[i];
for (i=0,prod=1;i<n;i++)
;
for (i=max;i<=prod;i+=max)
{
c=0;
for (j=0;j<n;j++)
c+=1;
if (c==n)
{
printf("The LCM of the nos: %d" ,i);
break ;
}
}
getch();
}
I got up to the LCM but I need a test for % and a for loop to add 60 but I haven't been able to find a way to test the equality.
Hope all of that makes sense... I'm pretty new to c++.
Last edited on Jan 5, 2013 at 1:39pm UTC
Jan 5, 2013 at 7:50am UTC
Pretty simple man!
I am writing only the required code you can Add the user part and main() later on!
int flag;
for(int i=1;i<1000;i++)
{
flag=1;
if(i%7==0)
{
for(int j=2;j<7;j++)
if(i%j!=1)
{
flag=0;
break;
}
if(flag==1)
cout<<"match found:"<<i;
continue;
}
}
Jan 5, 2013 at 7:51am UTC
and its kinda amazing that you figured it out without writing a program! how did you do it?
Jan 5, 2013 at 1:05pm UTC
Apparently by hurting my head on paper ;-; . I guess I needed some sleep because I was heading to the right solution lol...
I'm usually good at programming from PSEUDOCODES but my professor apparently loves giving riddles :'( to dumbfound me haha
Thank you IcyFlame and others the cplusplus community is very helpful and very fast in responding :)
Last edited on Jan 5, 2013 at 1:08pm UTC
Jan 5, 2013 at 5:05pm UTC
Well, do tell me what is pseudo code?
Jan 5, 2013 at 6:47pm UTC
Well its a description or information in a certain manner to help you make a program.
Kind of like a basic algorithm to get you started on how to start and finish the program.
This riddle took me a while because I wasn't sure how to start it.
I also had to re-edit to be more informative to help a user understand the riddle.
Jan 6, 2013 at 7:51am UTC
oh.... well did the code that i had posted work?