Overclock.net banner
41 - 60 of 99 Posts

·
Registered
Joined
·
1,198 Posts
The ultimate challenge!

Increment a 64bit variable from 0 to maximum of the unsigned type.

In GNU C, 64bit variable is declared unsigned long long.
 

·
Premium Member
Joined
·
5,282 Posts
Quote:

Originally Posted by metala View Post
The ultimate challenge!

Increment a 64bit variable from 0 to maximum of the unsigned type.

In GNU C, 64bit variable is declared unsigned long long.

Code:

Code:
int main(int argc, char **argv){
Code:
unsigned long long x = 0; --x; return 0;
}
 

·
Registered
Joined
·
1,198 Posts
Increment, not decrement!

PS. I forgot to add "by +1".
 

·
Premium Member
Joined
·
5,282 Posts
Quote:


Originally Posted by metala
View Post

Increment, not decrement!

PS. I forgot to add "by +1".

Oh fine

Code:
Code:
int main(int argc, char **argv){[QUOTE]unsigned long long x = 0;
while(x+++2);
return 0;[/QUOTE]}
 

·
Premium Member
Joined
·
5,282 Posts
Quote:


Originally Posted by Kuntz
View Post

I don't get the challenge, but oh well:

Code:
Code:
:xor rax, rax
inc rax
jne @B
That will terminate when rax is zero again, not when it's the max value
 

·
Banned
Joined
·
2,461 Posts
Quote:


Originally Posted by rabidgnome229
View Post

That will terminate when rax is zero again, not when it's the max value

I know, but he said just to increment it, I assume because he thinks someone will run it and not realize it would probably take 200 years to execute.
 

·
Registered
Joined
·
1,198 Posts
Quote:


Originally Posted by Kuntz
View Post

I know, but he said just to increment it, I assume because he thinks someone will run it and not realize it would probably take 200 years to execute.

157 years to be more exact, for a code written in C on my CPU, single thread, gcc, Debian Lenny amd64.

WTH is "x+++2"
The loop is fine, you want to loop until rewind, but WTH is this operator. Post increment?? with what?
It think I understand it ... "x++ + 2"
 

·
Registered
Joined
·
821 Posts
Quote:


Originally Posted by metala
View Post

The ultimate challenge!

Increment a 64bit variable from 0 to maximum of the unsigned type.

In GNU C, 64bit variable is declared unsigned long long.

Behold the power of the union:


Code:
Code:
#include "stdafx.h"
#include <process.h>
#include <limits.h>

union CounterType {
    unsigned long long Counter;
    unsigned char CounterBytes[sizeof(unsigned long long)];
};

CounterType TheCounter = {0};

HANDLE thread[sizeof(unsigned long long)] = {0};

unsigned __stdcall IncremtntThreadProc(void* ptr)
{
    const int index = (int)ptr;
    while (TheCounter.CounterBytes[index] != UCHAR_MAX)
        ++TheCounter.CounterBytes[index];
    return 0;
}

void StartThreads()
{
    for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++)
    {
        unsigned id;
        thread[i] = (HANDLE)_beginthreadex(0, 0, IncremtntThreadProc, (void*)i, 0, &id);
    }
}

void WaitForThreadsToFinish()
{
    WaitForMultipleObjectsEx(sizeof(thread)/sizeof(thread[0]), thread, TRUE, INFINITE, TRUE);
    for (int i = 0; i < sizeof(thread)/sizeof(thread[0]); i++)
        CloseHandle(thread[i]);
}

int _tmain(int argc, _TCHAR* argv[])
{
    CounterType* pCounter = &TheCounter;
    StartThreads();
    WaitForThreadsToFinish();
    bool success = pCounter->Counter == _UI64_MAX;
return 0;
}
 

·
Premium Member
Joined
·
5,282 Posts
Quote:

Originally Posted by metala View Post
157 years to be more exact, for a code written in C on my CPU, single thread, gcc, Debian Lenny amd64.

WTH is "x+++2"
The loop is fine, you want to loop until rewind, but WTH is this operator. Post increment?? with what?
It think I understand it ... "x++ + 2"
Yup - that's it. It can be written (x++) +2
 

·
Banned
Joined
·
2,461 Posts
Quote:


Originally Posted by rabidgnome229
View Post

Yup - that's it. It can be written (x++) +2

I think it could also be written:

while(++x+1);
 

·
Registered
Joined
·
219 Posts
Quote:

Originally Posted by Spotswood View Post
Circular Primes in C++, built using a Test Driven Development approach:

