To LUGNET HomepageTo LUGNET News HomepageTo LUGNET Guide Homepage
 Help on Searching
 
Post new message to lugnet.roboticsOpen lugnet.robotics in your NNTP NewsreaderTo LUGNET News Traffic PageSign In (Members)
 Robotics / 27264
27263  |  27265
Subject: 
Prime number generator
Newsgroups: 
lugnet.robotics
Date: 
Sat, 13 Oct 2007 20:38:42 GMT
Original-From: 
linmix <linmix@gmailSPAMCAKE.com>
Viewed: 
4631 times
  
I've been trying to write a prime number generator in NQC by way of
exercise to improve my programming skills, but I've run into some
unexpected trouble.

I have two sets of code that are virtually identical, one in NQC and the
other in plain C. The C code works as expected, but the NQC code which
has only been altered in some very minor ways to adapt to the
peculiarities of NQC will not show the prime numbers between 3 and 30 on
the screen, but every sinlge number from 5 till 30.

Can anyone explain why this doesn't work as expected and maybe even
provide a workaround?

Regards,
Linmix

*NQC code:*

int nmb, div, x;

task main()
{
  nmb = 3;
  while (nmb<30)
    {
      for (div=2;div<nmb/2;div++)
        {
          x = (nmb % div);
          if (x == 0)
            break;
        }
      if (div == nmb/2)
     {
        SetUserDisplay (nmb, 0);
      }
      nmb++;
      Wait (100);
    }
}

*C Code

*#include <stdio.h>
int nmb, div, x;

int main() // instead of task main()
{
  nmb = 3;
  while (nmb<30)
    {
      for (div=2;div<nmb/2;div++)
        {
          x = (nmb % div);
          if (x == 0)
            break;
        }
      if (div == nmb/2)
     {
        printf("\n%d", nmb); //instead of SetUserDisplay
      }
      nmb++;  //no 'Wait' command
    }
}*
*



Message has 1 Reply:
  Re: Prime number generator
 
(...) SetUserDisplay hooks up the display to whatever source you specify and until you tell it to hook up to a different source it will continue to display the value of that source until the program ends. The first time that your NQC code enters the (...) (17 years ago, 15-Oct-07, to lugnet.robotics)

2 Messages in This Thread:

Entire Thread on One Page:
Nested:  All | Brief | Compact | Dots
Linear:  All | Brief | Compact
    

Custom Search

©2005 LUGNET. All rights reserved. - hosted by steinbruch.info GbR