float* getPossibleRoots(int coefLast,int coefFirst,float possibleRoots[]) { int i,j,len1=0,len2=0,factors1[100],factors2[100]; memset(factors1,-1,sizeof(factors1)/sizeof(factors1[0])); memset(factors2,-1,sizeof(factors2)/sizeof(factors2[0])); getFactors(coefLast,factors1); getFactors(coefFirst,factors2); len1 = getQuantityOfFactors(factors1); len2 = getQuantityOfFactors(factors2); int m=0; for (i=0;i<len1;i++) { for (j=0;j<len2;j++) { if (factors1[i]%factors2[j]==0 || factors1[i]%factors2[j]==1) { possibleRoots[m++]=((float)factors1[i]/factors2[j]); possibleRootCounter++; printf("possibleRoots[%d]=%f\n",m-1,possibleRoots[m-1]); possibleRoots[m++]=(-1)*((float)factors1[i]/factors2[j]); possibleRootCounter++; printf("possibleRoots[%d]=%f\n",m-1,possibleRoots[m-1]); } } } return possibleRoots; }
vector<vector<int>> getFactors(int n) { if (n <= 3) return {{}}; vector<int> factors; getFactors(n, factors); vector<vector<int>> result; vector<int> v; getFactors(factors, 0, n, v, result); return result; }
//get bigger prime factor of number N void main(){ factors f = getFactors(N); while(!isPrime(f.a)){ f.a = getFactors(f.a).b; } while(!isPrime(f.b)){ f.b = getFactors(f.b).b; } printf("bigger factor: %llu\n",(f.a>f.b)?f.a:f.b); }
int main(void){ for(i = 2; i < 101; i++) for(j = 0,k = i; k!=1; j++) while(k%primes[j]==0) { pfact[i][primes[j]]++; k /= primes[j]; } scanf("%d",&i); while(i){ getFactors(); printf("%3d! =",i); for(j = 2,counter =0; j < 98 && j <= i ; j++) if(curr[j]){ if(counter == 15){ printf("\n "); counter = 0; } printf("%3d",curr[j]); counter++; } printf("\n"); scanf("%d",&i); } return 0; }
// Used for fib/factor since it's different from add,sub etc. // due to it requiring only one parameter // @fundex == choice of math function based our functions[] table // @para is the string of a number being passed in char * doMoreMath(int fundex, char * para){ char* a = malloc(strlen(para)+1); a = strcpy(a,para); int fpara = atoi(a); int * ret = NULL; char * str_ret = NULL; if(fundex == 5){ printf("we're computing the prime factors for %d\n", fpara); int * factors = getFactors(fpara); ret = factors; int i; char * str_temp = (char *)malloc(sizeof(char)*1000); printf("sizeof is %d\n",(int)(sizeof(factors)/sizeof(factors[0])+1)); for(i = 0; factors[i] != -1; i++){ printf("ret[%d] is %d\n",i,ret[i]); if(str_ret==NULL){ str_ret = malloc(sizeof(char)*1000); sprintf(str_ret,"%d",ret[i]); } else{ sprintf(str_temp, "%d", ret[i]); str_ret = strcat(str_ret, str_temp); } str_ret = strcat(str_ret, "\n"); }//end of for str_ret = strcat(str_ret, "\0"); }//end of prime factors else if(fundex == 7){ printf("we're getting the first %d fibonacci numbers\n",fpara); int i; int * fibs = (int *)malloc(sizeof(int)*fpara); for(i = 0; i < fpara; i++){ fibs[i] = getFib(i); }//end of for ret = fibs; char * str_temp = malloc(sizeof(char)*1000); for(i = 0; i < fpara; i++){ if(str_ret==NULL){ str_ret = malloc(sizeof(char)*1000); sprintf(str_ret,"%d",ret[i]); } else{ //str_temp = (char *)malloc(sizeof(char)*1000); printf("ret[%d] is %d\n",i,ret[i]); sprintf(str_temp,"%d",ret[i]); str_ret = strcat(str_ret,str_temp); } str_ret = strcat(str_ret, "\n"); }//end of for str_ret = strcat(str_ret, "\0"); }//end of fibonacci return str_ret; }//end of domoremath
void getFactors(vector<int>& factors, int idx, int n, vector<int>& v, vector<vector<int>>& result){ if (n == 1){ result.push_back(v); return; } for (int i = idx; i < factors.size() && factors[i] <= n; i++){ if (!(n%factors[i])){ v.push_back(factors[i]); getFactors(factors, i, n/factors[i], v, result); v.pop_back(); } } }
typec combinationModFac(typec n, typec k, typec m) { getFactors(m); typec a, b, p, q; for(int i = 0; i < facCnt; i++) { if(!i) a = combinationModPtFac(n, k, factor[i][0], factor[i][2]), p = factor[i][1]; else b = combinationModPtFac(n, k, factor[i][0], factor[i][2]), q = factor[i][1]; if(!i) continue; a = linearCongruence(a, b, p, q), p *= q; } return a; }
///you need to init the prime table typec combinationModLucas(typec n, typec k, typec m) { if(m == 1 || k > n) return 0; if(n - k < k) k = n - k; getFactors(m); typec a, b, p, q; for(int i = 0; i < facCnt; i++) { if(!i) a = generalizedLucas(n, k, factor[i][0], factor[i][2]), p = factor[i][1]; else b = generalizedLucas(n, k, factor[i][0], factor[i][2]), q = factor[i][1]; if(!i) continue; a = linearCongruence(a, b, p, q), p *= q; } return a; }
void main(){ int i; for (i=MAX_N; i>0; i--){ char str[100]; intToStr(i,str); bign n = getPalindromous(str); if(1){//modificações a fazer aqui factors f = getFactors((bign)n); if(f.a<=MAX_N && f.b>=MIN_N && f.b<=MAX_N && f.b>=MIN_N){ printf("bigger palindromous: %llu\n",n); return; } } } }
vector<vector<int>> getFactors(int n) { if(record.find(n)!=record.end()) return record[n]; vector<vector<int>> result; for(int i=2; i*i<=n; i++) { if(n%i!=0) continue; result.push_back({i, n/i}); vector<vector<int>> remnants = getFactors(n/i); for(auto r:remnants) { if(r[0]<i) continue; vector<int> sol(r); sol.insert(sol.begin(), i); result.push_back(sol); } } record[n] = result; return result; }
void blend(TToonzImageP ti, TRasterPT<PIXEL> rasOut, const std::vector<BlendParam> ¶ms) { assert(ti->getRaster()->getSize() == rasOut->getSize()); // Extract the interesting raster. It should be the savebox of passed cmap, // plus - if // some param has the 0 index as blending color - the intensity of that blend // param. unsigned int i, j; TRect saveBox(ti->getSavebox()); int enlargement = 0; for (i = 0; i < params.size(); ++i) for (j = 0; j < params[i].colorsIndexes.size(); ++j) if (params[i].colorsIndexes[j] == 0) enlargement = std::max(enlargement, tceil(params[i].intensity)); saveBox = saveBox.enlarge(enlargement); TRasterCM32P cmIn(ti->getRaster()->extract(saveBox)); TRasterPT<PIXEL> rasOutExtract = rasOut->extract(saveBox); // Ensure that cmIn and rasOut have the same size unsigned int lx = cmIn->getLx(), ly = cmIn->getLy(); // Build the pure colors infos SelectionRaster selectionRaster(cmIn); // Now, build a little group of BlurPatterns - and for each, one for passed // param. // A small number of patterns per param is needed to make the pattern look not // ever the same. const int blurPatternsPerParam = 10; std::vector<BlurPatternContainer> blurGroup(params.size()); for (i = 0; i < params.size(); ++i) { BlurPatternContainer &blurContainer = blurGroup[i]; blurContainer.reserve(blurPatternsPerParam); for (j = 0; j < blurPatternsPerParam; ++j) blurContainer.push_back(BlurPattern( params[i].intensity, params[i].smoothness, params[i].stopAtCountour)); } // Build the palette TPalette *palette = ti->getPalette(); std::vector<TPixel32> paletteColors; paletteColors.resize(palette->getStyleCount()); for (i = 0; i < paletteColors.size(); ++i) paletteColors[i] = premultiply(palette->getStyle(i)->getAverageColor()); // Build the 4 auxiliary rasters for the blending procedure: they are ink / // paint versus input / output in the blend. // The output raster is reused to spare some memory - it should be, say, the // inkLayer's second at the end of the overall // blending procedure. It could be the first, without the necessity of // clearing it before blending the layers, but things // get more complicated when PIXEL is TPixel64... RGBMRasterPair inkLayer, paintLayer; TRaster32P rasOut32P_1(lx, ly, lx, (TPixel32 *)rasOut->getRawData(), false); inkLayer.first = (params.size() % 2) ? rasOut32P_1 : TRaster32P(lx, ly); inkLayer.second = (params.size() % 2) ? TRaster32P(lx, ly) : rasOut32P_1; if (PIXEL::maxChannelValue >= TPixel64::maxChannelValue) { TRaster32P rasOut32P_2(lx, ly, lx, ((TPixel32 *)rasOut->getRawData()) + lx * ly, false); paintLayer.first = (params.size() % 2) ? rasOut32P_2 : TRaster32P(lx, ly); paintLayer.second = (params.size() % 2) ? TRaster32P(lx, ly) : rasOut32P_2; } else { paintLayer.first = TRaster32P(lx, ly); paintLayer.second = TRaster32P(lx, ly); } inkLayer.first->clear(); inkLayer.second->clear(); paintLayer.first->clear(); paintLayer.second->clear(); // Now, we have to perform the blur of each of the cm's pixels. cmIn->lock(); rasOut->lock(); inkLayer.first->lock(); inkLayer.second->lock(); paintLayer.first->lock(); paintLayer.second->lock(); // Convert the initial cmIn to fullcolor ink - paint layers buildLayers(cmIn, paletteColors, inkLayer.first, paintLayer.first); // Perform the blend on separated ink - paint layers for (i = 0; i < params.size(); ++i) { if (params[i].colorsIndexes.size() == 0) continue; selectionRaster.updateSelection(cmIn, params[i]); doBlend(cmIn, inkLayer, paintLayer, selectionRaster, blurGroup[i]); tswap(inkLayer.first, inkLayer.second); tswap(paintLayer.first, paintLayer.second); } // Release the unnecessary rasters inkLayer.second->unlock(); paintLayer.second->unlock(); inkLayer.second = TRaster32P(); paintLayer.second = TRaster32P(); // Clear rasOut - since it was reused to spare space... rasOut->clear(); // Add the ink & paint layers on the output raster double PIXELmaxChannelValue = PIXEL::maxChannelValue; double toPIXELFactor = PIXELmaxChannelValue / (double)TPixel32::maxChannelValue; double inkFactor, paintFactor; TPoint pos; PIXEL *outPix, *outBegin = (PIXEL *)rasOutExtract->getRawData(); TPixelCM32 *cmPix, *cmBegin = (TPixelCM32 *)cmIn->getRawData(); int wrap = rasOutExtract->getWrap(); TPixel32 *inkPix = (TPixel32 *)inkLayer.first->getRawData(); TPixel32 *paintPix = (TPixel32 *)paintLayer.first->getRawData(); for (i = 0; i < ly; ++i) { outPix = outBegin + wrap * i; cmPix = cmBegin + wrap * i; for (j = 0; j < lx; ++j, ++outPix, ++cmPix, ++inkPix, ++paintPix) { getFactors(cmPix->getTone(), inkFactor, paintFactor); outPix->r = tcrop( toPIXELFactor * (inkFactor * inkPix->r + paintFactor * paintPix->r), 0.0, PIXELmaxChannelValue); outPix->g = tcrop( toPIXELFactor * (inkFactor * inkPix->g + paintFactor * paintPix->g), 0.0, PIXELmaxChannelValue); outPix->b = tcrop( toPIXELFactor * (inkFactor * inkPix->b + paintFactor * paintPix->b), 0.0, PIXELmaxChannelValue); outPix->m = tcrop( toPIXELFactor * (inkFactor * inkPix->m + paintFactor * paintPix->m), 0.0, PIXELmaxChannelValue); } } inkLayer.first->unlock(); paintLayer.first->unlock(); cmIn->unlock(); rasOut->unlock(); // Destroy the auxiliary bitmaps selectionRaster.destroy(); }
// Calculates the estimate of blend selection in the neighbourhood specified by // blurPattern. inline void addSamples(const TRasterCM32P &cmIn, const TPoint &pos, const TRaster32P &inkRas, const TRaster32P &paintRas, const SelectionRaster &selRas, const BlurPattern &blurPattern, DoubleRGBMPixel &pixSum, double &factorsSum) { double inkFactor, paintFactor; unsigned int xy, j, l; int lx = cmIn->getLx(), ly = cmIn->getLy(); TPixel32 *color; TPoint samplePos, pathPos; const TPoint *samplePoint = blurPattern.m_samples.empty() ? 0 : &blurPattern.m_samples[0]; const TPoint *pathPoint; unsigned int i, blurSamplesCount = blurPattern.m_samples.size(); for (i = 0; i < blurSamplesCount; ++i, ++samplePoint) { // Add each samples contribute to the sum samplePos.x = pos.x + samplePoint->x; samplePos.y = pos.y + samplePoint->y; if (samplePos.x < 0 || samplePos.y < 0 || samplePos.x >= lx || samplePos.y >= ly) continue; // Ensure that each pixel on the sample's path (if any) is selected l = blurPattern.m_samplePaths[i].size(); pathPoint = blurPattern.m_samplePaths[i].empty() ? 0 : &blurPattern.m_samplePaths[i][0]; for (j = 0; j < l; ++j, ++pathPoint) { pathPos.x = pos.x + pathPoint->x; pathPos.y = pos.y + pathPoint->y; xy = pathPos.x + lx * pathPos.y; if (!(selRas.isPurePaint(xy) || selRas.isSelectedInk(xy))) break; if (!(selRas.isPureInk(xy) || selRas.isSelectedPaint(xy))) break; } if (j < l) continue; xy = samplePos.x + lx * samplePos.y; if (selRas.isSelectedInk(xy) && !selRas.isPurePaint(xy)) { getFactors(cmIn->pixels(samplePos.y)[samplePos.x].getTone(), inkFactor, paintFactor); color = &inkRas->pixels(samplePos.y)[samplePos.x]; pixSum.r += inkFactor * color->r; pixSum.g += inkFactor * color->g; pixSum.b += inkFactor * color->b; pixSum.m += inkFactor * color->m; factorsSum += inkFactor; } if (selRas.isSelectedPaint(xy) && !selRas.isPureInk(xy)) { getFactors(cmIn->pixels(samplePos.y)[samplePos.x].getTone(), inkFactor, paintFactor); color = &paintRas->pixels(samplePos.y)[samplePos.x]; pixSum.r += paintFactor * color->r; pixSum.g += paintFactor * color->g; pixSum.b += paintFactor * color->b; pixSum.m += paintFactor * color->m; factorsSum += paintFactor; } } }