Code:

Code:
#pragma once

#include "IStringRotator.h"

class StringRotator : public IStringRotator
{
public:
    StringRotator() {}

    virtual std::string Rotate(const std::string& str)
    {
        if (str.size() < 2)
            return str;
        const std::string firstChar = str.substr(0, 1);
        const std::string restOfString = str.substr(1);
        return restOfString + firstChar;
    }

    virtual std::vector<std::string> RotateForSizeOf(const std::string& str)
    {
        int rotations = (int)str.size() -1;
        if (rotations < 1)
            return std::vector<std::string>();

        std::string intermediate(str);
        std::vector<std::string> result;
        for (int i = 0; i < rotations; ++i)
            result.push_back(intermediate = Rotate(intermediate));
        return result;
    }

};
Code:

Code:
#pragma once

#include "IPrimalityTest.h"
#include <math.h>

class PrimeNumberValidator : public IPrimalityTest
{
public:
    PrimeNumberValidator() {}

    virtual bool IsPrimeNumber(int n)
    {
        if (n < 2)
            return false;
        double f = n + 0.0;
        int z, y = (int)sqrt(f);
        for (z = 2; z <= y; z++) 
        {
            if ((n % z) == 0)
                return false;
        }
        return true;
    }
};
Code:

Code:
#include "cppunit/extensions/HelperMacros.h"
#include "cppunit/TestFixture.h"
#include <stdio.h>

#include "OutputStream.h"
#include "StringRotator.h"
#include "PrimeNumberValidator.h"

class MockOutputStream : public IOutputStream
{
public:
    MockOutputStream() : m_counter(0) {}
    virtual void WriteLine(const std::string& line)
    {
        m_counter++;
        std::cout<< line << std::endl;
    }
    int m_counter;
    std::string m_output;
};

class CircularPrimesGenerator
{
public:
    CircularPrimesGenerator(IOutputStream* output, IStringRotator* rotator, IPrimalityTest* validator)
        : m_output(output), m_rotator(rotator), m_validator(validator)
    {
    }

    void Generate(int max)
    {
        for (int i = 0; i <= max; i++)
        {
            if (!m_validator->IsPrimeNumber(i))
                continue;
            char buf[16] = {0};
            std::vector<std::string> rotations = m_rotator->RotateForSizeOf(itoa(i, buf, 10));
            if (AllArePrimeNumbers(rotations))
                m_output->WriteLine(buf);
        }
    }

    bool AllArePrimeNumbers(std::vector<std::string> numbers)
    {
        if (numbers.size() == 0)
            return false;
        std::vector<std::string>::iterator it = numbers.begin();
        for (; it != numbers.end(); ++it)
        {
            int possiblePrime = atoi((*it).c_str());
            if (!m_validator->IsPrimeNumber(possiblePrime))
                return false;
        }
        return true;
    }

    virtual ~CircularPrimesGenerator()
    {
        delete m_output;
        delete m_rotator;
        delete m_validator;
    }

    IOutputStream* get_OutputStream() { return m_output; }
    IStringRotator* get_Rotator() { return m_rotator; }
    IPrimalityTest* get_Validator() { return m_validator; }

private:
    __declspec(property(get = get_OutputStream)) IOutputStream* OutputStream;
    IOutputStream* m_output;

    __declspec(property(get = get_Rotator)) IStringRotator* Rotator;
    IStringRotator* m_rotator;

    __declspec(property(get = get_Validator)) IPrimalityTest* Validator;
    IPrimalityTest* m_validator;

private:
    CircularPrimesGenerator(const CircularPrimesGenerator&);            // not implemented
    CircularPrimesGenerator& operator=(const CircularPrimesGenerator&); // not implemented
};

namespace UnitTests {
class CircularPrimesGeneratorTests : public CppUnit::TestFixture
{
    CPPUNIT_TEST_SUITE( CircularPrimesGeneratorTests );
    CPPUNIT_TEST( Ctor );
    CPPUNIT_TEST( Generate );
    CPPUNIT_TEST_SUITE_END();
public:
    CircularPrimesGeneratorTests() {}

    void Ctor() 
    {
        CircularPrimesGenerator generator(new OutputStream("results.txt"),
                                            new StringRotator,
                                            new PrimeNumberValidator);
        CPPUNIT_ASSERT(generator.OutputStream);
        CPPUNIT_ASSERT(generator.Rotator);
        CPPUNIT_ASSERT(generator.Validator);
    }

