Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C 

Need to create a simple program for homework

Here is what I have to do:

You know that the combination to a safe is a four-digit integer (between 1000 and 9999) that:
a) is a perfect square, and
b) is not divisible by 2, 3 or 5.


Could anyone help me out on where i'd start? I'm really bad at C++ and think i'm gonna fail my class lol
If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.
Assuming you have to code it, the way I would start is by having a loop that goes from:

32 till 99.

In each of these iterations of the loop, get the squared number (i.e. number times itself, then check if it's divisible by 2, 3 or 5.) If it isn't divisible by any of them. Print it out.

Of course that's just assuming this is what the assignment actually wants you to do.

You'll need to use a for loop and an if statement to accomplish this.
(Nov 1, 2018 03:55 PM)Mouse❤️ Wrote:  If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.

LOOOOOOOOOL poor guy
Nobody will help you here, it's about your programming education, if you don't like programming that means you are in a wrong school
Hey,
The Solution is quite easy to understand.

Code
#include <math.h>
#include <iostream>


using namespace std;
bool isDivisible(int x, int n);
bool isPerfectSquare(int n);
int main(){
    int a=1000;
    while(a<10000){
        if(!isDivisible(a,2) && !isDivisible(a,3) && !isDivisible(a,5) && isPerfectSquare(a)){
            cout<<a<<"\n";
        }
        a++;
    }
    return 0;
}
bool isPerfectSquare(int n){
    int squareRootN=(int)(sqrt((double)n)+0.5);
    return squareRootN*squareRootN == n;
}
bool isDivisible(int x, int n){
    return ((double)x/(double)n) == (double)(x/n);
}

Do it now yourself, use stackoverflow and other sites to gain informations how other people have solved this problem.

(There is a better version and you should be able to create it now on your own)
(Nov 2, 2018 08:37 AM)Abruptum Wrote:  Hey,
The Solution is quite easy to understand.

Code
#include <math.h>
#include <iostream>


using namespace std;
bool isDivisible(int x, int n);
bool isPerfectSquare(int n);
int main(){
   int a=1000;
   while(a<10000){
       if(!isDivisible(a,2) && !isDivisible(a,3) && !isDivisible(a,5) && isPerfectSquare(a)){
           cout<<a<<"\n";
       }
       a++;
   }
   return 0;
}
bool isPerfectSquare(int n){
   int squareRootN=(int)(sqrt((double)n)+0.5);
   return squareRootN*squareRootN == n;
}
bool isDivisible(int x, int n){
   return ((double)x/(double)n) == (double)(x/n);
}

Do it now yourself, use stackoverflow and other sites to gain informations how other people have solved this problem.

(There is a better version and you should be able to create it now on your own)

This is inefficient, I'm not sure why you're checking if each number is a perfect square at all. Just generate only perfect squares instead.
Don't forget about the modulus operator.

You can check if a number is divisible by another number the following way:
( x % y ) == 0, where x is the dividend, y the divisor.

Good luck with your assignment!
(Nov 2, 2018 12:21 PM)Souffrance Wrote:  This is inefficient, I'm not sure why you're checking if each number is a perfect square at all. Just generate only perfect squares instead.

OMG ;D it was just an bad example so he could improve it.
Pleas read the whole post and don't bitch around.
A short example program 🤝.
PHP Code

<?php 
#include <cmath>
#include <array>
#include <vector>
#include <iostream>
#include <numeric>
#include <algorithm>

int main()
{
 
   const auto is_div = [](const short x, const short y) -> bool {
 
       return x % y == 0;
 
   };

 
   const auto is_sqrt = [](const short n) -> bool {
 
       const auto sr = std::sqrt(n); 
 
       return ((sr - std::floor(sr)) == 0);
 
   };

 
   std::array<short, 9000> data;

 
   std::iota(data.begin(), data.end(), 1000);

 
   std::vector<short> copyOfData;

 
   const auto itr = std::copy_if(data.begin(), data.end(), std::back_inserter(copyOfData), [&is_div, &is_sqrt](const short n) {
 
       return is_sqrt(n) && is_div(n, 2) && is_div(n, 3) && is_div(n, 5); 
 
   });

 
   for (const auto n : copyOfData)
 
       std::cout << n << '\n';

 
   return 0;
} 
(Nov 1, 2018 03:55 PM)Mouse❤️ Wrote:  If that's your assignment, I see why you're failing. There aren't even any instructions. It's just telling you what an incorrect definition of a number combination is.

after first reading the original post, I was confused. After reading your reply, I laughed so hard I'm now dead.
im a bad gpt3 AI.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[C] by Mad HatterNov 23, 2016 06:26 PM 3 419 Nov 23, 2016 06:26 PM
Last Post: REDRIFLE
[C#] by VerizurMar 20, 2016 05:44 PM 21 856 Mar 20, 2016 05:44 PM
Last Post: Corios
[.NET] by Portland TimbersJul 19, 2015 09:11 PM 2 134 Jul 19, 2015 09:11 PM
Last Post: Portland Timbers