Tags down


C++ question on prime numbers

By : cgaffga
Date : October 14 2020, 01:41 PM

Share : facebook icon twitter icon

Why is this check for prime numbers returning true on some non-prime numbers?

By : shiri
Date : March 29 2020, 07:55 AM
I wish this help you Possibly it because of the rounding of the sqrt(x) as result of this function call is floating point value. So it can be a little less than rounded to the closest integer.
In this case e.g. sqrt(25) could be rounded to 4 instead of 5.
code :
 if(!(x&1)) return 0;   //if x is even  return false
if(!(x%2)) return 0;   //if x is even  return false

C Programming: Sum of Prime Numbers of a Given Range Printing Both The Prime Numbers and The Sum

By : kwietstorm20
Date : March 29 2020, 07:55 AM
I wish this helpful for you The solution is to print no plus sign in front of the first number, and to print a plus sign in front of all other numbers. To achieve this I am using a boolean with false/true values.
code :
#include <stdio.h>
#include <stdlib.h>
int is_prime(int m)
    int i;
    for(i=2; i<=m/2; i++)
        if (m % i==0)
            return 0;

    return 1;
int main()
    int num, min, max, sum = 0;
    scanf("%d", &min);
    scanf("%d", &max);
    bool first = true;
    for(num = min; num <= max; num++)
            sum += num;
            printf("%d", num);
            first = false;

    printf("=%d\n", sum);
    return 0;

Question regarding R code to do with rounding prime numbers

By : user2584502
Date : March 29 2020, 07:55 AM
this one helps. I need to have a function where if a number entered is a prime number, it must round it up to the next prime number and if it is not to round it down to the previous prime number. , Using your prime checker,
code :
prime <- function(x) {
  if (x == 2) {
  } else if (any(x %% 2:(x-1) == 0)) {
  } else { 
new <- function(x){
  if (isTRUE(prime(x))){
    x = x+1
    while(prime(x) == FALSE){
      x = x+1
  } else {
    while(prime(x) == FALSE){
      x = x-1
> new(7)
[1] 11
> new(10)
[1] 7

Understand what is going on with my Prime Numbers Calculation and Prime Numbers JFrame

By : Seif Tama
Date : March 29 2020, 07:55 AM
I hope this helps you . set visible(true); is out side the constructor and is not a valid method name, it should be setVisible(true);, move up so that it appears just before the } that is currently above it... private JButton instantiateJButton(String, sInCaption, Color inColor) is not a valid method signature, you've added a , after String where it doesn't need to be, it should be private JButton instantiateJButton(String sInCaption, Color inColor) JOptionPane.showMessageDialog(null, "You pressed the "" sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE); is missing a + and has too many ". I think it should be JOptionPane.showMessageDialog(null, "You pressed the " + sActionCommand + " button", this.getTitle(), JOptionPane.INFORMATION_MESSAGE); ProgrammingAssignment8 = new ProgrammingAssignment8("Instantiates with a Method"); is not a valid assignment, you're not actually assigning the value to anything...something like ProgrammingAssignment8 assignment = new ProgrammingAssignment8("Instantiates with a Method"); will fix it.
The following is not a valid comment...

Check the prime numbers in the array and return the sum of the prime numbers

By : Alfred Quiambao
Date : October 04 2020, 08:00 AM
Hope that helps The first problem is checkPrime; it should take a single value and return true if that value is prime. The algorithm below is slightly optimized; we only need to test odd values (after checking two and its' multiples). Also, none of your methods depend on class state so I would remove the class fields and make the methods static.
code :
static boolean checkPrime(int v) {
    if (v == 1 || v == 2) {
        return true;
    } else if (v % 2 == 0) {
        return false;
    int sq = (int) Math.sqrt(v);
    for (int i = 3; i <= sq; i += 2) {
        if (v % i == 0) {
            return false;
    return true;
static int sum(int[] arr) {
    int total = 0;
    for (int i = 0; i < arr.length; i++) {
        if (checkPrime(arr[i])) {
            total += arr[i];
    return (total);
public static void main(String[] args) {
    int[] numbers = { 1, 2, 3 };
    System.out.println("The sum of primes is " + sum(numbers));
Related Posts Related Posts :
  • how to use dll?
  • C++ string literal data type storage
  • How to get qmake to generate "project dependencies" in a Visual Studio .sln project
  • Why would someone use C instead of C++?
  • C ReadProcessMemory - how to examine the memory area associated with a process
  • how can I force C++ macro substitution at the time I choose in this case?
  • C++: What is the appropriate use for the std::logic_error exception?
  • Is there a working C++ grammar file for ANTLR?
  • Copy Constructor?
  • How to start a voice quality PESQ test?
  • I'm trying to change my Maze traversal recursive coding part into a while loop
  • Providing less than operator for one element of a pair
  • Question about exact time of destruction of temporaries in C++
  • Please suggest some algorithm to find the node in a tree whose distance to its farthest node is minimum among all the no
  • Best practices with object manager
  • Using input to call a member function
  • How to find out if a character in a string is an integer
  • How to find a formatted number in a string?
  • Multiple Counter Problem In For Loop
  • Protecting /etc/passwd and /etc/shadow from concurrent access
  • Getting ptr from memory address with c++
  • How to make the Win32 APP Background Transparent?
  • How do you return a 2d array in C++?
  • static member initialization for specialized template class
  • How to find the location of two consecutive spaces in a string
  • c++ volatile multithreading variables
  • What is the difference between an array and a dynamic array?
  • Why is the Loki library not more widely used?
  • Corruption of the heap & F12 Problem
  • Colors in C++ win32 console
  • Position of elements in vector
  • How to convert an ASCII string to an UTF8 string in C++?
  • warning: GDB: Failed to set controlling terminal: Operation not permitted
  • How to find the first character in a C++ string
  • How to test if a string contains any digits in C++
  • C++ templated class implementation of the multiton pattern
  • C++ - Two Basic Questions
  • Finding all permutations that match a set of rules
  • How to get data out of the STL's const_iterator?
  • How can I make a banner in QT, like the news banner of CNN/FOX?
  • Various ways to get month name from inbuilt C library
  • How do I programmatically send email w/attachment to a known recipient using MAPI in C++? MAPISendMail()
  • Why is there no boost::copy_on_write_ptr?
  • How to create a radio button and see if it's checked?
  • Simple expression parser example using Boost::Spirit?
  • g++, R_X86_64_32S : what is it?
  • Find the numbers missing
  • What is wrong with my code? My program will not compile
  • Undefined references when including boost library
  • Avoiding Calls to floor()
  • C++ Need to compare one string to 200.000 words
  • Display web page using libgtkhtml c c++
  • wxWidgets and context menus
  • How to get PCM data from microphone in C++ (os Windows)?
  • Header file best practices for typedefs
  • Calling a method with this pointer from an inherited class becomes const
  • C socket API is thread safe?
  • Can a class member function template be virtual?
  • How do I get a string description of a Win32 crash while in Top level filter (I am looking for the address of the instru
  • Pcrepp - Perl Regular Expression syntax to match host name
  • shadow
    Privacy Policy - Terms - Contact Us © bighow.org