    void Generate() 
    {
        MockOutputStream* output = new MockOutputStream;;
        CircularPrimesGenerator generator(output,
                                            new StringRotator,
                                            new PrimeNumberValidator);
        generator.Generate(1000000);
        CPPUNIT_ASSERT(output->m_counter == 51);

    }

};
CPPUNIT_TEST_SUITE_REGISTRATION(CircularPrimesGeneratorTests);

}
51 of them...

Code:

Code:
11
13
17
31
37
71
73
79
97
113
131
197
199
311
337
373
719
733
919
971
991
1193
1931
3119
3779
7793
7937
9311
9377
11939
19391
19937
37199
39119
71993
91193
93719
93911
99371
193939
199933
319993
331999
391939
393919
919393
933199
939193
939391
993319
999331


Has anyone tested this? If it works, then hats off to you spotswood. ^_^

anyway, illl actually LOOK at the code later.. just thought it was cool that someone finally got it.
 

·
Registered
Joined
·
88 Posts
Quote:


Originally Posted by The Bartender Paradox
View Post

Lisp/Scheme is really a fun language if you take the time to learn it. It may look a little odd with all the parentheses, but once you get past that it begins to look really beautiful.

i use to think this about scheme, until i did a language concepts course and one of the projects was to build a scheme interpreter in scheme without just reusing the commands. i can't look at scheme without getting a headache...but i must say i liked the ability to have the program create a function on the fly
 

·
Registered
Joined
·
219 Posts
Can we get some more challenges?

btw, spotswood, would you mind sending me the source code for that prime numbers test?
 

·
Registered
Joined
·
821 Posts
Quote:


Originally Posted by Action Trey!
View Post

...
btw, spotswood, would you mind sending me the source code for that prime numbers test?

See attachment.
 

·
Premium Member
Joined
·
11,951 Posts
Discussion Starter · #57 ·
Quote:

Originally Posted by Action Trey! View Post
Can we get some more challenges?
Sure, I'll start writing some up


I'll have them posted by tomorrow.
 

·
Registered
Joined
·
219 Posts
Quote:

Originally Posted by Spotswood View Post
See attachment.
Thank you.

[QOUTE=Manyak]Sure, I'll start writing some up

I'll have them posted by tomorrow. [/QOUTE]

nice i'm ready =D
 

·
Registered
Joined
·
821 Posts
NUnit test fixture for "The number of Sundays that were the 1st of a month in the 20th century" problem:

Code:

Code:
using System;
using System.Collections.Generic;
using System.Text;
using NUnit.Framework;

//
//  Create a program that calculates the number of Sundays 
//  that were the 1st of a month in the 20th century.
//
namespace OCN
{
    /*
     *  Interfaces...
     */

    public interface IMonth
    {
        DayOfWeek FirstDayOfMonth { get; }
    }

    /*
     *  Concrete classes...
     */
    public class Month : IMonth
    {
        private DateTime _begin;

        public Month(DateTime begin) { _begin = begin; }

        public DayOfWeek FirstDayOfMonth { get { return _begin.DayOfWeek; } } 
    }

    public class TwentiethCentury
    {
        private DateTime _begin;

        public TwentiethCentury() { _begin = new DateTime(1900, 1, 1); }
        public TwentiethCentury(DateTime begin) { _begin = begin; }

        public IEnumerator<IMonth> GetEnumerator()
        {
            DateTime d = _begin;
            while (d.Year != 2000)
            {
                yield return new Month(d);  // Very cool language feature
                if (d.Month < 12)
                    d = new DateTime(d.Year, d.Month + 1, 1);
                else 
                    d = new DateTime(d.Year + 1, 1, 1);
            }
        }
    }

    /*
     *  Tests...
     */
    [TestFixture]
    public class TwentiethCenturyOfSundaysTests
    {
        [Test]
        public void EnumerateEntireTwentiethCentury()
        {
            int count = 0;
            TwentiethCentury century = new TwentiethCentury();
            foreach (IMonth month in century)
                ++count;
            Assert.IsTrue(count == 12 * 100);
        }

        [Test]
        public void MumberOfSundaysThatWereThe1stOfAMonthInThe20thCentury()
        {
            int count = 0;
            TwentiethCentury century = new TwentiethCentury();
            foreach (IMonth month in century)
            {
                if (month.FirstDayOfMonth == DayOfWeek.Sunday)
                    ++count;
            }
            Assert.IsTrue(count == 172);
        }
    }
}
 
41 - 60 of 99 Posts
Top